Refactored realtime API. Add dot component and colored dots in log.

This commit is contained in:
Mike Cao 2020-10-10 11:04:07 -07:00
parent f2cfab5078
commit b72a4c001c
12 changed files with 153 additions and 86 deletions

View file

@ -62,3 +62,19 @@ export function formatLongNumber(value) {
return formatNumber(n);
}
export function stringToColor(str) {
if (!str) {
return '#ffffff';
}
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let color = '#';
for (let i = 0; i < 3; i++) {
let value = (hash >> (i * 8)) & 0xff;
color += ('00' + value.toString(16)).substr(-2);
}
return color;
}

View file

@ -500,3 +500,30 @@ export function getEventMetrics(
params,
);
}
export async function getRealtimeData(websites, time) {
const [pageviews, sessions, events] = await Promise.all([
getPageviews(websites, time),
getSessions(websites, time),
getEvents(websites, time),
]);
return {
pageviews: pageviews.map(({ view_id, ...props }) => ({
__id: `p${view_id}`,
view_id,
...props,
})),
sessions: sessions.map(({ session_id, ...props }) => ({
__id: `s${session_id}`,
session_id,
...props,
})),
events: events.map(({ event_id, ...props }) => ({
__id: `e${event_id}`,
event_id,
...props,
})),
timestamp: Date.now(),
};
}