umami/lib/filters.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-07 04:14:44 +02:00
import { BROWSERS, ISO_COUNTRIES } from './constants';
2020-08-01 12:34:56 +02:00
export const browserFilter = data =>
2020-08-07 04:14:44 +02:00
data.map(({ x, ...props }) => ({ x: BROWSERS[x] || x, ...props }));
2020-08-01 12:34:56 +02:00
export const urlFilter = data => data.filter(({ x }) => x !== '' && !x.startsWith('#'));
export const refFilter = data =>
data.filter(({ x }) => x !== '' && !x.startsWith('/') && !x.startsWith('#'));
export const deviceFilter = data => {
2020-08-05 07:45:05 +02:00
if (data.length === 0) return [];
2020-08-01 12:34:56 +02:00
const devices = data.reduce(
(obj, { x, y }) => {
const [width] = x.split('x');
if (width >= 1920) {
obj.Desktop += +y;
} else if (width >= 1024) {
obj.Laptop += +y;
} else if (width >= 767) {
obj.Tablet += +y;
} else {
obj.Mobile += +y;
}
return obj;
},
{ Desktop: 0, Laptop: 0, Tablet: 0, Mobile: 0 },
);
return percentFilter(Object.keys(devices).map(key => ({ x: key, y: devices[key] })));
};
export const countryFilter = data =>
2020-08-07 04:14:44 +02:00
data.map(({ x, ...props }) => ({ x: ISO_COUNTRIES[x] || x, ...props }));
2020-08-01 12:34:56 +02:00
export const percentFilter = data => {
const total = data.reduce((n, { y }) => n + y, 0);
return data.map(({ x, y }) => ({ x, y, z: total ? (y / total) * 100 : 0 }));
};