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');
|
2017-11-07 00:18:45 +01:00
|
|
|
const config = require('../config/speechConfig.js');
|
2017-06-28 05:25:36 +02:00
|
|
|
const db = require('../models');
|
2017-11-07 00:18:45 +01:00
|
|
|
const googleApiKey = config.analytics.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-09-07 20:46:06 +02:00
|
|
|
logger.debug('action:', action);
|
2017-06-30 02:52:37 +02:00
|
|
|
// 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
|
2017-06-30 02:52:37 +02:00
|
|
|
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-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) {
|
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'],
|
|
|
|
};
|
2017-06-30 02:52:37 +02:00
|
|
|
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'],
|
|
|
|
};
|
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
|
|
|
getTrendingClaims (startDate) {
|
2017-12-07 19:45:19 +01:00
|
|
|
logger.debug('retrieving trending');
|
2017-07-30 20:24:12 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-07-13 00:30:31 +02:00
|
|
|
// get the raw requests data
|
2017-12-07 19:45:19 +01:00
|
|
|
db.getTrendingFiles(startDate)
|
|
|
|
.then(fileArray => {
|
|
|
|
let claimsPromiseArray = [];
|
|
|
|
if (fileArray) {
|
|
|
|
fileArray.forEach(file => {
|
|
|
|
claimsPromiseArray.push(db.Claim.resolveClaim(file.name, file.claimId));
|
2017-09-11 21:10:41 +02:00
|
|
|
});
|
2017-12-07 19:45:19 +01:00
|
|
|
return Promise.all(claimsPromiseArray);
|
2017-09-11 21:10:41 +02:00
|
|
|
}
|
2017-12-07 19:45:19 +01:00
|
|
|
})
|
|
|
|
.then(claimsArray => {
|
|
|
|
resolve(claimsArray);
|
2017-08-16 00:35:03 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-07-11 02:51:29 +02:00
|
|
|
});
|
|
|
|
},
|
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-11-01 01:19:26 +01:00
|
|
|
db.File.getRecentClaims()
|
2017-08-25 01:09:04 +02:00
|
|
|
.then(results => {
|
|
|
|
resolve(results);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-06-28 05:25:36 +02:00
|
|
|
};
|