umami/queries/analytics/stats/getRealtimeData.ts

28 lines
834 B
TypeScript
Raw Normal View History

import { md5 } from 'next-basics';
import { getSessions, getEvents } from 'queries';
2023-04-07 01:31:16 +00:00
import { EVENT_TYPE } from 'lib/constants';
2022-07-12 21:14:36 +00:00
2023-02-15 10:27:18 +00:00
export async function getRealtimeData(websiteId, time) {
2022-07-12 21:14:36 +00:00
const [pageviews, sessions, events] = await Promise.all([
2023-04-07 01:31:16 +00:00
getEvents(websiteId, time, EVENT_TYPE.pageView),
2023-02-15 10:27:18 +00:00
getSessions(websiteId, time),
2023-04-07 01:31:16 +00:00
getEvents(websiteId, time, EVENT_TYPE.customEvent),
2022-07-12 21:14:36 +00:00
]);
2023-02-18 05:42:42 +00:00
const decorate = (id, data) => {
return data.map(props => ({
2022-07-12 21:14:36 +00:00
...props,
2023-02-18 05:42:42 +00:00
__id: md5(id, ...Object.values(props)),
2023-02-23 04:59:59 +00:00
__type: id,
timestamp: props.timestamp ? props.timestamp * 1000 : new Date(props.createdAt).getTime(),
2023-02-18 05:42:42 +00:00
}));
};
return {
pageviews: decorate('pageviews', pageviews),
2023-02-23 04:59:59 +00:00
sessions: decorate('session', sessions),
events: decorate('event', events),
2022-07-12 21:14:36 +00:00
timestamp: Date.now(),
};
}