-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
113 lines (103 loc) · 3.06 KB
/
stats.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
import helper from './helper.js'
function group_by_value (list, key) {
return list.reduce(function (x, y) {
(x[y[key]] = x[y[key]] || []).push(y)
return x
}, {})
};
async function get_stats_by_value (data, value) {
const temp_array = {}
try {
const grouped = group_by_value(data, value)
const temp_found = {
good: 0,
maybe: 0,
bad: 0,
all: 0
}
await Object.keys(grouped).forEach(async function (key) {
await ['good', 'maybe', 'bad', 'all'].forEach(async function (item, index) {
if (Object.keys(grouped[key]).length > 0) {
const len = grouped[key].filter((_item) => _item.status === item).length
if (len > 0) {
temp_found[item] += len
if (item in temp_array) {
temp_array[item].push([key, len])
} else {
temp_array[item] = [
[key, len]
]
}
}
}
})
})
await Object.keys(temp_array).forEach(async function (key) {
await temp_array[key].forEach(async function (item, index) {
temp_array[key][index][1] = ((temp_array[key][index][1] / temp_found[key]) * 100).toFixed(2)
})
})
} catch (error) {
helper.verbose && console.log(error)
}
return temp_array
}
async function get_metadata(data, type){
const temp_array = []
try {
const temp_filtered = data.filter(item => item.status === type)
await temp_filtered.forEach(site => {
if ('metadata' in site) {
if (site.metadata !== 'unavailable' && site.metadata.length > 0) {
site.metadata.forEach(meta => {
if ('content' in meta) {
let temp_content = ''
if ('name' in meta) {
temp_content = meta.content
} else if ('itemprop' in meta) {
temp_content = meta.content
} else if ('property' in meta) {
temp_content = meta.content
}
if (temp_content != ''){
const entry = temp_array.find( x => x[1] === temp_content);
if (entry) {
++entry[0];
} else {
temp_array.push([1,temp_content]);
}
}
}
})
}
}
})
}
catch(error){
helper.verbose && console.log(error)
}
if (temp_array.length >0) {
temp_array.sort(function(a,b) {return b[0]-a[0]})
}
return temp_array
}
async function get_stats (req, data) {
let categories = {}
let countries = {}
let metadata = []
try {
if (req.body.option.includes('CategoriesStats')) {
categories = await get_stats_by_value(data, 'type')
countries = await get_stats_by_value(data, 'country')
}
if (req.body.option.includes('ExtractMetadata') && req.body.option.includes('MetadataStats')){
metadata = await get_metadata(data, 'good')
}
} catch (error) {
helper.verbose && console.log(error)
}
return { categories: categories, countries: countries, metadata: metadata}
}
export default{
get_stats
}