2023-08-08 22:29:59 +00:00
|
|
|
import { getSessions, getEvents } from 'queries/index';
|
2023-04-07 01:31:16 +00:00
|
|
|
import { EVENT_TYPE } from 'lib/constants';
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2023-12-01 05:58:11 +00:00
|
|
|
export async function getRealtimeData(websiteId: string, startDate: Date) {
|
2022-07-12 21:14:36 +00:00
|
|
|
const [pageviews, sessions, events] = await Promise.all([
|
2023-12-01 05:58:11 +00:00
|
|
|
getEvents(websiteId, startDate, EVENT_TYPE.pageView),
|
|
|
|
|
getSessions(websiteId, startDate),
|
|
|
|
|
getEvents(websiteId, startDate, EVENT_TYPE.customEvent),
|
2022-07-12 21:14:36 +00:00
|
|
|
]);
|
|
|
|
|
|
2023-12-10 04:55:50 +00:00
|
|
|
const decorate = (type: string, data: any[]) => {
|
|
|
|
|
return data.map((values: { [key: string]: any }) => ({
|
|
|
|
|
...values,
|
|
|
|
|
__type: type,
|
|
|
|
|
timestamp: values.timestamp ? values.timestamp * 1000 : new Date(values.createdAt).getTime(),
|
2023-02-18 05:42:42 +00:00
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-10 04:55:50 +00:00
|
|
|
const set = new Set();
|
|
|
|
|
const uniques = (type: string, data: any[]) => {
|
|
|
|
|
return data.reduce((arr, values: { [key: string]: any }) => {
|
|
|
|
|
if (!set.has(values.id)) {
|
|
|
|
|
set.add(values.id);
|
|
|
|
|
|
|
|
|
|
return arr.concat({
|
|
|
|
|
...values,
|
|
|
|
|
__type: type,
|
|
|
|
|
timestamp: values.timestamp
|
|
|
|
|
? values.timestamp * 1000
|
|
|
|
|
: new Date(values.createdAt).getTime(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}, []);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-18 05:42:42 +00:00
|
|
|
return {
|
2023-07-29 05:40:57 +00:00
|
|
|
pageviews: decorate('pageview', pageviews),
|
2023-12-10 04:55:50 +00:00
|
|
|
sessions: uniques('session', sessions),
|
2023-02-23 04:59:59 +00:00
|
|
|
events: decorate('event', events),
|
2022-07-12 21:14:36 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
}
|