Analytics Update #43

Merged
bones7242 merged 5 commits from analytics into master 2017-06-28 06:40:46 +02:00
8 changed files with 147 additions and 7 deletions
Showing only changes of commit 8da934af84 - Show all commits

View file

@ -0,0 +1,72 @@
const logger = require('winston');
const db = require('../models');
module.exports = {
getAnalyticsSummary: () => {
const deferred = new Promise((resolve, reject) => {
// get the raw analytics data
db.Analytics
.findAll()
.then(data => {
const resultHashTable = {};
let totalRequests = 0;
let totalPublish = 0;
let totalShow = 0;
let totalCount = 0;
let totalSuccess = 0;
let totalFailure = 0;
// 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) {
case 'request':
totalRequests += 1;
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;
}
}
}
const percentSuccess = Math.round(totalSuccess / totalCount * 100);
// return results
resolve({ records: resultHashTable, totals: { totalRequests, totalPublish, totalShow, totalCount, totalSuccess, totalFailure }, percentSuccess });
})
.catch(error => {
logger.error('sequelize error', error);
reject(error);
});
});
return deferred;
},
};

View file

@ -80,6 +80,10 @@ h4 {
padding: 3px;
}
.center-text {
text-align: center;
}
.code {
font-family: monospace;
color: darkgrey;
@ -87,6 +91,10 @@ h4 {
}
/* other */
table {
width: 100%;
text-align: left;
}
.stop-float {
clear: both;

View file

@ -65,3 +65,10 @@ canvas {
float: left;
}
/* analytics */
.totals-row {
border-top: 1px solid grey;
border-bottom: 1px solid grey;
font-weight: bold;
}

View file

@ -0,0 +1,17 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const analyticsController = require('../controllers/analyticsController.js');
module.exports = (app) => {
// route to show analytics
app.get('/analytics', (req, res) => {
// get and serve content
analyticsController
.getAnalyticsSummary()
.then(result => {
res.status(200).render('analytics', result);
})
.catch(error => {
errorHandlers.handleRequestError(error, res);
});
});
};

View file

@ -3,7 +3,7 @@ const showController = require('../controllers/showController.js');
const logger = require('winston');
const { postShowAnalytics } = require('../helpers/libraries/analytics');
module.exports = (app, ua, googleAnalyticsId) => {
module.exports = (app) => {
// route to fetch all free public claims
app.get('/meme-fodder/play', ({ originalUrl, ip }, res) => {
logger.debug(`GET request on ${originalUrl} from ${ip}`);

View file

@ -4,7 +4,7 @@ const publishHelpers = require('../helpers/libraries/publishHelpers.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { postPublishAnalytics } = require('../helpers/libraries/analytics');
module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
module.exports = (app, siofu, hostedContentPath) => {
const http = require('http').Server(app);
const io = require('socket.io')(http);

View file

@ -5,10 +5,8 @@ const siofu = require('socketio-file-upload');
const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const config = require('config');
const ua = require('universal-analytics');
const winston = require('winston');
const googleAnalyticsId = config.get('AnalyticsConfig.GoogleId');
const hostedContentPath = config.get('Database.PublishUploadPath');
// configure logging
@ -57,12 +55,13 @@ app.set('view engine', 'handlebars');
// require express routes
require('./routes/api-routes.js')(app);
require('./routes/show-routes.js')(app, ua, googleAnalyticsId);
require('./routes/serve-routes.js')(app, ua, googleAnalyticsId);
require('./routes/analytics-routes.js')(app);
require('./routes/show-routes.js')(app);
require('./routes/serve-routes.js')(app);
require('./routes/home-routes.js')(app);
// require socket.io routes
const http = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath, ua, googleAnalyticsId);
const http = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath);
// sync sequelize
// wrap the server in socket.io to intercept incoming sockets requests

View file

@ -0,0 +1,37 @@
<div class="wrapper">
<div class="top-bar">
{{> topBar}}
</div>
<div>
<h3>Analytics</h3>
<table>
<tr>
<th>action</th>
<th>url</th>
<th class="center-text">count</th>
<th class="center-text">success</th>
<th class="center-text">failure</th>
</tr>
{{#each records}}
<tr>
<td>{{ this.action }}</td>
<td>{{ this.url }}</td>
<td class="center-text">{{ this.count }}</td>
<td class="center-text">{{ this.success }}</td>
<td class="center-text">{{ this.failure }}</td>
</tr>
{{/each}}
<tr>
<td></td>
<td></td>
<td class="totals-row center-text">{{ totals.totalCount }}</td>
<td class="totals-row center-text">{{ totals.totalSuccess }}</td>
<td class="totals-row center-text">{{ totals.totalFailure }}</td>
</tr>
</table>
<p>Requests: {{ totals.totalRequests }}</p>
<p>Publish: {{ totals.totalPublish }}</p>
<p>Show: {{ totals.totalShow }}</p>
<p>Percent Success: {{ percentSuccess}}%</p>
</div>
</div>