This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstats.js
More file actions
213 lines (203 loc) · 6.8 KB
/
stats.js
File metadata and controls
213 lines (203 loc) · 6.8 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"use strict";
const {History, Stat, Map, Op} = require("../db/modelsB.js");
const { getParameterFromQuery } = require('./dbutils');
const localconfig = require('../localconfig');
const config = require('../config');
// from: require('../public/js/utils');
var httpToken = '://';
var getWikipediaLang = function (record) {
if (record.indexOf(httpToken) !== -1) {
return record.split(httpToken)[1].split('.')[0];
}
else {
return record;
}
};
var dictItems = function (Ogg) {
// ottieni valori dell'oggetto (dizionario)
var els = [];
for (var k in Ogg) {
if (Ogg.hasOwnProperty(k)) {
els.push(Ogg[k]);
}
};
return els;
};
var arrSum = function (arr) {
// somma elementi di un Array numerico
const reducer = (accumulator, currentValue) => accumulator + currentValue;
return arr.reduce(reducer);
};
var isWikipediaURL = function (record) {
// Verifica se un link NON appartiene a Commons, includento i langcode
return record.indexOf('wikipedia.org') > -1 || record.indexOf(httpToken) === -1;
};
// end from require('../public/js/utils');
// adapted from markerCounter2PinDataObj from icons.js
var quality = function (counters) {
// Se non esiste in tutte le lingue ufficiali o tot <= 2
if (!counters['wikipediaBaseLang']) {
return "qualityBlack"
}
if (counters['wikipediaBaseLang'] == 1 || (counters['wikipediaBaseLang'] == 2 && counters['commons'] == 0)) {
return "qualityRed"
}
if ((counters['wikipediaBaseLang'] == 2 && counters['commons'] == 1) || counters['wikipediaBaseLang'] == 3 || (counters['wikipediaBaseLang'] == 4 && counters['commons'] == 0)) {
return "qualityYellow"
}
if (counters['wikipediaBaseLang'] == 4 && counters['commons'] == 1) {
return "qualityGreen"
}
};
const getSetting = (key) => {
const fallback = config.stat[key];
if (localconfig.hasOwnProperty('stat') && typeof localconfig.stat[key] !== "undefined") {
return localconfig.stat[key]
}
return fallback
};
const CronJob = require('cron').CronJob;
const sequelize = require('sequelize');
const got = require('got');
const { logger } = require("./logger.js");
/**
*
* @param {Object} feature A single point on the map (pin).
* @param {Object} hist Sequelize instance of History record.
* @returns counters
*/
var featureLinkCounter = function(feature, hist) {
let languagechoices = getParameterFromQuery(hist.map.get('mapargs'), 'languagechoices', JSON.stringify(config.defaultLanguageChoices))
languagechoices = JSON.parse(languagechoices)
// conta il numero di link del museo corrente
var counters = {
'wikipediaBaseLang': 0, // 0-4
'wikipediaMoreLang': 0, // 0-N
'website': 0, // 0-1
'commons': 0 // 0-1
// 'tot': // Totale somma contatori
};
if (typeof feature.properties.website !== 'undefined') {
counters['website'] += 1;
}
var hasWikipediaArticles = true;
// Verifico che esista almeno un vero link a wikipedia nella lista
if (typeof feature.properties.lang !== 'undefined' && feature.properties.lang.length) {
if (feature.properties.lang.length == 1) {
if (!isWikipediaURL(feature.properties.lang[0])) {
hasWikipediaArticles = false;
}
}
}
else {
hasWikipediaArticles = false;
}
if (hasWikipediaArticles) {
for (let i=0; i < feature.properties.lang.length; i++) {
if (isWikipediaURL(feature.properties.lang[i])) {
// Conto le lingue aggiuntive separandole da quelle principali
var langcode = getWikipediaLang(feature.properties.lang[i]);
if (!languagechoices.includes(langcode)) {
counters['wikipediaMoreLang'] += 1;
}
else {
counters['wikipediaBaseLang'] += 1;
}
}
}
}
if (typeof feature.properties.commons !== 'undefined') {
counters['commons'] += 1;
}
counters['tot'] = arrSum(dictItems(counters));
return counters;
};
const mapStat = function (featureStats) {
const stats = {
qualityBlack: 0,
qualityRed: 0,
qualityYellow: 0,
qualityGreen: 0,
}
for (const fstats of featureStats) {
for (const field of Object.keys(stats)) {
if (fstats[field] === true) {
stats[field]++
}
}
}
stats.pins = featureStats.length
return stats
}
/**
*
* @param {Object} feature A single point on the map (pin).
* @param {Object} hist Sequelize instance of History record.
* @returns
*/
const featureStat = function (feature, hist) {
let qualityFlags = {
qualityBlack: false,
qualityRed: false,
qualityYellow: false,
qualityGreen: false,
}
let counter = featureLinkCounter(feature, hist)
let rank = quality(counter)
qualityFlags[rank] = true
qualityFlags.commons = feature.properties.commons ? true : false
qualityFlags.image = feature.properties.image ? true : false
qualityFlags.languages = feature.properties.lang.length
return qualityFlags
}
const saveStat = async function () {
let saveStatOffset = 0
const lastStat = await Stat.findOne({
limit: 1,
order: [
['id', 'DESC']
],
})
// if no stat in database, take all
const lastStatDate = lastStat ? lastStat.get('createdAt') : new Date(0)
const intervalHandler = setInterval(async () => {
logger.info(`saveStat offset ${saveStatOffset}`)
const hists = await History.findAll({
include:[{model: Map }],
where: {
error: false,
createdAt: {
[Op.gt]: lastStatDate
}
},
limit: 1,
offset: saveStatOffset++
})
const hist = hists.shift()
if (typeof hist !== "undefined") {
const data = JSON.parse(hist.json)
const features = data.data
const featuresStatArr = features.map(feature => featureStat(feature, hist))
const stats = mapStat(featuresStatArr)
stats.mapId = hist.mapId
stats.createdAt = hist.createdAt
stats.updatedAt = hist.updatedAt
logger.debug(`Saving stats for map id ${hist.mapId}`)
await Stat.upsert(stats)
}
else {
logger.debug(`Stop saving stats, no more maps to process`)
clearInterval(intervalHandler)
}
}, getSetting('interval'))
};
exports.saveStat = saveStat;
// DEBUG to launch from command line this unit
/** (async () => {
await saveStat()
})(); **/
/**
* Save periodically some data for statistics.
*/
const job = new CronJob(getSetting('time'), saveStat);
job.start();