2017-06-28 05:25:36 +02:00
|
|
|
const logger = require('winston');
|
2017-06-30 02:52:37 +02:00
|
|
|
const ua = require('universal-analytics');
|
|
|
|
const config = require('config');
|
2017-06-28 05:25:36 +02:00
|
|
|
const db = require('../models');
|
2017-06-30 02:52:37 +02:00
|
|
|
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
|
2017-06-28 05:25:36 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats (action, url, ipAddress, name, claimId, result) {
|
2017-06-30 02:52:37 +02:00
|
|
|
// make sure the result is a string
|
|
|
|
if (result && (typeof result !== 'string')) {
|
|
|
|
result = result.toString();
|
|
|
|
}
|
|
|
|
// // make sure the ip address(es) are a string
|
|
|
|
if (ipAddress && (typeof ipAddress !== 'string')) {
|
|
|
|
ipAddress = ipAddress.toString();
|
|
|
|
}
|
2017-07-13 00:30:31 +02:00
|
|
|
logger.silly(name, claimId);
|
|
|
|
db.File
|
|
|
|
.findOne({where: { name, claimId }})
|
|
|
|
.then(file => {
|
|
|
|
// create record in the db
|
|
|
|
let FileId;
|
|
|
|
if (file) {
|
|
|
|
FileId = file.dataValues.id;
|
|
|
|
} else {
|
|
|
|
FileId = null;
|
|
|
|
}
|
|
|
|
logger.silly('file id:', FileId);
|
|
|
|
return db.Request
|
|
|
|
.create({
|
|
|
|
action,
|
|
|
|
url,
|
|
|
|
ipAddress,
|
|
|
|
result,
|
|
|
|
FileId,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
|
|
|
});
|
2017-06-30 02:52:37 +02:00
|
|
|
},
|
2017-07-05 18:26:22 +02:00
|
|
|
sendGoogleAnalytics (action, headers, ip, originalUrl) {
|
2017-06-30 02:52:37 +02:00
|
|
|
const visitorId = ip.replace(/\./g, '-');
|
|
|
|
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
|
2017-07-05 18:26:22 +02:00
|
|
|
let params;
|
2017-06-30 02:52:37 +02:00
|
|
|
switch (action) {
|
|
|
|
case 'serve':
|
2017-07-05 18:26:22 +02:00
|
|
|
params = {
|
|
|
|
ec : 'serve',
|
|
|
|
ea : originalUrl,
|
|
|
|
uip: ip,
|
|
|
|
ua : headers['user-agent'],
|
|
|
|
ul : headers['accept-language'],
|
|
|
|
};
|
2017-06-30 02:52:37 +02:00
|
|
|
break;
|
|
|
|
case 'publish':
|
2017-07-05 18:26:22 +02:00
|
|
|
params = {
|
|
|
|
ec : 'publish',
|
|
|
|
ea : originalUrl,
|
|
|
|
uip: ip,
|
|
|
|
ua : headers['user-agent'],
|
|
|
|
ul : headers['accept-language'],
|
|
|
|
};
|
2017-06-30 02:52:37 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2017-07-05 18:26:22 +02:00
|
|
|
visitor.event(params, (err) => {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Google Analytics Event Error >>', err);
|
|
|
|
}
|
|
|
|
});
|
2017-06-30 02:52:37 +02:00
|
|
|
},
|
2017-07-11 02:51:29 +02:00
|
|
|
getStatsSummary (startDate) {
|
2017-07-13 00:30:31 +02:00
|
|
|
logger.debug('retrieving request records');
|
2017-06-28 05:25:36 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-07-13 00:30:31 +02:00
|
|
|
// get the raw Requests data
|
|
|
|
db.Request
|
2017-07-11 02:51:29 +02:00
|
|
|
.findAll({
|
|
|
|
where: {
|
|
|
|
createdAt: {
|
|
|
|
gt: startDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2017-06-28 05:25:36 +02:00
|
|
|
.then(data => {
|
2017-07-12 01:55:03 +02:00
|
|
|
let resultHashTable = {};
|
2017-06-28 08:11:51 +02:00
|
|
|
let totalServe = 0;
|
2017-06-28 05:25:36 +02:00
|
|
|
let totalPublish = 0;
|
|
|
|
let totalShow = 0;
|
|
|
|
let totalCount = 0;
|
|
|
|
let totalSuccess = 0;
|
|
|
|
let totalFailure = 0;
|
2017-07-12 01:55:03 +02:00
|
|
|
let percentSuccess;
|
2017-06-28 05:25:36 +02:00
|
|
|
// sumarise the data
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
let key = data[i].action + data[i].url;
|
|
|
|
totalCount += 1;
|
|
|
|
switch (data[i].action) {
|
2017-06-28 08:15:43 +02:00
|
|
|
case 'serve':
|
2017-06-28 08:11:51 +02:00
|
|
|
totalServe += 1;
|
2017-06-28 05:25:36 +02:00
|
|
|
break;
|
|
|
|
case 'publish':
|
|
|
|
totalPublish += 1;
|
|
|
|
break;
|
|
|
|
case 'show':
|
|
|
|
totalShow += 1;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (resultHashTable[key]) {
|
|
|
|
resultHashTable[key]['count'] += 1;
|
|
|
|
if (data[i].result === 'success') {
|
|
|
|
resultHashTable[key]['success'] += 1;
|
|
|
|
totalSuccess += 1;
|
|
|
|
} else {
|
|
|
|
resultHashTable[key]['failure'] += 1;
|
|
|
|
totalFailure += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resultHashTable[key] = {
|
|
|
|
action : data[i].action,
|
|
|
|
url : data[i].url,
|
|
|
|
count : 1,
|
|
|
|
success: 0,
|
|
|
|
failure: 0,
|
|
|
|
};
|
|
|
|
if (data[i].result === 'success') {
|
|
|
|
resultHashTable[key]['success'] += 1;
|
|
|
|
totalSuccess += 1;
|
|
|
|
} else {
|
|
|
|
resultHashTable[key]['failure'] += 1;
|
|
|
|
totalFailure += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 01:55:03 +02:00
|
|
|
percentSuccess = Math.round(totalSuccess / totalCount * 100);
|
2017-06-28 05:25:36 +02:00
|
|
|
// return results
|
2017-06-28 08:11:51 +02:00
|
|
|
resolve({ records: resultHashTable, totals: { totalServe, totalPublish, totalShow, totalCount, totalSuccess, totalFailure }, percentSuccess });
|
2017-06-28 05:25:36 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
},
|
2017-07-11 02:51:29 +02:00
|
|
|
getTrendingClaims (startDate) {
|
2017-07-13 00:30:31 +02:00
|
|
|
logger.debug('retrieving trending requests');
|
2017-07-11 02:51:29 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-07-13 00:30:31 +02:00
|
|
|
// get the raw requests data
|
|
|
|
db.Request
|
2017-07-11 02:51:29 +02:00
|
|
|
.findAll({
|
|
|
|
where: {
|
|
|
|
createdAt: {
|
|
|
|
gt: startDate,
|
|
|
|
},
|
2017-07-13 01:57:12 +02:00
|
|
|
FileId: {
|
|
|
|
not: null,
|
|
|
|
},
|
2017-07-11 02:51:29 +02:00
|
|
|
},
|
2017-07-13 00:30:31 +02:00
|
|
|
include: [db.File],
|
2017-07-11 02:51:29 +02:00
|
|
|
})
|
|
|
|
.then(data => {
|
2017-07-13 01:57:12 +02:00
|
|
|
if (data) {
|
|
|
|
logger.debug(data[0].File.name);
|
|
|
|
}
|
2017-07-13 00:30:31 +02:00
|
|
|
// let resultHashTable = {};
|
|
|
|
// let sortableArray = [];
|
|
|
|
// let sortedArray;
|
|
|
|
// // summarise the data
|
|
|
|
// for (let i = 0; i < data.length; i++) {
|
|
|
|
// let key = data[i].fileId;
|
|
|
|
// if (resultHashTable[key] === undefined) {
|
|
|
|
// resultHashTable[key] = {
|
|
|
|
// count : 0,
|
|
|
|
// details: {
|
|
|
|
// name : data[i].name,
|
|
|
|
// claimId : data[i].claimId,
|
|
|
|
// fileName: data[i].fileName,
|
|
|
|
// fileType: data[i].fileType,
|
|
|
|
// nsfw : data[i].nsfw,
|
|
|
|
// },
|
|
|
|
// };
|
|
|
|
// } else {
|
|
|
|
// resultHashTable[key]['count'] += 1;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// for (let objKey in resultHashTable) {
|
|
|
|
// if (resultHashTable.hasOwnProperty(objKey)) {
|
|
|
|
// sortableArray.push([
|
|
|
|
// resultHashTable[objKey]['count'],
|
|
|
|
// resultHashTable[objKey]['details'],
|
|
|
|
// ]);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// sortableArray.sort((a, b) => {
|
|
|
|
// return b[0] - a[0];
|
|
|
|
// });
|
|
|
|
// sortedArray = sortableArray.map((a) => {
|
|
|
|
// return a[1];
|
|
|
|
// });
|
|
|
|
// // return results
|
|
|
|
resolve();
|
2017-07-11 02:51:29 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
},
|
2017-06-28 05:25:36 +02:00
|
|
|
};
|