removed statsHelpers into statsController

This commit is contained in:
bill bittner 2017-06-29 17:52:37 -07:00
parent b23087094a
commit ad36f26aac
8 changed files with 59 additions and 64 deletions

View file

@ -1,7 +1,60 @@
const logger = require('winston'); const logger = require('winston');
const ua = require('universal-analytics');
const config = require('config');
const db = require('../models'); const db = require('../models');
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
module.exports = { module.exports = {
postToStats: (action, url, ipAddress, result) => {
logger.silly('creating record for statistics db');
// 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();
}
// create record in the db
db.Stats.create({
action,
url,
ipAddress,
result,
})
.then()
.catch(error => {
logger.error('sequelize error', error);
});
},
sendGoogleAnalytics: (action, ip, originalUrl) => {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
switch (action) {
case 'serve':
visitor.event('serve', originalUrl, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
break;
case 'show':
visitor.pageview(originalUrl, 'https://spee.ch', 'show route', (err) => {
if (err) {
logger.error('Google Analytics Pageview Error >>', err);
}
});
break;
case 'publish':
visitor.event('publish', originalUrl, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
break;
default: break;
}
},
getStatsSummary: () => { getStatsSummary: () => {
logger.debug('retrieving site statistics'); logger.debug('retrieving site statistics');
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {

View file

@ -1,5 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const { postToStats } = require('./statsHelpers.js'); const { postToStats } = require('./statsController.js');
module.exports = { module.exports = {
handleRequestError (action, originalUrl, ip, error, res) { handleRequestError (action, originalUrl, ip, error, res) {

View file

@ -1,58 +0,0 @@
const db = require('../../models');
const logger = require('winston');
const ua = require('universal-analytics');
const config = require('config');
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
module.exports = {
postToStats: (action, url, ipAddress, result) => {
logger.silly('creating record for statistics db');
// 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();
}
// create record in the db
db.Stats.create({
action,
url,
ipAddress,
result,
})
.then()
.catch(error => {
logger.error('sequelize error', error);
});
},
sendGoogleAnalytics: (action, ip, originalUrl) => {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
switch (action) {
case 'serve':
visitor.event('serve', originalUrl, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
break;
case 'show':
visitor.pageview(originalUrl, 'https://spee.ch', 'show route', (err) => {
if (err) {
logger.error('Google Analytics Pageview Error >>', err);
}
});
break;
case 'publish':
visitor.event('publish', originalUrl, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
break;
default: break;
}
},
};

View file

@ -5,7 +5,7 @@ const publishController = require('../controllers/publishController.js');
const lbryApi = require('../helpers/libraries/lbryApi.js'); const lbryApi = require('../helpers/libraries/lbryApi.js');
const publishHelpers = require('../helpers/libraries/publishHelpers.js'); const publishHelpers = require('../helpers/libraries/publishHelpers.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js');
module.exports = app => { module.exports = app => {
// route to run a claim_list request on the daemon // route to run a claim_list request on the daemon

View file

@ -1,5 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js');
module.exports = app => { module.exports = app => {
// route for the home page // route for the home page

View file

@ -1,7 +1,7 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const serveController = require('../controllers/serveController.js'); const serveController = require('../controllers/serveController.js');
const logger = require('winston'); const logger = require('winston');
const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js');
function serveFile ({ fileName, fileType, filePath }, res) { function serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName}`); logger.info(`serving file ${fileName}`);

View file

@ -1,7 +1,7 @@
const logger = require('winston'); const logger = require('winston');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const showController = require('../controllers/showController.js'); const showController = require('../controllers/showController.js');
const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js');
const statsController = require('../controllers/statsController.js'); const statsController = require('../controllers/statsController.js');
function sendAnalyticsAndLog (ip, originalUrl) { function sendAnalyticsAndLog (ip, originalUrl) {

View file

@ -2,7 +2,7 @@ const logger = require('winston');
const publishController = require('../controllers/publishController.js'); const publishController = require('../controllers/publishController.js');
const publishHelpers = require('../helpers/libraries/publishHelpers.js'); const publishHelpers = require('../helpers/libraries/publishHelpers.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { postToStats } = require('../helpers/libraries/statsHelpers.js'); const { postToStats } = require('../helpers/libraries/statsController.js');
module.exports = (app, siofu, hostedContentPath) => { module.exports = (app, siofu, hostedContentPath) => {
const http = require('http').Server(app); const http = require('http').Server(app);