Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/umami-software/umami into qa
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscao633 committed Aug 24, 2024
2 parents 41b5960 + a15d0ca commit 3042c19
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/components/hooks/queries/useRealtime.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useTimezone } from 'components/hooks';
import { REALTIME_INTERVAL } from 'lib/constants';
import { RealtimeData } from 'lib/types';
import { useApi } from './useApi';
import { REALTIME_INTERVAL } from 'lib/constants';

export function useRealtime(websiteId: string) {
const { get, useQuery } = useApi();
const { timezone } = useTimezone();
const { data, isLoading, error } = useQuery<RealtimeData>({
queryKey: ['realtime', websiteId],
queryKey: ['realtime', { websiteId, timezone }],
queryFn: async () => {
return get(`/realtime/${websiteId}`);
return get(`/realtime/${websiteId}`, { timezone });
},
enabled: !!websiteId,
refetchInterval: REALTIME_INTERVAL,
Expand Down
17 changes: 9 additions & 8 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { filtersToArray } from './params';
const log = debug('umami:prisma');

const MYSQL_DATE_FORMATS = {
minute: '%Y-%m-%dT%H:%i:00Z',
hour: '%Y-%m-%dT%H:00:00Z',
day: '%Y-%m-%dT00:00:00Z',
month: '%Y-%m-01T00:00:00Z',
year: '%Y-01-01T00:00:00Z',
minute: '%Y-%m-%dT%H:%i:00',
hour: '%Y-%m-%d %H:00:00',
day: '%Y-%m-%d',
month: '%Y-%m-01',
year: '%Y-01-01',
};

const POSTGRESQL_DATE_FORMATS = {
Expand Down Expand Up @@ -82,15 +82,16 @@ function getDateSQL(field: string, unit: string, timezone?: string): string {
}
}

function getDateWeeklySQL(field: string) {
function getDateWeeklySQL(field: string, timezone?: string) {
const db = getDatabaseType();

if (db === POSTGRESQL) {
return `concat(extract(dow from ${field}), ':', to_char(${field}, 'HH24'))`;
return `concat(extract(dow from (${field} at time zone '${timezone}')), ':', to_char((${field} at time zone '${timezone}'), 'HH24'))`;
}

if (db === MYSQL) {
return `date_format(${field}, '%w:%H')`;
const tz = moment.tz(timezone).format('Z');
return `date_format(convert_tz(${field},'+00:00','${tz}'), '%w:%H')`;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/pages/api/realtime/[websiteId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getRealtimeData } from 'queries';
import * as yup from 'yup';
import { REALTIME_RANGE } from 'lib/constants';
import { TimezoneTest } from 'lib/yup';

export interface RealtimeRequestQuery {
websiteId: string;
timezone?: string;
}

const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
timezone: TimezoneTest,
}),
};

Expand All @@ -23,15 +26,15 @@ export default async (req: NextApiRequestQueryBody<RealtimeRequestQuery>, res: N
await useValidate(schema, req, res);

if (req.method === 'GET') {
const { websiteId } = req.query;
const { websiteId, timezone } = req.query;

if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}

const startDate = subMinutes(startOfMinute(new Date()), REALTIME_RANGE);

const data = await getRealtimeData(websiteId, { startDate });
const data = await getRealtimeData(websiteId, { startDate, timezone });

return ok(res, data);
}
Expand Down
7 changes: 5 additions & 2 deletions src/pages/api/websites/[websiteId]/sessions/weekly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { pageInfo } from 'lib/schema';
import { getWebsiteSessionsWeekly } from 'queries';
import { TimezoneTest } from 'lib/yup';

export interface ReportsRequestQuery extends PageParams {
websiteId: string;
timezone?: string;
}

const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
endAt: yup.number().integer().min(yup.ref('startAt')).required(),
timezone: TimezoneTest,
...pageInfo,
}),
};
Expand All @@ -28,7 +31,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);

const { websiteId, startAt, endAt } = req.query;
const { websiteId, startAt, endAt, timezone } = req.query;

if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
Expand All @@ -38,7 +41,7 @@ export default async (
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);

const data = await getWebsiteSessionsWeekly(websiteId, { startDate, endDate });
const data = await getWebsiteSessionsWeekly(websiteId, { startDate, endDate, timezone });

return ok(res, data);
}
Expand Down
11 changes: 7 additions & 4 deletions src/queries/analytics/getRealtimeData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRealtimeActivity, getPageviewStats, getSessionStats } from 'queries/index';
import { getPageviewStats, getRealtimeActivity, getSessionStats } from 'queries/index';

function increment(data: object, key: string) {
if (key) {
Expand All @@ -10,9 +10,12 @@ function increment(data: object, key: string) {
}
}

export async function getRealtimeData(websiteId: string, criteria: { startDate: Date }) {
const { startDate } = criteria;
const filters = { startDate, endDate: new Date(), unit: 'minute' };
export async function getRealtimeData(
websiteId: string,
criteria: { startDate: Date; timezone: string },
) {
const { startDate, timezone } = criteria;
const filters = { startDate, endDate: new Date(), unit: 'minute', timezone };
const [activity, pageviews, sessions] = await Promise.all([
getRealtimeActivity(websiteId, filters),
getPageviewStats(websiteId, filters),
Expand Down
6 changes: 4 additions & 2 deletions src/queries/analytics/sessions/getWebsiteSessionsWeekly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export async function getWebsiteSessionsWeekly(
}

async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { timezone = 'utc' } = filters;
const { rawQuery, getDateWeeklySQL, parseFilters } = prisma;
const { params } = await parseFilters(websiteId, filters);

return rawQuery(
`
select
${getDateWeeklySQL('created_at')} as time,
${getDateWeeklySQL('created_at', timezone)} as time,
count(distinct session_id) as value
from website_event
where website_id = {{websiteId::uuid}}
Expand All @@ -32,13 +33,14 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
}

async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
const { timezone = 'utc' } = filters;
const { rawQuery } = clickhouse;
const { startDate, endDate } = filters;

return rawQuery(
`
select
formatDateTime(created_at, '%w:%H') as time,
formatDateTime(toDateTime(created_at, '${timezone}'), '%w:%H') as time,
count(distinct session_id) as value
from website_event_stats_hourly
where website_id = {websiteId:UUID}
Expand Down

0 comments on commit 3042c19

Please sign in to comment.