Fix metrics.

This commit is contained in:
Brian Cao 2023-03-31 14:53:48 -07:00
parent 2172dddd1c
commit d2687b6580
3 changed files with 15 additions and 6 deletions

View File

@ -104,7 +104,7 @@ function getEventDataFilterQuery(
return query.join('\n');
}
function getFilterQuery(filters = {}, params = {}) {
function getFilterQuery(filters = {}, params = {}, field: string) {
const query = Object.keys(filters).reduce((arr, key) => {
const filter = filters[key];
@ -112,6 +112,10 @@ function getFilterQuery(filters = {}, params = {}) {
return arr;
}
if (key === field) {
return arr;
}
switch (key) {
case 'url':
arr.push(`and url_path = {${key}:String}`);
@ -157,7 +161,7 @@ function getFilterQuery(filters = {}, params = {}) {
return query.join('\n');
}
function parseFilters(filters: any = {}, params: any = {}) {
function parseFilters(filters: any = {}, params: any = {}, field?: string) {
const {
domain,
url,
@ -184,7 +188,7 @@ function parseFilters(filters: any = {}, params: any = {}) {
sessionFilters,
eventFilters,
event: { eventName },
filterQuery: getFilterQuery(filters, params),
filterQuery: getFilterQuery(filters, params, field),
};
}

View File

@ -106,7 +106,7 @@ function getEventDataFilterQuery(
return query.join('\n');
}
function getFilterQuery(filters = {}, params = []): string {
function getFilterQuery(filters = {}, params = [], field: string): string {
const query = Object.keys(filters).reduce((arr, key) => {
const filter = filters[key];
@ -114,6 +114,10 @@ function getFilterQuery(filters = {}, params = []): string {
return arr;
}
if (key === field) {
return arr;
}
switch (key) {
case 'url':
arr.push(`and url_path=$${params.length + 1}`);
@ -162,6 +166,7 @@ function getFilterQuery(filters = {}, params = []): string {
function parseFilters(
filters: { [key: string]: any } = {},
params = [],
field?: string,
sessionKey = 'session_id',
) {
const {
@ -194,7 +199,7 @@ function parseFilters(
os || browser || device || country || subdivision1 || subdivision2 || city
? `inner join session on website_event.${sessionKey} = session.${sessionKey}`
: '',
filterQuery: getFilterQuery(filters, params),
filterQuery: getFilterQuery(filters, params, field),
};
}

View File

@ -58,7 +58,7 @@ async function clickhouseQuery(
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params = { websiteId };
const { filterQuery } = parseFilters(filters, params);
const { filterQuery } = parseFilters(filters, params, field);
return rawQuery(
`select ${field} x, count(distinct session_id) y