spee.ch/controllers/statsController.js

184 lines
5.5 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const ua = require('universal-analytics');
const config = require('config');
const db = require('../models');
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
module.exports = {
2017-07-13 00:30:31 +02:00
postToStats (action, url, ipAddress, name, claimId, result) {
logger.debug('action:', action);
// make sure the result is a string
if (result && (typeof result !== 'string')) {
result = result.toString();
}
2017-07-20 00:42:56 +02:00
// 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
db.File
.findOne({where: { name, claimId }})
.then(file => {
// create record in the db
let FileId;
if (file) {
FileId = file.dataValues.id;
} else {
FileId = null;
}
return db.Request
.create({
action,
url,
ipAddress,
result,
FileId,
});
})
.catch(error => {
2017-09-14 00:59:29 +02:00
logger.error('Sequelize error >>', error);
2017-07-13 00:30:31 +02:00
});
},
2017-07-05 18:26:22 +02:00
sendGoogleAnalytics (action, headers, ip, originalUrl) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
2017-07-05 18:26:22 +02:00
let params;
switch (action) {
2017-08-04 06:59:22 +02:00
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'],
};
break;
2017-08-04 06:59:22 +02:00
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'],
};
break;
default: break;
}
2017-07-05 18:26:22 +02:00
visitor.event(params, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
},
getStatsSummary (startDate) {
2017-07-13 00:30:31 +02:00
logger.debug('retrieving request records');
return new Promise((resolve, reject) => {
2017-07-13 00:30:31 +02:00
// get the raw Requests data
db.Request
.findAll({
where: {
createdAt: {
gt: startDate,
},
},
})
.then(data => {
let resultHashTable = {};
2017-06-28 08:11:51 +02:00
let totalServe = 0;
let totalPublish = 0;
let totalShow = 0;
let totalCount = 0;
let totalSuccess = 0;
let totalFailure = 0;
let percentSuccess;
2017-09-07 20:58:52 +02:00
// summarise 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-09-07 20:58:52 +02:00
case 'SERVE':
2017-06-28 08:11:51 +02:00
totalServe += 1;
break;
2017-09-07 20:58:52 +02:00
case 'PUBLISH':
totalPublish += 1;
break;
2017-09-07 20:58:52 +02:00
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;
}
}
}
percentSuccess = Math.round(totalSuccess / totalCount * 100);
// return results
2017-06-28 08:11:51 +02:00
resolve({ records: resultHashTable, totals: { totalServe, totalPublish, totalShow, totalCount, totalSuccess, totalFailure }, percentSuccess });
})
.catch(error => {
2017-09-14 00:59:29 +02:00
logger.error('sequelize error >>', error);
reject(error);
});
});
},
getTrendingClaims (startDate) {
2017-07-13 00:30:31 +02:00
logger.debug('retrieving trending requests');
return new Promise((resolve, reject) => {
2017-07-13 00:30:31 +02:00
// get the raw requests data
db.getTrendingClaims(startDate)
.then(results => {
if (results) {
results.forEach(element => {
const fileExtenstion = element.fileType.substring(element.fileType.lastIndexOf('/') + 1);
element['showUrlLong'] = `/${element.claimId}/${element.name}`;
element['directUrlLong'] = `/${element.claimId}/${element.name}.${fileExtenstion}`;
element['directUrlShort'] = `/${element.claimId}/${element.name}.${fileExtenstion}`;
element['contentType'] = element.fileType;
2017-10-06 00:14:50 +02:00
element['thumbnail'] = 'https://spee.ch/assets/img/video_thumb_default.png';
});
}
resolve(results);
})
.catch(error => {
2017-09-14 00:59:29 +02:00
logger.error('sequelize error >>', error);
reject(error);
});
});
},
2017-09-07 21:24:40 +02:00
getRecentClaims () {
2017-08-25 01:09:04 +02:00
logger.debug('retrieving most recent claims');
return new Promise((resolve, reject) => {
// get the raw requests data
2017-09-07 21:24:40 +02:00
db.getRecentClaims()
2017-08-25 01:09:04 +02:00
.then(results => {
resolve(results);
})
.catch(error => {
logger.error('sequelize error', error);
reject(error);
});
});
},
};