Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 39 additions & 54 deletions modules/stats.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
function group_by_value(list, key) {
return list.reduce((acc, item) => {
(acc[item[key]] = acc[item[key]] || []).push(item)
return acc
}, {})
};
}

async function get_stats_by_value (data, value) {
async function get_stats_by_value(data, value) {
const temp_array = {}
try {
const grouped = group_by_value(data, value)
Expand All @@ -17,97 +17,82 @@ async function get_stats_by_value (data, value) {
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

for (const key of Object.keys(grouped)) {
['good', 'maybe', 'bad', 'all'].forEach(item => {
if (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]
]
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)
})
})
for (const key of Object.keys(temp_array)) {
temp_array[key] = temp_array[key].map(([k, count]) => [k, ((count / temp_found[key]) * 100).toFixed(2)])
}
} catch (error) {
helper.verbose && console.log(error)
}

return temp_array
}

async function get_metadata(data, type){
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]);
}
for (const site of temp_filtered) {
if (site.metadata && site.metadata !== 'unavailable' && site.metadata.length > 0) {
for (const meta of site.metadata) {
if ('content' in meta) {
let 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){
}
} 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
return temp_array.length > 0 ? temp_array.sort((a, b) => b[0] - a[0]) : temp_array
}

async function get_stats (req, data) {
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')){

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}
return { categories, countries, metadata }
}

export default{
export default {
get_stats
}