umami/queries/analytics/stats/getRealtimeData.ts

29 lines
845 B
TypeScript
Raw Normal View History

2023-02-18 06:42:42 +01:00
import { md5 } from 'lib/crypto';
2022-07-12 23:14:36 +02:00
import { getPageviews } from '../pageview/getPageviews';
import { getSessions } from '../session/getSessions';
import { getEvents } from '../event/getEvents';
2023-02-15 11:27:18 +01:00
export async function getRealtimeData(websiteId, time) {
2022-07-12 23:14:36 +02:00
const [pageviews, sessions, events] = await Promise.all([
2023-02-15 11:27:18 +01:00
getPageviews(websiteId, time),
getSessions(websiteId, time),
getEvents(websiteId, time),
2022-07-12 23:14:36 +02:00
]);
2023-02-18 06:42:42 +01:00
const decorate = (id, data) => {
return data.map(props => ({
2022-07-12 23:14:36 +02:00
...props,
2023-02-18 06:42:42 +01:00
__id: md5(id, ...Object.values(props)),
timestamp: props.timestamp * 1000,
timestampCompare: new Date(props.createdAt).getTime(),
}));
};
return {
pageviews: decorate('pageviews', pageviews),
sessions: decorate('sessions', sessions),
events: decorate('events', events),
2022-07-12 23:14:36 +02:00
timestamp: Date.now(),
};
}