mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
feat: API for event data now can return associated event name
This commit is contained in:
parent
7bfbe26485
commit
92fc4e87c7
1
.gitignore
vendored
1
.gitignore
vendored
@ -25,6 +25,7 @@ node_modules
|
|||||||
*.iml
|
*.iml
|
||||||
*.log
|
*.log
|
||||||
.vscode
|
.vscode
|
||||||
|
.tool-versions
|
||||||
|
|
||||||
# debug
|
# debug
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
|
@ -21,13 +21,19 @@ export default async (
|
|||||||
await useAuth(req, res);
|
await useAuth(req, res);
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
const { websiteId, startAt, endAt, field } = req.query;
|
const { websiteId, startAt, endAt, field, withEventNames } = req.query;
|
||||||
|
|
||||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||||
return unauthorized(res);
|
return unauthorized(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt), field);
|
const data = await getEventDataFields(
|
||||||
|
websiteId,
|
||||||
|
new Date(+startAt),
|
||||||
|
new Date(+endAt),
|
||||||
|
field,
|
||||||
|
withEventNames,
|
||||||
|
);
|
||||||
|
|
||||||
return ok(res, data);
|
return ok(res, data);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,13 @@ import { loadWebsite } from 'lib/query';
|
|||||||
import { DEFAULT_CREATED_AT } from 'lib/constants';
|
import { DEFAULT_CREATED_AT } from 'lib/constants';
|
||||||
|
|
||||||
export async function getEventDataFields(
|
export async function getEventDataFields(
|
||||||
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
|
...args: [
|
||||||
|
websiteId: string,
|
||||||
|
startDate: Date,
|
||||||
|
endDate: Date,
|
||||||
|
field?: string,
|
||||||
|
withEventNames?: boolean,
|
||||||
|
]
|
||||||
): Promise<WebsiteEventDataFields[]> {
|
): Promise<WebsiteEventDataFields[]> {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
@ -14,7 +20,13 @@ export async function getEventDataFields(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
|
async function relationalQuery(
|
||||||
|
websiteId: string,
|
||||||
|
startDate: Date,
|
||||||
|
endDate: Date,
|
||||||
|
field: string,
|
||||||
|
withEventNames: boolean,
|
||||||
|
) {
|
||||||
const { toUuid, rawQuery } = prisma;
|
const { toUuid, rawQuery } = prisma;
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
||||||
@ -31,12 +43,30 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Date
|
|||||||
and created_at between $4 and $5
|
and created_at between $4 and $5
|
||||||
group by event_key, string_value
|
group by event_key, string_value
|
||||||
order by 3 desc, 2 desc, 1 asc
|
order by 3 desc, 2 desc, 1 asc
|
||||||
limit 100
|
|
||||||
`,
|
`,
|
||||||
[websiteId, field, resetDate, startDate, endDate] as any,
|
[websiteId, field, resetDate, startDate, endDate] as any,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (withEventNames) {
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
ed.event_key as field,
|
||||||
|
ed.data_type as type,
|
||||||
|
count(ed.*) as total,
|
||||||
|
e.event_name as event
|
||||||
|
from event_data as ed
|
||||||
|
join website_event as e on e.event_id = ed.website_event_id
|
||||||
|
where ed.website_id = $1${toUuid()}
|
||||||
|
and ed.created_at >= $2
|
||||||
|
and ed.created_at between $3 and $4
|
||||||
|
group by e.event_name, ed.event_key, ed.data_type
|
||||||
|
order by 3 desc, 2 asc, 1 asc
|
||||||
|
`,
|
||||||
|
[websiteId, resetDate, startDate, endDate] as any,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select
|
`select
|
||||||
event_key as field,
|
event_key as field,
|
||||||
@ -48,13 +78,18 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Date
|
|||||||
and created_at between $3 and $4
|
and created_at between $3 and $4
|
||||||
group by event_key, data_type
|
group by event_key, data_type
|
||||||
order by 3 desc, 2 asc, 1 asc
|
order by 3 desc, 2 asc, 1 asc
|
||||||
limit 100
|
|
||||||
`,
|
`,
|
||||||
[websiteId, resetDate, startDate, endDate] as any,
|
[websiteId, resetDate, startDate, endDate] as any,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
startDate: Date,
|
||||||
|
endDate: Date,
|
||||||
|
field: string,
|
||||||
|
withEventNames: boolean,
|
||||||
|
) {
|
||||||
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
|
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
||||||
@ -72,12 +107,30 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date
|
|||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
group by event_key, string_value
|
group by event_key, string_value
|
||||||
order by 3 desc, 2 desc, 1 asc
|
order by 3 desc, 2 desc, 1 asc
|
||||||
limit 100
|
|
||||||
`,
|
`,
|
||||||
{ websiteId, field },
|
{ websiteId, field },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (withEventNames) {
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
ed.event_key as field,
|
||||||
|
ed.data_type as type,
|
||||||
|
count(ed.*) as total,
|
||||||
|
e.event_name as event
|
||||||
|
from event_data as ed
|
||||||
|
join website_event as e on e.event_id = ed.website_event_id
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
group by e.event_name, ed.event_key, ed.data_type
|
||||||
|
order by 3 desc, 2 asc, 1 asc
|
||||||
|
`,
|
||||||
|
[websiteId, resetDate, startDate, endDate] as any,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select
|
`select
|
||||||
event_key as field,
|
event_key as field,
|
||||||
@ -89,7 +142,6 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date
|
|||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
group by event_key, data_type
|
group by event_key, data_type
|
||||||
order by 3 desc, 2 asc, 1 asc
|
order by 3 desc, 2 asc, 1 asc
|
||||||
limit 100
|
|
||||||
`,
|
`,
|
||||||
{ websiteId },
|
{ websiteId },
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user