2022-07-21 10:11:10 +02:00
|
|
|
import { removeWWW } from './url';
|
2020-08-01 12:34:56 +02:00
|
|
|
|
2022-07-21 10:11:10 +02:00
|
|
|
export const urlFilter = data => {
|
2020-08-23 04:05:07 +02:00
|
|
|
const isValidUrl = url => {
|
2021-05-06 10:47:23 +02:00
|
|
|
return url !== '' && url !== null && !url.startsWith('#');
|
2020-08-23 04:05:07 +02:00
|
|
|
};
|
2020-08-01 12:34:56 +02:00
|
|
|
|
2020-08-23 04:05:07 +02:00
|
|
|
const cleanUrl = url => {
|
|
|
|
try {
|
2020-09-26 07:31:18 +02:00
|
|
|
const { pathname, search } = new URL(url, location.origin);
|
2020-09-03 00:13:45 +02:00
|
|
|
|
|
|
|
if (search.startsWith('?/')) {
|
|
|
|
return `${pathname}${search}`;
|
|
|
|
}
|
2020-08-23 04:05:07 +02:00
|
|
|
|
2021-04-19 01:43:09 +02:00
|
|
|
return pathname;
|
2020-08-23 04:05:07 +02:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const map = data.reduce((obj, { x, y }) => {
|
|
|
|
if (!isValidUrl(x)) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2020-09-26 07:31:18 +02:00
|
|
|
const url = cleanUrl(x);
|
2020-08-23 04:05:07 +02:00
|
|
|
|
|
|
|
if (url) {
|
|
|
|
if (!obj[url]) {
|
|
|
|
obj[url] = y;
|
|
|
|
} else {
|
|
|
|
obj[url] += y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
2020-10-22 01:14:51 +02:00
|
|
|
return Object.keys(map).map(key => ({ x: key, y: map[key] }));
|
2020-08-23 04:05:07 +02:00
|
|
|
};
|
|
|
|
|
2022-07-21 10:11:10 +02:00
|
|
|
export const refFilter = data => {
|
2020-09-16 06:25:51 +02:00
|
|
|
const links = {};
|
2020-08-28 08:45:37 +02:00
|
|
|
|
2022-07-21 10:11:10 +02:00
|
|
|
const map = data.reduce((obj, { x, y }) => {
|
|
|
|
let id;
|
2020-08-23 04:05:07 +02:00
|
|
|
|
|
|
|
try {
|
2022-07-21 10:11:10 +02:00
|
|
|
const url = new URL(x);
|
2020-08-23 04:05:07 +02:00
|
|
|
|
2022-07-21 10:11:10 +02:00
|
|
|
id = removeWWW(url.hostname) || url.href;
|
2020-08-23 04:05:07 +02:00
|
|
|
} catch {
|
2022-07-21 10:11:10 +02:00
|
|
|
id = '';
|
2020-08-23 04:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 10:11:10 +02:00
|
|
|
links[id] = x;
|
2020-09-16 06:25:51 +02:00
|
|
|
|
2022-07-21 10:11:10 +02:00
|
|
|
if (!obj[id]) {
|
|
|
|
obj[id] = y;
|
|
|
|
} else {
|
|
|
|
obj[id] += y;
|
2020-08-23 04:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
2020-10-22 01:14:51 +02:00
|
|
|
return Object.keys(map).map(key => ({ x: key, y: map[key], w: links[key] }));
|
2020-08-07 11:46:21 +02:00
|
|
|
};
|
2020-08-01 12:34:56 +02:00
|
|
|
|
2021-02-15 13:02:12 +01:00
|
|
|
export const eventTypeFilter = (data, types) => {
|
|
|
|
if (!types || types.length === 0) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.filter(({ x }) => {
|
|
|
|
const [event] = x.split('\t');
|
|
|
|
return types.some(type => type === event);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-01 12:34:56 +02:00
|
|
|
export const percentFilter = data => {
|
|
|
|
const total = data.reduce((n, { y }) => n + y, 0);
|
2020-08-25 08:49:14 +02:00
|
|
|
return data.map(({ x, y, ...props }) => ({ x, y, z: total ? (y / total) * 100 : 0, ...props }));
|
2020-08-01 12:34:56 +02:00
|
|
|
};
|