update prisma / ch filters logic

This commit is contained in:
Francis Cao 2023-03-20 11:26:45 -07:00
parent 434ef3eb9e
commit b0c5899569
6 changed files with 49 additions and 24 deletions

View File

@ -40,7 +40,7 @@ export default function MetricsTable({
const { data, isLoading, isFetched, error } = useQuery(
[
'websites:mnetrics',
'websites:metrics',
{ websiteId, type, modified, url, referrer, os, browser, device, country },
],
() =>

View File

@ -85,11 +85,11 @@ SELECT website_id,
subdivision1,
subdivision2,
city,
url_path String,
url_query String,
referrer_path String,
referrer_query String,
referrer_domain String,
url_path,
url_query,
referrer_path,
referrer_query,
referrer_domain,
page_title,
event_type,
event_name,

View File

@ -74,6 +74,9 @@ function getFilterQuery(filters = {}, params = {}) {
switch (key) {
case 'url':
arr.push(`and url_path = {${key}:String}`);
params[key] = filter;
break;
case 'pageTitle':
case 'os':
case 'browser':
@ -92,18 +95,20 @@ function getFilterQuery(filters = {}, params = {}) {
break;
case 'referrer':
arr.push(`and referrer ILIKE {${key}:String}`);
params[key] = `%${filter}`;
arr.push(`and referrer_domain= {${key}:String}`);
params[key] = filter;
break;
case 'domain':
arr.push(`and referrer NOT ILIKE {${key}:String}`);
arr.push(`and referrer NOT ILIKE '/%'`);
arr.push(`and referrer_domain NOT ILIKE {${key}:String}`);
arr.push(`and referrer_domain NOT ILIKE '/%'`);
params[key] = `%://${filter}/%`;
break;
case 'query':
arr.push(`and url like '%?%'`);
arr.push(`and url_query= {${key}:String}`);
params[key] = filter;
break;
}
return arr;

View File

@ -74,6 +74,9 @@ function getFilterQuery(filters = {}, params = []): string {
switch (key) {
case 'url':
arr.push(`and url_path=$${params.length + 1}`);
params.push(decodeURIComponent(filter));
break;
case 'os':
case 'pageTitle':
case 'browser':
@ -92,18 +95,20 @@ function getFilterQuery(filters = {}, params = []): string {
break;
case 'referrer':
arr.push(`and referrer like $${params.length + 1}`);
params.push(`%${decodeURIComponent(filter)}%`);
arr.push(`and referrer_domain=$${params.length + 1}`);
params.push(decodeURIComponent(filter));
break;
case 'domain':
arr.push(`and referrer not like $${params.length + 1}`);
arr.push(`and referrer not like '/%'`);
arr.push(`and referrer_domain not like $${params.length + 1}`);
arr.push(`and referrer_domain not like '/%'`);
params.push(`%://${filter}/%`);
break;
case 'query':
arr.push(`and url like '%?%'`);
arr.push(`and url_query=$${params.length + 1}`);
params.push(decodeURIComponent(filter));
break;
}
return arr;

View File

@ -87,12 +87,12 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
let referrerQuery;
let referrerDomain;
if (referrer.substring(0, 4) === 'http') {
try {
const newRef = new URL(referrer);
referrerPath = newRef.pathname;
referrerDomain = newRef.hostname;
referrerQuery = newRef.search.substring(1);
} else {
} catch {
referrerPath = referrer.split('?')[0];
referrerQuery = referrer.split('?')[1];
}

View File

@ -6,7 +6,17 @@ import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewMetrics, getSessionMetrics, getWebsite } from 'queries';
const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language'];
const sessionColumns = [
'browser',
'os',
'device',
'screen',
'country',
'language',
'subdivision1',
'subdivision2',
'city',
];
const pageviewColumns = ['url', 'referrer', 'query', 'pageTitle'];
function getTable(type) {
@ -26,12 +36,17 @@ function getTable(type) {
}
function getColumn(type) {
if (type === 'event') {
return 'event_name';
}
if (type === 'query') {
return 'url';
switch (type) {
case 'url':
return 'url_path';
case 'referrer':
return 'referrer_domain';
case 'event':
return 'event_name';
case 'query':
return 'url_query';
}
return type;
}