Fix filter issue for metrics. Closes #1268

This commit is contained in:
Mike Cao 2022-07-22 23:56:29 -07:00
parent 25d97fca95
commit ba1e28f082
4 changed files with 37 additions and 10 deletions

View File

@ -94,6 +94,8 @@ export const CLICKHOUSE_DATE_FORMATS = {
year: '%Y-01-01', year: '%Y-01-01',
}; };
export const FILTER_IGNORED = Symbol.for('filter-ignored');
export const DOMAIN_REGEX = 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})$/; /^(localhost(:[1-9]\d{0,4})?|((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63})$/;

View File

@ -8,6 +8,7 @@ import {
POSTGRESQL_DATE_FORMATS, POSTGRESQL_DATE_FORMATS,
CLICKHOUSE, CLICKHOUSE,
RELATIONAL, RELATIONAL,
FILTER_IGNORED,
} from 'lib/constants'; } from 'lib/constants';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { CLICKHOUSE_DATE_FORMATS } from './constants'; import { CLICKHOUSE_DATE_FORMATS } from './constants';
@ -156,9 +157,9 @@ export function getTimestampInterval(field) {
export function getFilterQuery(table, column, filters = {}, params = []) { export function getFilterQuery(table, column, filters = {}, params = []) {
const query = Object.keys(filters).reduce((arr, key) => { 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; return arr;
} }
@ -166,7 +167,7 @@ export function getFilterQuery(table, column, filters = {}, params = []) {
case 'url': case 'url':
if (table === 'pageview' || table === 'event') { if (table === 'pageview' || table === 'event') {
arr.push(`and ${table}.${key}=$${params.length + 1}`); arr.push(`and ${table}.${key}=$${params.length + 1}`);
params.push(decodeURIComponent(value)); params.push(decodeURIComponent(filter));
console.log(params); console.log(params);
} }
break; break;
@ -177,21 +178,21 @@ export function getFilterQuery(table, column, filters = {}, params = []) {
case 'country': case 'country':
if (table === 'session') { if (table === 'session') {
arr.push(`and ${table}.${key}=$${params.length + 1}`); arr.push(`and ${table}.${key}=$${params.length + 1}`);
params.push(decodeURIComponent(value)); params.push(decodeURIComponent(filter));
} }
break; break;
case 'event_type': case 'event_type':
if (table === 'event') { if (table === 'event') {
arr.push(`and ${table}.${key}=$${params.length + 1}`); arr.push(`and ${table}.${key}=$${params.length + 1}`);
params.push(decodeURIComponent(value)); params.push(decodeURIComponent(filter));
} }
break; break;
case 'referrer': case 'referrer':
if (table === 'pageview') { if (table === 'pageview' || table === 'event') {
arr.push(`and ${table}.referrer like $${params.length + 1}`); arr.push(`and ${table}.referrer like $${params.length + 1}`);
params.push(`%${decodeURIComponent(value)}%`); params.push(`%${decodeURIComponent(filter)}%`);
} }
break; break;
@ -199,7 +200,7 @@ export function getFilterQuery(table, column, filters = {}, params = []) {
if (table === 'pageview') { if (table === 'pageview') {
arr.push(`and ${table}.referrer not like $${params.length + 1}`); arr.push(`and ${table}.referrer not like $${params.length + 1}`);
arr.push(`and ${table}.referrer not like '/%'`); arr.push(`and ${table}.referrer not like '/%'`);
params.push(`%://${value}/%`); params.push(`%://${filter}/%`);
} }
break; break;
} }

View File

@ -2,6 +2,7 @@ import { getPageviewMetrics, getSessionMetrics, getWebsiteById, getPageviewParam
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response'; import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware'; import { useCors } from 'lib/middleware';
import { FILTER_IGNORED } from 'lib/constants';
const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language']; const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language'];
const pageviewColumns = ['url', 'referrer']; const pageviewColumns = ['url', 'referrer'];
@ -130,7 +131,7 @@ export default async (req, res) => {
const filters = { const filters = {
domain, domain,
url: type !== 'url' && table !== 'event' ? url : undefined, 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, os: type !== 'os' ? os : undefined,
browser: type !== 'browser' ? browser : undefined, browser: type !== 'browser' ? browser : undefined,
device: type !== 'device' ? device : undefined, device: type !== 'device' ? device : undefined,

View File

@ -4218,10 +4218,33 @@ jsonify@~0.0.0:
resolved "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz" resolved "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz"
integrity sha512-trvBk1ki43VZptdBI5rIlG4YOzyeH/WefQt5rj1grasPn4iiZWKet8nkgc4GlsAylaztn0qZfUYOiTsASJFdNA== 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" version "3.3.0"
resolved "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.0.tgz" resolved "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.0.tgz"
integrity sha512-XzO9luP6L0xkxwhIJMTJQpZo/eeN60K08jHdexfD569AGxeNug6UketeHXEhROoM8aR7EcUoOQmIhcJQjcuq8Q== 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: dependencies:
array-includes "^3.1.5" array-includes "^3.1.5"
object.assign "^4.1.2" object.assign "^4.1.2"