forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.js
132 lines (102 loc) · 2.87 KB
/
filters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { BROWSERS } from './constants';
import { removeTrailingSlash, removeWWW, getDomainName } from './url';
export const urlFilter = (data, { raw }) => {
const isValidUrl = url => {
return url !== '' && !url.startsWith('#');
};
if (raw) {
return data.filter(({ x }) => isValidUrl(x));
}
const cleanUrl = url => {
try {
const { pathname, search } = new URL(url, location.origin);
if (search.startsWith('?/')) {
return `${pathname}${search}`;
}
return pathname;
} catch {
return null;
}
};
const map = data.reduce((obj, { x, y }) => {
if (!isValidUrl(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] }));
};
export const refFilter = (data, { domain, domainOnly, raw }) => {
const domainName = getDomainName(domain);
const regex = new RegExp(`http[s]?://${domainName}`);
const links = {};
const isValidRef = ref => {
return ref !== '' && !ref.startsWith('/') && !ref.startsWith('#');
};
if (raw) {
return data.filter(({ x }) => isValidRef(x) && !regex.test(x));
}
const cleanUrl = url => {
try {
const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
if (hostname === domainName) {
return null;
}
if (domainOnly && hostname) {
return removeWWW(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 removeTrailingSlash(`${removeWWW(hostname)}${path}`) + query;
}
return null;
} catch {
return null;
}
};
const map = data.reduce((obj, { x, y }) => {
if (!isValidRef(x)) {
return obj;
}
const url = cleanUrl(x);
if (!domainOnly && !raw) {
links[url] = 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], w: links[key] }));
};
export const browserFilter = data => data.map(({ x, y }) => ({ x: BROWSERS[x] ?? x, y }));
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);
});
};
export const percentFilter = data => {
const total = data.reduce((n, { y }) => n + y, 0);
return data.map(({ x, y, ...props }) => ({ x, y, z: total ? (y / total) * 100 : 0, ...props }));
};