| 1 | import helper from './helper.js' |
| 2 | |
| 3 | function group_by_value (list, key) { |
| 4 | return list.reduce(function (x, y) { |
| 5 | (x[y[key]] = x[y[key]] || []).push(y) |
| 6 | return x |
| 7 | }, {}) |
| 8 | }; |
| 9 | |
| 10 | async function get_stats_by_value (data, value) { |
| 11 | const temp_array = {} |
| 12 | try { |
| 13 | const grouped = group_by_value(data, value) |
| 14 | const temp_found = { |
| 15 | good: 0, |
| 16 | maybe: 0, |
| 17 | bad: 0, |
| 18 | all: 0 |
| 19 | } |
| 20 | await Object.keys(grouped).forEach(async function (key) { |
| 21 | await ['good', 'maybe', 'bad', 'all'].forEach(async function (item, index) { |
| 22 | if (Object.keys(grouped[key]).length > 0) { |
| 23 | const len = grouped[key].filter((_item) => _item.status === item).length |
| 24 | if (len > 0) { |
| 25 | temp_found[item] += len |
| 26 | if (item in temp_array) { |
| 27 | temp_array[item].push([key, len]) |
| 28 | } else { |
| 29 | temp_array[item] = [ |
| 30 | [key, len] |
| 31 | ] |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | }) |
| 36 | }) |
| 37 | |
| 38 | await Object.keys(temp_array).forEach(async function (key) { |
| 39 | await temp_array[key].forEach(async function (item, index) { |
| 40 | temp_array[key][index][1] = ((temp_array[key][index][1] / temp_found[key]) * 100).toFixed(2) |
| 41 | }) |
| 42 | }) |
| 43 | } catch (error) { |
| 44 | helper.verbose && console.log(error) |
| 45 | } |
| 46 | return temp_array |
| 47 | } |
| 48 | |
| 49 | async function get_metadata(data, type){ |
| 50 | const temp_array = [] |
| 51 | try { |
| 52 | const temp_filtered = data.filter(item => item.status === type) |
| 53 | await temp_filtered.forEach(site => { |
| 54 | if ('metadata' in site) { |
| 55 | if (site.metadata !== 'unavailable' && site.metadata.length > 0) { |
| 56 | site.metadata.forEach(meta => { |
| 57 | if ('content' in meta) { |
| 58 | let temp_content = '' |
| 59 | if ('name' in meta) { |
| 60 | temp_content = meta.content |
| 61 | } else if ('itemprop' in meta) { |
| 62 | temp_content = meta.content |
| 63 | } else if ('property' in meta) { |
| 64 | temp_content = meta.content |
| 65 | } |
| 66 | |
| 67 | if (temp_content != ''){ |
| 68 | const entry = temp_array.find( x => x[1] === temp_content); |
| 69 | if (entry) { |
| 70 | ++entry[0]; |
| 71 | } else { |
| 72 | temp_array.push([1,temp_content]); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | }) |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | catch(error){ |
| 82 | helper.verbose && console.log(error) |
| 83 | } |
| 84 | |
| 85 | if (temp_array.length >0) { |
| 86 | temp_array.sort(function(a,b) {return b[0]-a[0]}) |
| 87 | } |
| 88 | |
| 89 | return temp_array |
| 90 | } |
| 91 | |
| 92 | async function get_stats (req, data) { |
| 93 | let categories = {} |
| 94 | let countries = {} |
| 95 | let metadata = [] |
| 96 | try { |
| 97 | if (req.body.option.includes('CategoriesStats')) { |
| 98 | categories = await get_stats_by_value(data, 'type') |
| 99 | countries = await get_stats_by_value(data, 'country') |
| 100 | } |
| 101 | if (req.body.option.includes('ExtractMetadata') && req.body.option.includes('MetadataStats')){ |
| 102 | metadata = await get_metadata(data, 'good') |
| 103 | } |
| 104 | } catch (error) { |
| 105 | helper.verbose && console.log(error) |
| 106 | } |
| 107 | |
| 108 | return { categories: categories, countries: countries, metadata: metadata} |
| 109 | } |
| 110 | |
| 111 | export default{ |
| 112 | get_stats |
| 113 | } |
| 114 |