split publish timing into two stats

This commit is contained in:
bill bittner 2018-01-23 12:08:53 -08:00
parent c760f1a392
commit 76aadefc5e
6 changed files with 58 additions and 39 deletions

2
constants/index.js Normal file
View file

@ -0,0 +1,2 @@
export const PUBLISH_ANONYMOUS_CLAIM = 'PUBLISH_ANONYMOUS_CLAIM';
export const PUBLISH_IN_CHANNEL_CLAIM = 'PUBLISH_IN_CHANNEL_CLAIM';

View file

@ -112,6 +112,7 @@ module.exports = {
},
createChannel (name) {
return new Promise((resolve, reject) => {
// make the request
axios
.post(lbryApiUrl, {
method: 'channel_new',

View file

@ -1,3 +1,4 @@
import * as constants from '../constants';
const logger = require('winston');
const fs = require('fs');
const { site, wallet } = require('../config/speechConfig.js');
@ -158,5 +159,29 @@ module.exports = {
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,8 +1,21 @@
import * as constants from '../constants';
const logger = require('winston');
const ua = require('universal-analytics');
const config = require('../config/speechConfig.js');
const db = require('../models');
const googleApiKey = config.analytics.googleId;
const db = require('../models');
function createPublishTimingEventParams (publishDurration, ip, headers, label) {
return {
userTimingCategory : 'lbrynet',
userTimingVariableName: 'publish',
userTimingTime : publishDurration,
userTimingLabel : label,
uip : ip,
ua : headers['user-agent'],
ul : headers['accept-language'],
};
};
module.exports = {
postToStats (action, url, ipAddress, name, claimId, result) {
@ -63,18 +76,13 @@ module.exports = {
sendGoogleAnalyticsTiming (action, headers, ip, originalUrl, startTime, endTime) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
const publishDurration = endTime - startTime;
const durration = endTime - startTime;
let params;
switch (action) {
case 'PUBLISH':
params = {
userTimingCategory : 'lbrynet',
userTimingVariableName: 'publish',
userTimingTime : publishDurration,
uip : ip,
ua : headers['user-agent'],
ul : headers['accept-language'],
};
case constants.PUBLISH_ANONYMOUS_CLAIM:
case constants.PUBLISH_IN_CHANNEL_CLAIM:
logger.verbose(`${action} completed successfully in ${durration}ms`);
params = createPublishTimingEventParams(durration, ip, headers, action);
break;
default: break;
}
@ -82,7 +90,7 @@ module.exports = {
if (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]) => {
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['userName'] = newUser.userName;
logger.verbose('channel result >', newChannel.dataValues);
userInfo['channelName'] = newChannel.channelName;
userInfo['channelClaimId'] = newChannel.channelClaimId;
logger.verbose('certificate result >', newCertificate.dataValues);
// associate the instances
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 { checkClaimNameAvailability, checkChannelAvailability, publish } = require('../controllers/publishController.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 { sendGoogleAnalyticsTiming } = require('../helpers/statsHelpers.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) => {
// route to run a claim_list request on the daemon
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) => {
logger.debug('api/claim-publish body:', body);
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();
logger.debug('publish request started @', publishStartTime);
let timingActionType;
// define variables
let name, fileName, filePath, fileType, nsfw, license, title, description, thumbnail, channelName, channelPassword;
// validate the body and files of the request
try {
@ -158,7 +141,8 @@ module.exports = (app) => {
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
})
.then(publishParams => {
logger.debug('publishParams:', publishParams);
// set the timing event type for reporting
timingActionType = returnPublishTimingActionType(publishParams.channelName);
// publish the asset
return publish(publishParams, fileName, fileType);
})
@ -171,9 +155,10 @@ module.exports = (app) => {
lbryTx: result,
},
});
// log the publish end time
const publishEndTime = Date.now();
logger.debug('publish request completed @', publishEndTime);
sendGoogleAnalyticsTiming('PUBLISH', headers, ip, originalUrl, publishStartTime, publishEndTime);
sendGoogleAnalyticsTiming(timingActionType, headers, ip, originalUrl, publishStartTime, publishEndTime);
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);