umami/src/lib/filters.ts
Dan Kotov cd1ee8461a fix: calculate country visitors share relative to total
as opposed to relative to the top-10 countries shown in the breakdown
2026-01-11 15:53:57 -05:00

31 lines
868 B
TypeScript

export const percentFilter = (data: any[], total?: number) => {
if (!Array.isArray(data)) return [];
const sum = total ?? data.reduce((n, { y }) => n + y, 0);
return data.map(({ x, y, ...props }) => ({ x, y, z: sum ? (y / sum) * 100 : 0, ...props }));
};
export const paramFilter = (data: any[]) => {
const map = data.reduce((obj, { x, y }) => {
try {
const searchParams = new URLSearchParams(x);
for (const [key, value] of searchParams) {
if (!obj[key]) {
obj[key] = { [value]: y };
} else if (!obj[key][value]) {
obj[key][value] = y;
} else {
obj[key][value] += y;
}
}
} catch {
// Ignore
}
return obj;
}, {});
return Object.keys(map).flatMap(key =>
Object.keys(map[key]).map(n => ({ x: `${key}=${n}`, p: key, v: n, y: map[key][n] })),
);
};