mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 08:37:13 +01:00
Use dataStartDate for resetDate.
This commit is contained in:
parent
ee8de858d1
commit
0f98fb5959
15 changed files with 172 additions and 85 deletions
47
queries/analytics/stats/getWebsiteDateRange.ts
Normal file
47
queries/analytics/stats/getWebsiteDateRange.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { DEFAULT_RESET_DATE } from 'lib/constants';
|
||||
|
||||
export async function getWebsiteDateRange(...args: [websiteId: string]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string) {
|
||||
const { rawQuery } = prisma;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
min(created_at) as min,
|
||||
max(created_at) as max
|
||||
from website_event
|
||||
join website
|
||||
on website_event.website_id = website.website_id
|
||||
where website.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at >= coalesce(website.reset_at, website.created_at)
|
||||
`,
|
||||
{ websiteId },
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string) {
|
||||
const { rawQuery } = clickhouse;
|
||||
const website = await loadWebsite(websiteId);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
min(created_at) as min,
|
||||
max(created_at) as max
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at >= {dataStartDate:DateTime}
|
||||
`,
|
||||
{ websiteId, dataStartDate: website.dataStartDate },
|
||||
);
|
||||
}
|
||||
|
|
@ -23,7 +23,6 @@ async function relationalQuery(
|
|||
const { startDate, endDate, filters = {} } = criteria;
|
||||
const { getDateQuery, getTimestampIntervalQuery, parseFilters, rawQuery } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
|
||||
const { filterQuery, joinSession } = parseFilters(filters);
|
||||
|
||||
return rawQuery(
|
||||
|
|
@ -45,13 +44,20 @@ async function relationalQuery(
|
|||
${joinSession}
|
||||
where event_type = {{eventType}}
|
||||
and website.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at >= {{resetDate}}
|
||||
and website_event.created_at >= {{dataStartDate}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
${filterQuery}
|
||||
group by 1, 2
|
||||
) as t
|
||||
`,
|
||||
{ ...filters, websiteId, resetDate, startDate, endDate, eventType: EVENT_TYPE.pageView },
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
dataStartDate: website.dataStartDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -62,7 +68,6 @@ async function clickhouseQuery(
|
|||
const { startDate, endDate, filters = {} } = criteria;
|
||||
const { rawQuery, getDateQuery, parseFilters } = clickhouse;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
|
||||
const { filterQuery } = parseFilters(filters);
|
||||
|
||||
return rawQuery(
|
||||
|
|
@ -81,13 +86,20 @@ async function clickhouseQuery(
|
|||
max(created_at) max_time
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at >= {resetDate:DateTime}
|
||||
and created_at >= {dataStartDate:DateTime}
|
||||
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by session_id, time_series
|
||||
) as t;
|
||||
`,
|
||||
{ ...filters, websiteId, resetDate, startDate, endDate, eventType: EVENT_TYPE.pageView },
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
dataStartDate: website.dataStartDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue