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( const { data, isLoading, isFetched, error } = useQuery(
[ [
'websites:mnetrics', 'websites:metrics',
{ websiteId, type, modified, url, referrer, os, browser, device, country }, { websiteId, type, modified, url, referrer, os, browser, device, country },
], ],
() => () =>

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,17 @@ import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewMetrics, getSessionMetrics, getWebsite } from 'queries'; 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']; const pageviewColumns = ['url', 'referrer', 'query', 'pageTitle'];
function getTable(type) { function getTable(type) {
@ -26,12 +36,17 @@ function getTable(type) {
} }
function getColumn(type) { function getColumn(type) {
if (type === 'event') { switch (type) {
return 'event_name'; case 'url':
} return 'url_path';
if (type === 'query') { case 'referrer':
return 'url'; return 'referrer_domain';
case 'event':
return 'event_name';
case 'query':
return 'url_query';
} }
return type; return type;
} }