umami/lib/filters.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

import firstBy from 'thenby';
2020-08-07 04:37:19 +02:00
import { BROWSERS, ISO_COUNTRIES, DEVICES } from './constants';
import { removeTrailingSlash } from './format';
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, { domain }) => {
const isValidUrl = url => {
return url !== '' && !url.startsWith('#');
};
2020-08-01 12:34:56 +02:00
const cleanUrl = url => {
try {
const { pathname, searchParams } = new URL(url);
const path = removeTrailingSlash(pathname);
const ref = searchParams.get('ref');
const query = ref ? `?ref=${ref}` : '';
return `${path}${query}`;
} catch {
return null;
}
};
const map = data.reduce((obj, { x, y }) => {
if (!isValidUrl(x)) {
return obj;
}
const url = cleanUrl(x.startsWith('/') ? `http://${domain}${x}` : x);
if (url) {
if (!obj[url]) {
obj[url] = y;
} else {
obj[url] += y;
}
}
return obj;
}, {});
return Object.keys(map)
.map(key => ({ x: key, y: map[key] }))
.sort(firstBy('y', -1).thenBy('x'));
};
export const refFilter = (data, { domain, domainsOnly }) => {
const isValidRef = ref => {
return ref !== '' && !ref.startsWith('/') && !ref.startsWith('#');
};
const cleanUrl = url => {
try {
const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
if (hostname === domain) {
return null;
}
if (domainsOnly && hostname) {
return hostname;
}
if (!origin || origin === 'null') {
return `${protocol}${removeTrailingSlash(pathname)}`;
}
if (protocol.startsWith('http')) {
const path = removeTrailingSlash(pathname);
const ref = searchParams.get('ref');
const query = ref ? `?ref=${ref}` : '';
return `${origin}${path}${query}`;
}
return null;
} catch {
return null;
}
};
const map = data.reduce((obj, { x, y }) => {
if (!isValidRef(x)) {
return obj;
}
const url = cleanUrl(x);
if (url) {
if (!obj[url]) {
obj[url] = y;
} else {
obj[url] += y;
}
}
return obj;
}, {});
return Object.keys(map)
.map(key => ({ x: key, y: map[key] }))
.sort(firstBy('y', -1).thenBy('x'));
2020-08-07 11:46:21 +02:00
};
2020-08-01 12:34:56 +02:00
2020-08-07 04:37:19 +02:00
export const deviceFilter = data =>
data.map(({ x, ...props }) => ({ x: DEVICES[x] || x, ...props }));
2020-08-01 12:34:56 +02:00
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 }));
};