mirror of
https://github.com/umami-software/umami.git
synced 2026-02-23 22:15:35 +01:00
Compare commits
2 commits
14f5babea7
...
bf498d9239
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf498d9239 | ||
|
|
30781430c5 |
6 changed files with 27 additions and 34 deletions
|
|
@ -1,21 +1,15 @@
|
||||||
import { REALTIME_RANGE } from '@/lib/constants';
|
import { REALTIME_RANGE } from '@/lib/constants';
|
||||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||||
import { json, unauthorized } from '@/lib/response';
|
import { json, unauthorized } from '@/lib/response';
|
||||||
import { timezoneParam } from '@/lib/schema';
|
|
||||||
import { canViewWebsite } from '@/permissions';
|
import { canViewWebsite } from '@/permissions';
|
||||||
import { getRealtimeData } from '@/queries/sql';
|
import { getRealtimeData } from '@/queries/sql';
|
||||||
import { startOfMinute, subMinutes } from 'date-fns';
|
import { startOfMinute, subMinutes } from 'date-fns';
|
||||||
import z from 'zod';
|
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(
|
||||||
request: Request,
|
request: Request,
|
||||||
{ params }: { params: Promise<{ websiteId: string }> },
|
{ params }: { params: Promise<{ websiteId: string }> },
|
||||||
) {
|
) {
|
||||||
const schema = z.object({
|
const { auth, query, error } = await parseRequest(request);
|
||||||
timezone: timezoneParam,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { auth, query, error } = await parseRequest(request, schema);
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return error();
|
return error();
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,13 @@
|
||||||
import { useTimezone } from '@/components/hooks/useTimezone';
|
|
||||||
import { REALTIME_INTERVAL } from '@/lib/constants';
|
import { REALTIME_INTERVAL } from '@/lib/constants';
|
||||||
import { useApi } from '../useApi';
|
import { useApi } from '../useApi';
|
||||||
|
import { RealtimeData } from '@/lib/types';
|
||||||
export interface RealtimeData {
|
|
||||||
countries: Record<string, number>;
|
|
||||||
events: any[];
|
|
||||||
pageviews: any[];
|
|
||||||
referrers: Record<string, number>;
|
|
||||||
timestamp: number;
|
|
||||||
series: {
|
|
||||||
views: any[];
|
|
||||||
visitors: any[];
|
|
||||||
};
|
|
||||||
totals: {
|
|
||||||
views: number;
|
|
||||||
visitors: number;
|
|
||||||
events: number;
|
|
||||||
countries: number;
|
|
||||||
};
|
|
||||||
urls: Record<string, number>;
|
|
||||||
visitors: any[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useRealtimeQuery(websiteId: string) {
|
export function useRealtimeQuery(websiteId: string) {
|
||||||
const { get, useQuery } = useApi();
|
const { get, useQuery } = useApi();
|
||||||
const { timezone } = useTimezone();
|
|
||||||
const { data, isLoading, error } = useQuery<RealtimeData>({
|
const { data, isLoading, error } = useQuery<RealtimeData>({
|
||||||
queryKey: ['realtime', { websiteId, timezone }],
|
queryKey: ['realtime', { websiteId }],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
return get(`/realtime/${websiteId}`, { timezone });
|
return get(`/realtime/${websiteId}`);
|
||||||
},
|
},
|
||||||
enabled: !!websiteId,
|
enabled: !!websiteId,
|
||||||
refetchInterval: REALTIME_INTERVAL,
|
refetchInterval: REALTIME_INTERVAL,
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ function getDateStringSQL(data: any, unit: string = 'utc', timezone?: string) {
|
||||||
|
|
||||||
function getDateSQL(field: string, unit: string, timezone?: string) {
|
function getDateSQL(field: string, unit: string, timezone?: string) {
|
||||||
if (timezone) {
|
if (timezone) {
|
||||||
return `toDateTime(date_trunc('${unit}', ${field}, '${timezone}'), '${timezone}')`;
|
return `toDateTime(date_trunc('${unit}', ${field}, '${timezone}'))`;
|
||||||
}
|
}
|
||||||
return `toDateTime(date_trunc('${unit}', ${field}))`;
|
return `toDateTime(date_trunc('${unit}', ${field}))`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -116,3 +116,23 @@ export interface PageResult<T> {
|
||||||
sortDescending?: boolean;
|
sortDescending?: boolean;
|
||||||
search?: string;
|
search?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RealtimeData {
|
||||||
|
countries: Record<string, number>;
|
||||||
|
events: any[];
|
||||||
|
pageviews: any[];
|
||||||
|
referrers: Record<string, number>;
|
||||||
|
timestamp: number;
|
||||||
|
series: {
|
||||||
|
views: any[];
|
||||||
|
visitors: any[];
|
||||||
|
};
|
||||||
|
totals: {
|
||||||
|
views: number;
|
||||||
|
visitors: number;
|
||||||
|
events: number;
|
||||||
|
countries: number;
|
||||||
|
};
|
||||||
|
urls: Record<string, number>;
|
||||||
|
visitors: any[];
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ async function clickhouseQuery(
|
||||||
websiteId: string,
|
websiteId: string,
|
||||||
filters: QueryFilters,
|
filters: QueryFilters,
|
||||||
): Promise<{ x: string; y: number }[]> {
|
): Promise<{ x: string; y: number }[]> {
|
||||||
const { timezone = 'utc', unit = 'day' } = filters;
|
const { timezone = 'UTC', unit = 'day' } = filters;
|
||||||
const { parseFilters, rawQuery, getDateSQL } = clickhouse;
|
const { parseFilters, rawQuery, getDateSQL } = clickhouse;
|
||||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||||
...filters,
|
...filters,
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ async function clickhouseQuery(
|
||||||
websiteId: string,
|
websiteId: string,
|
||||||
filters: QueryFilters,
|
filters: QueryFilters,
|
||||||
): Promise<{ x: string; y: number }[]> {
|
): Promise<{ x: string; y: number }[]> {
|
||||||
const { timezone = 'utc', unit = 'day' } = filters;
|
const { timezone = 'UTC', unit = 'day' } = filters;
|
||||||
const { parseFilters, rawQuery, getDateSQL } = clickhouse;
|
const { parseFilters, rawQuery, getDateSQL } = clickhouse;
|
||||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||||
...filters,
|
...filters,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue