mirror of
https://github.com/umami-software/umami.git
synced 2026-02-05 05:07:15 +01:00
Renamed folder.
This commit is contained in:
parent
8525188e42
commit
dcf0da7b14
39 changed files with 0 additions and 0 deletions
118
src/queries/sql/getValues.ts
Normal file
118
src/queries/sql/getValues.ts
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
import prisma from '@/lib/prisma';
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from '@/lib/db';
|
||||
|
||||
export async function getValues(
|
||||
...args: [websiteId: string, column: string, startDate: Date, endDate: Date, search: string]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
column: string,
|
||||
startDate: Date,
|
||||
endDate: Date,
|
||||
search: string,
|
||||
) {
|
||||
const { rawQuery, getSearchSQL } = prisma;
|
||||
const params = {};
|
||||
let searchQuery = '';
|
||||
|
||||
if (search) {
|
||||
if (decodeURIComponent(search).includes(',')) {
|
||||
searchQuery = `AND (${decodeURIComponent(search)
|
||||
.split(',')
|
||||
.slice(0, 5)
|
||||
.map((value: string, index: number) => {
|
||||
const key = `search${index}`;
|
||||
|
||||
params[key] = value;
|
||||
|
||||
return getSearchSQL(column, key).replace('and ', '');
|
||||
})
|
||||
.join(' OR ')})`;
|
||||
} else {
|
||||
searchQuery = getSearchSQL(column);
|
||||
}
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${column} as "value", count(*) as "count"
|
||||
from website_event
|
||||
inner join session
|
||||
on session.session_id = website_event.session_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
${searchQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
limit 10
|
||||
`,
|
||||
{
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
search: `%${search}%`,
|
||||
...params,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
column: string,
|
||||
startDate: Date,
|
||||
endDate: Date,
|
||||
search: string,
|
||||
) {
|
||||
const { rawQuery, getSearchSQL } = clickhouse;
|
||||
const params = {};
|
||||
let searchQuery = '';
|
||||
|
||||
if (search) {
|
||||
searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`;
|
||||
}
|
||||
|
||||
if (search) {
|
||||
if (decodeURIComponent(search).includes(',')) {
|
||||
searchQuery = `AND (${decodeURIComponent(search)
|
||||
.split(',')
|
||||
.slice(0, 5)
|
||||
.map((value: string, index: number) => {
|
||||
const key = `search${index}`;
|
||||
|
||||
params[key] = value;
|
||||
|
||||
return getSearchSQL(column, key).replace('and ', '');
|
||||
})
|
||||
.join(' OR ')})`;
|
||||
} else {
|
||||
searchQuery = getSearchSQL(column);
|
||||
}
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${column} as "value", count(*) as "count"
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
${searchQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
limit 10
|
||||
`,
|
||||
{
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
search,
|
||||
...params,
|
||||
},
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue