Merge pull request #331 from lbryio/split-publish-timing-events

Split publish timing events
This commit is contained in:
Bill Bittner 2018-01-23 15:13:31 -08:00 committed by GitHub
commit 4c94ccee52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 40 deletions

4
constants/index.js Normal file
View file

@ -0,0 +1,4 @@
module.exports = {
PUBLISH_ANONYMOUS_CLAIM : 'PUBLISH_ANONYMOUS_CLAIM',
PUBLISH_IN_CHANNEL_CLAIM: 'PUBLISH_IN_CHANNEL_CLAIM',
};

View file

@ -18,7 +18,7 @@ module.exports = {
logger.debug(`this claim was published in channel: ${publishParams.channel_name}`); logger.debug(`this claim was published in channel: ${publishParams.channel_name}`);
return db.Channel.findOne({where: {channelName: publishParams.channel_name}}); return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
} else { } else {
logger.debug('this claim was published in channel: n/a'); logger.debug('this claim was not published in a channel');
return null; return null;
} }
}) })

View file

@ -1,3 +1,4 @@
const constants = require('../constants');
const logger = require('winston'); const logger = require('winston');
const fs = require('fs'); const fs = require('fs');
const { site, wallet } = require('../config/speechConfig.js'); const { site, wallet } = require('../config/speechConfig.js');
@ -158,5 +159,29 @@ module.exports = {
logger.debug(`successfully deleted ${filePath}`); logger.debug(`successfully deleted ${filePath}`);
}); });
}, },
addGetResultsToFileData (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
},
createFileData ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
return {
name,
claimId,
outpoint,
height,
address,
fileName: '',
filePath: '',
fileType: contentType,
nsfw,
};
},
returnPublishTimingActionType (channelName) {
if (channelName) {
return constants.PUBLISH_IN_CHANNEL_CLAIM;
} else {
return constants.PUBLISH_ANONYMOUS_CLAIM;
}
},
}; };

View file

@ -1,10 +1,22 @@
const constants = require('../constants');
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 googleApiKey = config.analytics.googleId; const googleApiKey = config.analytics.googleId;
const db = require('../models');
module.exports = { module.exports = {
createPublishTimingEventParams (publishDurration, ip, headers, label) {
return {
userTimingCategory : 'lbrynet',
userTimingVariableName: 'publish',
userTimingTime : publishDurration,
userTimingLabel : label,
uip : ip,
ua : headers['user-agent'],
ul : headers['accept-language'],
};
},
postToStats (action, url, ipAddress, name, claimId, result) { postToStats (action, url, ipAddress, name, claimId, result) {
logger.debug('action:', action); logger.debug('action:', action);
// make sure the result is a string // make sure the result is a string
@ -63,18 +75,13 @@ module.exports = {
sendGoogleAnalyticsTiming (action, headers, ip, originalUrl, startTime, endTime) { sendGoogleAnalyticsTiming (action, headers, ip, originalUrl, startTime, endTime) {
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 });
const publishDurration = endTime - startTime; const durration = endTime - startTime;
let params; let params;
switch (action) { switch (action) {
case 'PUBLISH': case constants.PUBLISH_ANONYMOUS_CLAIM:
params = { case constants.PUBLISH_IN_CHANNEL_CLAIM:
userTimingCategory : 'lbrynet', logger.verbose(`${action} completed successfully in ${durration}ms`);
userTimingVariableName: 'publish', params = module.exports.createPublishTimingEventParams(durration, ip, headers, action);
userTimingTime : publishDurration,
uip : ip,
ua : headers['user-agent'],
ul : headers['accept-language'],
};
break; break;
default: break; default: break;
} }
@ -82,7 +89,7 @@ module.exports = {
if (err) { if (err) {
logger.error('Google Analytics Event Error >>', err); logger.error('Google Analytics Event Error >>', err);
} }
logger.info(`publish completed successfully in ${publishDurration}ms`); logger.debug(`${action} timing event successfully sent to google analytics`);
}); });
}, },
}; };

View file

@ -40,13 +40,11 @@ module.exports = new PassportLocalStrategy(
}) })
.then(([newUser, newChannel, newCertificate]) => { .then(([newUser, newChannel, newCertificate]) => {
logger.verbose('user and certificate successfully created'); logger.verbose('user and certificate successfully created');
logger.debug('user result >', newUser.dataValues); // store the relevant newUser info to be passed back for req.User
userInfo['id'] = newUser.id; userInfo['id'] = newUser.id;
userInfo['userName'] = newUser.userName; userInfo['userName'] = newUser.userName;
logger.verbose('channel result >', newChannel.dataValues);
userInfo['channelName'] = newChannel.channelName; userInfo['channelName'] = newChannel.channelName;
userInfo['channelClaimId'] = newChannel.channelClaimId; userInfo['channelClaimId'] = newChannel.channelClaimId;
logger.verbose('certificate result >', newCertificate.dataValues);
// associate the instances // associate the instances
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]); return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
}) })

View file

@ -5,31 +5,11 @@ const multipartMiddleware = multipart({uploadDir: files.uploadDirectory});
const db = require('../models'); const db = require('../models');
const { checkClaimNameAvailability, checkChannelAvailability, publish } = require('../controllers/publishController.js'); const { checkClaimNameAvailability, checkChannelAvailability, publish } = require('../controllers/publishController.js');
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, addGetResultsToFileData, createFileData, returnPublishTimingActionType } = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js'); const errorHandlers = require('../helpers/errorHandlers.js');
const { sendGoogleAnalyticsTiming } = require('../helpers/statsHelpers.js'); const { sendGoogleAnalyticsTiming } = require('../helpers/statsHelpers.js');
const { authenticateIfNoUserToken } = require('../auth/authentication.js'); const { authenticateIfNoUserToken } = require('../auth/authentication.js');
function addGetResultsToFileData (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
}
function createFileData ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
return {
name,
claimId,
outpoint,
height,
address,
fileName: '',
filePath: '',
fileType: contentType,
nsfw,
};
}
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
app.get('/api/claim-list/:name', ({ ip, originalUrl, params }, res) => { app.get('/api/claim-list/:name', ({ ip, originalUrl, params }, res) => {
@ -128,8 +108,11 @@ module.exports = (app) => {
app.post('/api/claim-publish', multipartMiddleware, ({ body, files, headers, 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);
// record the start time of the request and create variable for storing the action type
const publishStartTime = Date.now(); const publishStartTime = Date.now();
logger.debug('publish request started @', publishStartTime); logger.debug('publish request started @', publishStartTime);
let timingActionType;
// define variables
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 {
@ -158,7 +141,8 @@ module.exports = (app) => {
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName); return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
}) })
.then(publishParams => { .then(publishParams => {
logger.debug('publishParams:', publishParams); // set the timing event type for reporting
timingActionType = returnPublishTimingActionType(publishParams.channel_name);
// publish the asset // publish the asset
return publish(publishParams, fileName, fileType); return publish(publishParams, fileName, fileType);
}) })
@ -171,9 +155,10 @@ module.exports = (app) => {
lbryTx: result, lbryTx: result,
}, },
}); });
// log the publish end time
const publishEndTime = Date.now(); const publishEndTime = Date.now();
logger.debug('publish request completed @', publishEndTime); logger.debug('publish request completed @', publishEndTime);
sendGoogleAnalyticsTiming('PUBLISH', headers, ip, originalUrl, publishStartTime, publishEndTime); sendGoogleAnalyticsTiming(timingActionType, headers, ip, originalUrl, publishStartTime, publishEndTime);
}) })
.catch(error => { .catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res); errorHandlers.handleApiError(originalUrl, ip, error, res);