spee.ch/server/utils/processTrending.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-10-24 10:43:30 +02:00
const db = require('server/models');
const {
getInformationFromValues,
getZScore,
getFastPValue,
getWeight,
} = require('server/models/utils/trendingAnalysis');
const logger = require('winston');
2018-10-24 10:43:30 +02:00
module.exports = async () => {
try {
const claims = await db.Trending.getTrendingClaims();
const claimViews = await db.Views.getUniqueViews();
2018-10-24 10:43:30 +02:00
2018-11-11 01:11:12 +01:00
if (claimViews.length <= 1) {
return;
}
2018-10-24 10:43:30 +02:00
const time = Date.now();
2018-10-24 10:43:30 +02:00
// Must create statistical analytics before we can process zScores, etc
const viewsNumArray = claimViews.map((claimViewsEntry) => claimViewsEntry.views);
2018-10-24 10:43:30 +02:00
const {
mean,
standardDeviation,
} = getInformationFromValues(viewsNumArray);
2018-11-11 01:11:12 +01:00
for (let i = 0; i < claimViews.length; i++) {
let claimViewsEntry = claimViews[i];
const {
isChannel,
claimId,
publisherId,
} = claimViewsEntry;
const zScore = getZScore(claimViewsEntry.views, mean, standardDeviation);
const pValue = getFastPValue(zScore);
const weight = getWeight(zScore, pValue);
const trendingData = {
time,
2018-11-11 01:11:12 +01:00
isChannel : claimViewsEntry.isChannel,
claimId : claimViewsEntry.claimId,
publisherId : claimViewsEntry.publisherId,
intervalViews: claimViewsEntry.views,
weight,
zScore,
pValue,
};
db.Trending.create(trendingData);
}
2018-11-11 01:11:12 +01:00
} catch (e) {
logger.error('Error processing trending content:', e);
2018-10-24 10:43:30 +02:00
}
2018-11-11 01:11:12 +01:00
};