mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-06 17:35:45 +01:00
Added parseDateRangeQuery function.
This commit is contained in:
parent
a0aaeeeb57
commit
09af33c77e
@ -13,7 +13,7 @@ import {
|
||||
import { getTeamUser } from 'queries';
|
||||
import { getTeamWebsite, getTeamWebsiteByTeamMemberId } from 'queries/admin/teamWebsite';
|
||||
import { validate } from 'uuid';
|
||||
import { loadWebsite } from './query';
|
||||
import { loadWebsite } from './load';
|
||||
import { Auth } from './types';
|
||||
|
||||
const log = debug('umami:auth');
|
||||
|
33
lib/date.js
33
lib/date.js
@ -26,9 +26,20 @@ import {
|
||||
differenceInCalendarMonths,
|
||||
differenceInCalendarYears,
|
||||
format,
|
||||
max,
|
||||
min,
|
||||
isDate,
|
||||
} from 'date-fns';
|
||||
import { getDateLocale } from 'lib/lang';
|
||||
|
||||
const dateFuncs = {
|
||||
minute: [differenceInMinutes, addMinutes, startOfMinute],
|
||||
hour: [differenceInHours, addHours, startOfHour],
|
||||
day: [differenceInCalendarDays, addDays, startOfDay],
|
||||
month: [differenceInCalendarMonths, addMonths, startOfMonth],
|
||||
year: [differenceInCalendarYears, addYears, startOfYear],
|
||||
};
|
||||
|
||||
export function getTimezone() {
|
||||
return moment.tz.guess();
|
||||
}
|
||||
@ -155,10 +166,12 @@ export function parseDateRange(value, locale = 'en-US') {
|
||||
}
|
||||
}
|
||||
|
||||
export function getAllowedUnits(unit) {
|
||||
export function getAllowedUnits(startDate, endDate) {
|
||||
const units = ['minute', 'hour', 'day', 'month', 'year'];
|
||||
const minUnit = getMinimumUnit(startDate, endDate);
|
||||
const index = units.indexOf(minUnit);
|
||||
|
||||
return units.splice(units.indexOf(unit));
|
||||
return index >= 0 ? units.splice(index) : [];
|
||||
}
|
||||
|
||||
export function getMinimumUnit(startDate, endDate) {
|
||||
@ -196,14 +209,6 @@ export function getDateFromString(str) {
|
||||
return new Date(year, month - 1, day);
|
||||
}
|
||||
|
||||
const dateFuncs = {
|
||||
minute: [differenceInMinutes, addMinutes, startOfMinute],
|
||||
hour: [differenceInHours, addHours, startOfHour],
|
||||
day: [differenceInCalendarDays, addDays, startOfDay],
|
||||
month: [differenceInCalendarMonths, addMonths, startOfMonth],
|
||||
year: [differenceInCalendarYears, addYears, startOfYear],
|
||||
};
|
||||
|
||||
export function getDateArray(data, startDate, endDate, unit) {
|
||||
const arr = [];
|
||||
const [diff, add, normalize] = dateFuncs[unit];
|
||||
@ -249,3 +254,11 @@ export function dateFormat(date, str, locale = 'en-US') {
|
||||
locale: getDateLocale(locale),
|
||||
});
|
||||
}
|
||||
|
||||
export function maxDate(...args) {
|
||||
return max(args.filter(n => isDate(n)));
|
||||
}
|
||||
|
||||
export function minDate(...args) {
|
||||
return min(args.filter(n => isDate(n)));
|
||||
}
|
||||
|
51
lib/load.ts
Normal file
51
lib/load.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import cache from 'lib/cache';
|
||||
import { getWebsite, getSession, getUser } from 'queries';
|
||||
import { User, Website, Session } from '@prisma/client';
|
||||
|
||||
export async function loadWebsite(websiteId: string): Promise<Website> {
|
||||
let website;
|
||||
|
||||
if (cache.enabled) {
|
||||
website = await cache.fetchWebsite(websiteId);
|
||||
} else {
|
||||
website = await getWebsite({ id: websiteId });
|
||||
}
|
||||
|
||||
if (!website || website.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return website;
|
||||
}
|
||||
|
||||
export async function loadSession(sessionId: string): Promise<Session> {
|
||||
let session;
|
||||
|
||||
if (cache.enabled) {
|
||||
session = await cache.fetchSession(sessionId);
|
||||
} else {
|
||||
session = await getSession({ id: sessionId });
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function loadUser(userId: string): Promise<User> {
|
||||
let user;
|
||||
|
||||
if (cache.enabled) {
|
||||
user = await cache.fetchUser(userId);
|
||||
} else {
|
||||
user = await getUser({ id: userId });
|
||||
}
|
||||
|
||||
if (!user || user.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
@ -73,5 +73,6 @@ export const useAuth = createMiddleware(async (req, res, next) => {
|
||||
}
|
||||
|
||||
(req as any).auth = { user, token, shareToken, authKey };
|
||||
|
||||
next();
|
||||
});
|
||||
|
69
lib/query.ts
69
lib/query.ts
@ -1,51 +1,30 @@
|
||||
import cache from 'lib/cache';
|
||||
import { getWebsite, getSession, getUser } from 'queries';
|
||||
import { User, Website, Session } from '@prisma/client';
|
||||
import { NextApiRequest } from 'next';
|
||||
import { getAllowedUnits, getMinimumUnit } from './date';
|
||||
import { getWebsiteDateRange } from '../queries';
|
||||
|
||||
export async function loadWebsite(websiteId: string): Promise<Website> {
|
||||
let website;
|
||||
export async function parseDateRangeQuery(req: NextApiRequest) {
|
||||
const { id: websiteId, startAt, endAt, unit } = req.query;
|
||||
|
||||
if (cache.enabled) {
|
||||
website = await cache.fetchWebsite(websiteId);
|
||||
} else {
|
||||
website = await getWebsite({ id: websiteId });
|
||||
// All-time
|
||||
if (+startAt === 0 && +endAt === 1) {
|
||||
const { min, max } = await getWebsiteDateRange(websiteId as string);
|
||||
|
||||
return {
|
||||
websiteId,
|
||||
startDate: min,
|
||||
endDate: max,
|
||||
unit: getMinimumUnit(min, max),
|
||||
};
|
||||
}
|
||||
|
||||
if (!website || website.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
const startDate = new Date(+startAt);
|
||||
const endDate = new Date(+endAt);
|
||||
const minUnit = getMinimumUnit(startDate, endDate);
|
||||
|
||||
return website;
|
||||
}
|
||||
|
||||
export async function loadSession(sessionId: string): Promise<Session> {
|
||||
let session;
|
||||
|
||||
if (cache.enabled) {
|
||||
session = await cache.fetchSession(sessionId);
|
||||
} else {
|
||||
session = await getSession({ id: sessionId });
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function loadUser(userId: string): Promise<User> {
|
||||
let user;
|
||||
|
||||
if (cache.enabled) {
|
||||
user = await cache.fetchUser(userId);
|
||||
} else {
|
||||
user = await getUser({ id: userId });
|
||||
}
|
||||
|
||||
if (!user || user.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
return {
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
unit: getAllowedUnits(startDate, endDate).includes(unit as string) ? unit : minUnit,
|
||||
};
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
|
||||
import { createSession } from 'queries';
|
||||
import { validate } from 'uuid';
|
||||
import cache from './cache';
|
||||
import { loadSession, loadWebsite } from './query';
|
||||
import { loadSession, loadWebsite } from './load';
|
||||
|
||||
export async function findSession(req: NextApiRequestCollect) {
|
||||
const { payload } = getJsonBody<CollectRequestBody>(req);
|
||||
|
@ -5,6 +5,7 @@ import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors } from 'lib/middleware';
|
||||
import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS } from 'lib/constants';
|
||||
import { getPageviewMetrics, getSessionMetrics } from 'queries';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
|
||||
export interface WebsiteMetricsRequestQuery {
|
||||
id: string;
|
||||
@ -34,8 +35,6 @@ export default async (
|
||||
const {
|
||||
id: websiteId,
|
||||
type,
|
||||
startAt,
|
||||
endAt,
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
@ -54,8 +53,7 @@ export default async (
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const startDate = new Date(+startAt);
|
||||
const endDate = new Date(+endAt);
|
||||
const { startDate, endDate } = await parseDateRangeQuery(req);
|
||||
|
||||
if (SESSION_COLUMNS.includes(type)) {
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
|
@ -2,9 +2,8 @@ import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import { WebsiteEventDataFields } from 'lib/types';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { DEFAULT_RESET_DATE } from 'lib/constants';
|
||||
import { max } from 'date-fns';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getEventDataEvents(
|
||||
...args: [
|
||||
@ -48,7 +47,7 @@ async function relationalQuery(
|
||||
group by ed.event_key, ed.string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
`,
|
||||
{ ...filters, websiteId, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ ...filters, websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
return rawQuery(
|
||||
@ -67,7 +66,7 @@ async function relationalQuery(
|
||||
group by we.event_name, ed.event_key, ed.string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
`,
|
||||
{ websiteId, field, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ websiteId, field, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
||||
@ -98,7 +97,7 @@ async function clickhouseQuery(
|
||||
order by 1 asc, 2 asc, 3 asc, 4 desc
|
||||
limit 100
|
||||
`,
|
||||
{ ...filters, websiteId, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ ...filters, websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
||||
@ -116,6 +115,6 @@ async function clickhouseQuery(
|
||||
order by 1 asc, 2 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import { WebsiteEventDataFields } from 'lib/types';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { max } from 'date-fns';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getEventDataFields(
|
||||
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
|
||||
@ -33,7 +33,7 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Date
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, field, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ websiteId, field, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Date
|
||||
order by 3 desc, 2 asc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, field, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ websiteId, field, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
||||
@ -90,6 +90,6 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date
|
||||
order by 3 desc, 2 asc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { WebsiteEventMetric } from 'lib/types';
|
||||
import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { max } from 'date-fns';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getEventMetrics(
|
||||
...args: [
|
||||
@ -67,7 +67,7 @@ async function relationalQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.customEvent,
|
||||
},
|
||||
@ -114,7 +114,7 @@ async function clickhouseQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.customEvent,
|
||||
},
|
||||
|
@ -2,8 +2,8 @@ import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { max } from 'date-fns';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getPageviewMetrics(
|
||||
...args: [
|
||||
@ -36,7 +36,7 @@ async function relationalQuery(
|
||||
const website = await loadWebsite(websiteId);
|
||||
const params: any = {
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
};
|
||||
@ -84,7 +84,7 @@ async function clickhouseQuery(
|
||||
const website = await loadWebsite(websiteId);
|
||||
const params = {
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
domain: undefined,
|
||||
|
@ -2,8 +2,8 @@ import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { max } from 'date-fns';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getPageviewStats(
|
||||
...args: [
|
||||
@ -66,7 +66,7 @@ async function relationalQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
@ -118,7 +118,7 @@ async function clickhouseQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
|
@ -2,8 +2,8 @@ import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { max } from 'date-fns';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getSessionMetrics(
|
||||
...args: [
|
||||
@ -42,7 +42,7 @@ async function relationalQuery(
|
||||
group by 1
|
||||
order by 2 desc
|
||||
limit 100`,
|
||||
{ ...filters, websiteId, startDate: max([startDate, website.resetAt]), endDate },
|
||||
{ ...filters, websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
|
||||
);
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ async function clickhouseQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
|
@ -1,9 +1,9 @@
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { DEFAULT_RESET_DATE } from 'lib/constants';
|
||||
import { max } from 'date-fns';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getWebsiteDateRange(...args: [websiteId: string]) {
|
||||
return runQuery({
|
||||
@ -43,6 +43,6 @@ async function clickhouseQuery(websiteId: string) {
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at >= {startDate:DateTime}
|
||||
`,
|
||||
{ websiteId, startDate: max([new Date('2000-01-01'), website.resetAt]) },
|
||||
{ websiteId, startDate: maxDate(new Date(DEFAULT_RESET_DATE), website.resetAt) },
|
||||
);
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
import { max } from 'date-fns';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getWebsiteStats(
|
||||
...args: [
|
||||
@ -53,7 +53,7 @@ async function relationalQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
@ -94,7 +94,7 @@ async function clickhouseQuery(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: max([startDate, website.resetAt]),
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user