From ba1e28f0820adb05a178c167dd343d623a4fe7ef Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 22 Jul 2022 23:56:29 -0700 Subject: [PATCH] Fix filter issue for metrics. Closes #1268 --- lib/constants.js | 2 ++ lib/db.js | 17 +++++++++-------- pages/api/website/[id]/metrics.js | 3 ++- yarn.lock | 25 ++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/constants.js b/lib/constants.js index 59eb6b24..f971ee02 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -94,6 +94,8 @@ export const CLICKHOUSE_DATE_FORMATS = { year: '%Y-01-01', }; +export const FILTER_IGNORED = Symbol.for('filter-ignored'); + export const DOMAIN_REGEX = /^(localhost(:[1-9]\d{0,4})?|((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63})$/; diff --git a/lib/db.js b/lib/db.js index 10a08296..378ca742 100644 --- a/lib/db.js +++ b/lib/db.js @@ -8,6 +8,7 @@ import { POSTGRESQL_DATE_FORMATS, CLICKHOUSE, RELATIONAL, + FILTER_IGNORED, } from 'lib/constants'; import moment from 'moment-timezone'; import { CLICKHOUSE_DATE_FORMATS } from './constants'; @@ -156,9 +157,9 @@ export function getTimestampInterval(field) { export function getFilterQuery(table, column, filters = {}, params = []) { const query = Object.keys(filters).reduce((arr, key) => { - const value = filters[key]; + const filter = filters[key]; - if (value === undefined) { + if (filter === undefined || filter === FILTER_IGNORED) { return arr; } @@ -166,7 +167,7 @@ export function getFilterQuery(table, column, filters = {}, params = []) { case 'url': if (table === 'pageview' || table === 'event') { arr.push(`and ${table}.${key}=$${params.length + 1}`); - params.push(decodeURIComponent(value)); + params.push(decodeURIComponent(filter)); console.log(params); } break; @@ -177,21 +178,21 @@ export function getFilterQuery(table, column, filters = {}, params = []) { case 'country': if (table === 'session') { arr.push(`and ${table}.${key}=$${params.length + 1}`); - params.push(decodeURIComponent(value)); + params.push(decodeURIComponent(filter)); } break; case 'event_type': if (table === 'event') { arr.push(`and ${table}.${key}=$${params.length + 1}`); - params.push(decodeURIComponent(value)); + params.push(decodeURIComponent(filter)); } break; case 'referrer': - if (table === 'pageview') { + if (table === 'pageview' || table === 'event') { arr.push(`and ${table}.referrer like $${params.length + 1}`); - params.push(`%${decodeURIComponent(value)}%`); + params.push(`%${decodeURIComponent(filter)}%`); } break; @@ -199,7 +200,7 @@ export function getFilterQuery(table, column, filters = {}, params = []) { if (table === 'pageview') { arr.push(`and ${table}.referrer not like $${params.length + 1}`); arr.push(`and ${table}.referrer not like '/%'`); - params.push(`%://${value}/%`); + params.push(`%://${filter}/%`); } break; } diff --git a/pages/api/website/[id]/metrics.js b/pages/api/website/[id]/metrics.js index 5df159d6..b12566fd 100644 --- a/pages/api/website/[id]/metrics.js +++ b/pages/api/website/[id]/metrics.js @@ -2,6 +2,7 @@ import { getPageviewMetrics, getSessionMetrics, getWebsiteById, getPageviewParam import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response'; import { allowQuery } from 'lib/auth'; import { useCors } from 'lib/middleware'; +import { FILTER_IGNORED } from 'lib/constants'; const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language']; const pageviewColumns = ['url', 'referrer']; @@ -130,7 +131,7 @@ export default async (req, res) => { const filters = { domain, url: type !== 'url' && table !== 'event' ? url : undefined, - referrer: type !== 'referrer' ? referrer : true, + referrer: type !== 'referrer' && table !== 'event' ? referrer : FILTER_IGNORED, os: type !== 'os' ? os : undefined, browser: type !== 'browser' ? browser : undefined, device: type !== 'device' ? device : undefined, diff --git a/yarn.lock b/yarn.lock index 8f9857f9..144118b7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4218,10 +4218,33 @@ jsonify@~0.0.0: resolved "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz" integrity sha512-trvBk1ki43VZptdBI5rIlG4YOzyeH/WefQt5rj1grasPn4iiZWKet8nkgc4GlsAylaztn0qZfUYOiTsASJFdNA== -"jsx-ast-utils@^2.4.1 || ^3.0.0", jsx-ast-utils@^3.2.1: +jsonparse@^1.2.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280" + integrity sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg== + +jsprim@^1.2.2: + version "1.4.2" + resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.2.tgz#712c65533a15c878ba59e9ed5f0e26d5b77c5feb" + integrity sha512-P2bSOMAc/ciLz6DzgjVlGJP9+BrJWu5UDGK70C2iweC5QBIeFf0ZXRvGjEj2uYgrY2MkAAhsSWHDWlFtEroZWw== + dependencies: + assert-plus "1.0.0" + extsprintf "1.3.0" + json-schema "0.4.0" + verror "1.10.0" + +"jsx-ast-utils@^2.4.1 || ^3.0.0": version "3.3.0" resolved "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.0.tgz" integrity sha512-XzO9luP6L0xkxwhIJMTJQpZo/eeN60K08jHdexfD569AGxeNug6UketeHXEhROoM8aR7EcUoOQmIhcJQjcuq8Q== + dependencies: + array-includes "^3.1.4" + object.assign "^4.1.2" + +jsx-ast-utils@^3.3.1: + version "3.3.2" + resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.3.2.tgz#afe5efe4332cd3515c065072bd4d6b0aa22152bd" + integrity sha512-4ZCADZHRkno244xlNnn4AOG6sRQ7iBZ5BbgZ4vW4y5IZw7cVUD1PPeblm1xx/nfmMxPdt/LHsXZW8z/j58+l9Q== dependencies: array-includes "^3.1.5" object.assign "^4.1.2"