Skip to content

Commit

Permalink
Conditionally add keys filter to filterStr
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPetrey committed Dec 14, 2024
1 parent 80ced0d commit 57fba6c
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions app/analytics/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,27 +484,22 @@ export class AnalyticsEngineAPI {
// on the keys returned by the first query.
const keys = visitorCountByColumn.map(([key]) => key);

const filterStr = filtersToSql(filters);
const _column = ColumnMappings[column];
let filterStr = filtersToSql(filters);

const whereConditions = [
`timestamp >= ${startIntervalSql} AND timestamp < ${endIntervalSql}`,
keys.length > 0
? `${_column} IN (${keys.map((key) => `'${key}'`).join(", ")})`
: null,
`${ColumnMappings.newVisitor} = 0`,
`${ColumnMappings.siteId} = '${siteId}'`,
filterStr || null,
].filter(Boolean);

const whereClause = whereConditions.join(" AND ");
const _column = ColumnMappings[column];
if (keys.length > 0) {
filterStr += `AND ${_column} IN (${keys.map((key) => `'${key}'`).join(", ")})`;
}

const query = `
SELECT ${_column},
${ColumnMappings.newVisitor} as isVisitor,
SUM(_sample_interval) as count
FROM metricsDataset
WHERE ${whereClause}
WHERE timestamp >= ${startIntervalSql} AND timestamp < ${endIntervalSql}
AND ${ColumnMappings.newVisitor} = 0
AND ${ColumnMappings.siteId} = '${siteId}'
${filterStr}
GROUP BY ${_column}, ${ColumnMappings.newVisitor}
ORDER BY count DESC
LIMIT ${limit * page}`;
Expand Down

0 comments on commit 57fba6c

Please sign in to comment.