mirror of
https://github.com/umami-software/umami.git
synced 2026-02-14 01:25:37 +01:00
Refactored realtime.
This commit is contained in:
parent
cda3ba345b
commit
5108b91f80
16 changed files with 205 additions and 227 deletions
|
|
@ -1,29 +1,10 @@
|
|||
import { useMemo, useRef } from 'react';
|
||||
import { format, startOfMinute, subMinutes, isBefore } from 'date-fns';
|
||||
import { startOfMinute, subMinutes, isBefore } from 'date-fns';
|
||||
import PageviewsChart from './PageviewsChart';
|
||||
import { getDateArray } from 'lib/date';
|
||||
import { DEFAULT_ANIMATION_DURATION, REALTIME_RANGE } from 'lib/constants';
|
||||
import { RealtimeData } from 'lib/types';
|
||||
|
||||
function mapData(data: any[]) {
|
||||
let last = 0;
|
||||
const arr = [];
|
||||
|
||||
data?.reduce((obj, { timestamp }) => {
|
||||
const t = startOfMinute(new Date(timestamp));
|
||||
if (t.getTime() > last) {
|
||||
obj = { x: format(t, 'yyyy-LL-dd HH:mm:00'), y: 1 };
|
||||
arr.push(obj);
|
||||
last = t.getTime();
|
||||
} else {
|
||||
obj.y += 1;
|
||||
}
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
export interface RealtimeChartProps {
|
||||
data: RealtimeData;
|
||||
unit: string;
|
||||
|
|
@ -37,12 +18,12 @@ export function RealtimeChart({ data, unit, ...props }: RealtimeChartProps) {
|
|||
|
||||
const chartData = useMemo(() => {
|
||||
if (!data) {
|
||||
return { pageviews: [], sessions: [] };
|
||||
return { views: [], visitors: [] };
|
||||
}
|
||||
|
||||
return {
|
||||
pageviews: getDateArray(mapData(data.pageviews), startDate, endDate, unit),
|
||||
sessions: getDateArray(mapData(data.visitors), startDate, endDate, unit),
|
||||
views: getDateArray(data.series.views, startDate, endDate, unit),
|
||||
visitors: getDateArray(data.series.visitors, startDate, endDate, unit),
|
||||
};
|
||||
}, [data, startDate, endDate, unit]);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue