Merge branch 'dev' into jajaja

# Conflicts:
#	db/postgresql/schema.prisma
#	src/queries/sql/reports/getRevenue.ts
This commit is contained in:
Mike Cao 2025-06-10 21:11:44 -07:00
commit b2aa37a3df
9 changed files with 279 additions and 200 deletions

View file

@ -5,6 +5,7 @@ import kafka from '@/lib/kafka';
import prisma from '@/lib/prisma';
import { uuid } from '@/lib/crypto';
import { saveEventData } from './saveEventData';
import { saveRevenue } from './saveRevenue';
export interface SaveEventArgs {
websiteId: string;
@ -130,6 +131,20 @@ async function relationalQuery({
eventData,
createdAt,
});
const { revenue, currency } = eventData;
if (revenue > 0 && currency) {
await saveRevenue({
websiteId,
sessionId,
eventId: websiteEventId,
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
currency,
revenue,
createdAt,
});
}
}
}

View file

@ -0,0 +1,36 @@
import { uuid } from '@/lib/crypto';
import { PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
export interface SaveRevenueArgs {
websiteId: string;
sessionId: string;
eventId: string;
eventName: string;
currency: string;
revenue: number;
createdAt: Date;
}
export async function saveRevenue(data: SaveRevenueArgs) {
return runQuery({
[PRISMA]: () => relationalQuery(data),
});
}
async function relationalQuery(data: SaveRevenueArgs) {
const { websiteId, sessionId, eventId, eventName, currency, revenue, createdAt } = data;
await prisma.client.revenue.create({
data: {
id: uuid(),
websiteId,
sessionId,
eventId,
eventName,
currency,
revenue,
createdAt,
},
});
}

View file

@ -69,26 +69,16 @@ async function relationalQuery(
group by 1),`;
const revenueEventQuery = `WITH events AS (
select
we.session_id,
max(ed.created_at) max_dt,
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) value
from event_data ed
join website_event we
on we.event_id = ed.website_event_id
and we.website_id = ed.website_id
join (select website_event_id
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key ${like} '%currency%'
and string_value = {{currency}}) currency
on currency.website_event_id = ed.website_event_id
where ed.website_id = {{websiteId::uuid}}
and ed.created_at between {{startDate}} and {{endDate}}
and ${column} = {{conversionStep}}
and ed.data_key ${like} '%revenue%'
group by 1),`;
select
session_id,
max(created_at) max_dt,
sum(revenue) value
from revenue
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and ${column} = {{conversionStep}}
and currency ${like} {{currency}}
group by 1),`;
function getModelQuery(model: string) {
return model === 'firstClick'

View file

@ -38,22 +38,13 @@ async function relationalQuery(
const chartRes = await rawQuery(
`
select
we.event_name x,
${getDateSQL('ed.created_at', unit)} t,
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) y
from event_data ed
join website_event we
on we.event_id = ed.website_event_id
join (select website_event_id
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key ${like} '%currency%'
and string_value = {{currency}}) currency
on currency.website_event_id = ed.website_event_id
where ed.website_id = {{websiteId::uuid}}
and ed.created_at between {{startDate}} and {{endDate}}
and ed.data_key ${like} '%revenue%'
event_name x,
${getDateSQL('created_at', unit)} t,
sum(revenue) y
from revenue
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and currency ${like} {{currency}}
group by x, t
order by t
`,
@ -64,22 +55,13 @@ async function relationalQuery(
`
select
s.country as name,
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) value
from event_data ed
join website_event we
on we.event_id = ed.website_event_id
sum(r.revenue) value
from revenue r
join session s
on s.session_id = we.session_id
join (select website_event_id
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key ${like} '%currency%'
and string_value = {{currency}}) currency
on currency.website_event_id = ed.website_event_id
where ed.website_id = {{websiteId::uuid}}
and ed.created_at between {{startDate}} and {{endDate}}
and ed.data_key ${like} '%revenue%'
on s.session_id = r.session_id
where r.website_id = {{websiteId::uuid}}
and r.created_at between {{startDate}} and {{endDate}}
and r.currency ${like} {{currency}}
group by s.country
`,
{ websiteId, startDate, endDate, currency },
@ -88,22 +70,13 @@ async function relationalQuery(
const totalRes = await rawQuery(
`
select
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) as sum,
sum(revenue) as sum,
count(distinct event_id) as count,
count(distinct session_id) as unique_count
from event_data ed
join website_event we
on we.event_id = ed.website_event_id
join (select website_event_id
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key ${like} '%currency%'
and string_value = {{currency}}) currency
on currency.website_event_id = ed.website_event_id
where ed.website_id = {{websiteId::uuid}}
and ed.created_at between {{startDate}} and {{endDate}}
and ed.data_key ${like} '%revenue%'
from revenue r
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and currency ${like} {{currency}}
`,
{ websiteId, startDate, endDate, currency },
).then(result => result?.[0]);
@ -111,24 +84,15 @@ async function relationalQuery(
const tableRes = await rawQuery(
`
select
c.currency,
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) as sum,
count(distinct ed.website_event_id) as count,
count(distinct we.session_id) as unique_count
from event_data ed
join website_event we
on we.event_id = ed.website_event_id
join (select website_event_id, string_value as currency
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key ${like} '%currency%') c
on c.website_event_id = ed.website_event_id
where ed.website_id = {{websiteId::uuid}}
and ed.created_at between {{startDate}} and {{endDate}}
and ed.data_key ${like} '%revenue%'
group by c.currency
order by sum desc;
currency,
sum(revenue) as sum,
count(distinct event_id) as count,
count(distinct session_id) as unique_count
from revenue r
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
group by currency
order by sum desc
`,
{ websiteId, startDate, endDate, unit, currency },
);