umami/queries/analytics/stats/getRealtimeData.ts

29 lines
884 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 { getSessions } from '../session/getSessions';
import { getEvents } from '../event/getEvents';
2023-04-07 03:31:16 +02:00
import { EVENT_TYPE } from 'lib/constants';
2022-07-12 23:14:36 +02:00
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-04-07 03:31:16 +02:00
getEvents(websiteId, time, EVENT_TYPE.pageView),
2023-02-15 11:27:18 +01:00
getSessions(websiteId, time),
2023-04-07 03:31:16 +02:00
getEvents(websiteId, time, EVENT_TYPE.customEvent),
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)),
2023-02-23 05:59:59 +01:00
__type: id,
timestamp: props.timestamp ? props.timestamp * 1000 : new Date(props.createdAt).getTime(),
2023-02-18 06:42:42 +01:00
}));
};
return {
2023-02-23 05:59:59 +01:00
pageviews: decorate('pageview', pageviews),
sessions: decorate('session', sessions),
events: decorate('event', events),
2022-07-12 23:14:36 +02:00
timestamp: Date.now(),
};
}