Track publish time #329
2 changed files with 30 additions and 46 deletions
|
@ -1,7 +1,7 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const ua = require('universal-analytics');
|
const ua = require('universal-analytics');
|
||||||
const config = require('../config/speechConfig.js');
|
const config = require('../config/speechConfig.js');
|
||||||
const db = require('../models');
|
const db = require('../models/index');
|
||||||
const googleApiKey = config.analytics.googleId;
|
const googleApiKey = config.analytics.googleId;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -38,7 +38,7 @@ module.exports = {
|
||||||
logger.error('Sequelize error >>', error);
|
logger.error('Sequelize error >>', error);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
sendGoogleAnalytics (action, headers, ip, originalUrl) {
|
sendGoogleAnalyticsEvent (action, headers, ip, originalUrl) {
|
||||||
const visitorId = ip.replace(/\./g, '-');
|
const visitorId = ip.replace(/\./g, '-');
|
||||||
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
|
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
|
||||||
let params;
|
let params;
|
||||||
|
@ -52,15 +52,6 @@ module.exports = {
|
||||||
ul : headers['accept-language'],
|
ul : headers['accept-language'],
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case 'PUBLISH':
|
|
||||||
params = {
|
|
||||||
ec : 'publish',
|
|
||||||
ea : originalUrl,
|
|
||||||
uip: ip,
|
|
||||||
ua : headers['user-agent'],
|
|
||||||
ul : headers['accept-language'],
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
visitor.event(params, (err) => {
|
visitor.event(params, (err) => {
|
||||||
|
@ -69,40 +60,28 @@ module.exports = {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getTrendingClaims (startDate) {
|
sendGoogleAnalyticsTiming (action, headers, ip, originalUrl, startTime, endTime) {
|
||||||
logger.debug('retrieving trending');
|
const visitorId = ip.replace(/\./g, '-');
|
||||||
return new Promise((resolve, reject) => {
|
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
|
||||||
// get the raw requests data
|
const time = endTime - startTime;
|
||||||
db.getTrendingFiles(startDate)
|
let params;
|
||||||
.then(fileArray => {
|
switch (action) {
|
||||||
let claimsPromiseArray = [];
|
case 'PUBLISH':
|
||||||
if (fileArray) {
|
params = {
|
||||||
fileArray.forEach(file => {
|
userTimingCategory : 'lbrynet',
|
||||||
claimsPromiseArray.push(db.Claim.resolveClaim(file.name, file.claimId));
|
userTimingVariableName: 'publish',
|
||||||
});
|
userTimingTime : time,
|
||||||
return Promise.all(claimsPromiseArray);
|
uip : ip,
|
||||||
|
ua : headers['user-agent'],
|
||||||
|
ul : headers['accept-language'],
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
visitor.timing(params, (err) => {
|
||||||
|
if (err) {
|
||||||
|
logger.error('Google Analytics Event Error >>', err);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.then(claimsArray => {
|
|
||||||
resolve(claimsArray);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getRecentClaims () {
|
|
||||||
logger.debug('retrieving most recent claims');
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// get the raw requests data
|
|
||||||
db.File.getRecentClaims()
|
|
||||||
.then(results => {
|
|
||||||
resolve(results);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
logger.error('sequelize error', error);
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
|
@ -7,6 +7,7 @@ const { checkClaimNameAvailability, checkChannelAvailability, publish } = requir
|
||||||
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
|
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
|
||||||
const { createPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel } = require('../helpers/publishHelpers.js');
|
const { createPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel } = require('../helpers/publishHelpers.js');
|
||||||
const errorHandlers = require('../helpers/errorHandlers.js');
|
const errorHandlers = require('../helpers/errorHandlers.js');
|
||||||
|
const { sendGoogleAnalyticsTiming } = require('../helpers/statsHelpers.js');
|
||||||
const { authenticateIfNoUserToken } = require('../auth/authentication.js');
|
const { authenticateIfNoUserToken } = require('../auth/authentication.js');
|
||||||
|
|
||||||
function addGetResultsToFileData (fileInfo, getResult) {
|
function addGetResultsToFileData (fileInfo, getResult) {
|
||||||
|
@ -124,9 +125,10 @@ module.exports = (app) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// route to run a publish request on the daemon
|
// route to run a publish request on the daemon
|
||||||
app.post('/api/claim-publish', multipartMiddleware, ({ body, files, ip, originalUrl, user }, res) => {
|
app.post('/api/claim-publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl, user }, res) => {
|
||||||
logger.debug('api/claim-publish body:', body);
|
logger.debug('api/claim-publish body:', body);
|
||||||
logger.debug('api/claim-publish files:', files);
|
logger.debug('api/claim-publish files:', files);
|
||||||
|
const startTime = Date.now();
|
||||||
let name, fileName, filePath, fileType, nsfw, license, title, description, thumbnail, channelName, channelPassword;
|
let name, fileName, filePath, fileType, nsfw, license, title, description, thumbnail, channelName, channelPassword;
|
||||||
// validate the body and files of the request
|
// validate the body and files of the request
|
||||||
try {
|
try {
|
||||||
|
@ -168,6 +170,9 @@ module.exports = (app) => {
|
||||||
lbryTx: result,
|
lbryTx: result,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const endTime = Date.now();
|
||||||
|
console.log('publish end time', endTime);
|
||||||
|
sendGoogleAnalyticsTiming('PUBLISH', headers, ip, originalUrl, startTime, endTime);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
||||||
|
|
Loading…
Reference in a new issue