Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sdl server statistics #150

Closed
wants to merge 15 commits into from
Closed
4 changes: 4 additions & 0 deletions app/v1/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const path = require('path');
const config = require('../../settings'); //configuration module
const log = require(`../../custom/loggers/${config.loggerModule}/index.js`);
const db = require(`../../custom/databases/${config.dbModule}/index.js`)(log); //pass in the logger module that's loaded
const reportingService = require(`../../lib/reporting-service/index.js`)({db,expirationDays: config.reporting.expirationDays,trackingEnabled: config.reporting.enabled});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reference the database library and config file within the reporting-service rather than passing them as parameters. See existing project files for precedent.

const flame = require('../../lib/flame-box');
const hashify = require('../../lib/hashify');
const arrayify = require('../../lib/arrayify');
Expand All @@ -24,6 +25,7 @@ app.locals.arrayify = arrayify;
app.locals.emailer = emailer;
app.locals.flame = flame;
app.locals.version = path.basename(__dirname);
app.locals.reportingService = reportingService;

// construct base URL, e.g. "http://localhost:3000"
app.locals.baseUrl = "http";
Expand Down Expand Up @@ -61,6 +63,7 @@ function exposeRoutes () {
//app.post('/register', register.post);
app.post('/login', login.validateAuth);
app.get('/applications', auth.validateAuth, applications.get);
app.get('/applications/report', auth.validateAuth, applications.getReport);
app.post('/applications/action', auth.validateAuth, applications.actionPost);
app.post('/applications/auto', auth.validateAuth, applications.autoPost);
app.post('/applications/administrator', auth.validateAuth, applications.administratorPost);
Expand Down Expand Up @@ -88,6 +91,7 @@ function exposeRoutes () {
app.get('/module', auth.validateAuth, moduleConfig.get);
app.post('/module', auth.validateAuth, moduleConfig.post);
app.post('/module/promote', auth.validateAuth, moduleConfig.promote);
app.get('/module/report', auth.validateAuth, moduleConfig.getReport);
app.get('/about', auth.validateAuth, about.getInfo);
}

Expand Down
12 changes: 12 additions & 0 deletions app/v1/applications/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ const sql = require('./sql.js');
const flow = app.locals.flow;
const async = require('async');

function getReport (req, res, next) {
if(req.query.id === null || req.query.id === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(req.query.id === null || req.query.id === undefined) {
if(!req.query.id) {

return res.parcel.setStatus(400).setMessage('id required').deliver();
}
helper.getAggregateReportByAppId(req.query.id, function (reportData) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic executes code which may result in an error (such as a SQL connection or query error), but errors in callbacks seem to be ignored throughout this PR. Please revise to include proper error handling.

All callbacks are expected to be in the format of callback(error, [result]).

return res.parcel.setStatus(200)
.setData(reportData)
.deliver();
});
}

function get (req, res, next) {
//prioritize id, uuid, approval status, in that order.
//only one parameter can be acted upon in one request
Expand Down Expand Up @@ -294,6 +305,7 @@ function queryAndStoreApplicationsFlow (queryObj, notifyOEM = true) {

module.exports = {
get: get,
getReport: getReport,
actionPost: actionPost,
putServicePermission: putServicePermission,
autoPost: autoPost,
Expand Down
6 changes: 6 additions & 0 deletions app/v1/applications/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const flame = app.locals.flame;
const log = app.locals.log;
const db = app.locals.db;
const config = app.locals.config;
const moment = require('moment');

//validation functions

Expand Down Expand Up @@ -264,7 +265,12 @@ function attemptRetry(milliseconds, retryQueue){
}, milliseconds);
}

function getAggregateReportByAppId (appId, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary function. Function calls another function without any additions or modifications.

return app.locals.reportingService.getAppUsageReport(appId, cb);
}

module.exports = {
getAggregateReportByAppId: getAggregateReportByAppId,
validateActionPost: validateActionPost,
validateAutoPost: validateAutoPost,
validateAdministratorPost: validateAdministratorPost,
Expand Down
15 changes: 12 additions & 3 deletions app/v1/module-config/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ const model = require('./model.js');
const flow = app.locals.flow;
const cache = require('../../../custom/cache');

function getReport (req, res) {
helper.getAggregateReport(function (reportData) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling.

return res.parcel.setStatus(200)
.setData(reportData)
.deliver();
});
}

function get (req, res, next) {
//if environment is not of value "staging", then set the environment to production
const isProduction = !req.query.environment || req.query.environment.toLowerCase() !== 'staging';
Expand Down Expand Up @@ -59,7 +67,8 @@ function post (isProduction, req, res, next) {
}

module.exports = {
get: get,
post: post.bind(null, false),
promote: post.bind(null, true)
getReport: getReport,
get: get,
post: post.bind(null, false),
promote: post.bind(null, true)
};
20 changes: 19 additions & 1 deletion app/v1/module-config/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const app = require('../app');
const flow = app.locals.flow;
const setupSql = app.locals.db.setupSqlCommand;
const sql = require('./sql.js');
const reportingService = app.locals.reportingService;

//validation functions

Expand Down Expand Up @@ -96,7 +97,24 @@ function getModuleConfigFlow (property, value) {
], {method: 'waterfall', eventLoop: true});
}

function getAggregateReport (cb) {
let obj = {
report_days: reportingService.expirationDays,
};

reportingService.getDeviceReport(function (deviceReport) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling.

reportingService.getPolicyTableUpdatesReport(function (policyTableUpdatesReport) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling.

Object.assign(obj,
deviceReport,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation.

policyTableUpdatesReport
);
cb(obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error param in callback execution.

});
});
}

module.exports = {
getAggregateReport: getAggregateReport,
validatePost: validatePost,
getModuleConfigFlow: getModuleConfigFlow
}
};
11 changes: 11 additions & 0 deletions app/v1/policy/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const helper = require('./helper.js');
const encryption = require('../../../customizable/encryption');
const GET = require('lodash.get');

/**
* Called whenever core requests the policy table.
* Extra reporting can be recorded when isProduction = true
* @param isProduction
* @returns {Function}
*/
function postFromCore (isProduction) {
return function (req, res, next) {
// attempt decryption of the policy table if it's defined
Expand All @@ -17,6 +23,11 @@ function postFromCore (isProduction) {
}
const useLongUuids = GET(req, "body.policy_table.module_config.full_app_id_supported", false) ? true : false;
helper.generatePolicyTable(isProduction, useLongUuids, req.body.policy_table.app_policies, true, handlePolicyTableFlow.bind(null, res, true));


//Update reporting as a separate process. We do not need to wait on reporting to complete before responding with the policy table update request.
let policyTable = req.body.policy_table || {};
app.locals.reportingService.updateReporting(policyTable, undefined, useLongUuids);
}
}

Expand Down
3 changes: 2 additions & 1 deletion build/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin({
'AUTH_TYPE': JSON.stringify(settings.authType)
'AUTH_TYPE': JSON.stringify(settings.authType),
'REPORTING_ENABLED': settings.reporting.enabled
})
],
module: {
Expand Down
9 changes: 6 additions & 3 deletions build/webpack.prod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

const env = config.build.env

Expand All @@ -31,9 +32,11 @@ const webpackConfig = merge(baseWebpackConfig, {
'process.env': env
}),
// UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false
},
},
sourceMap: true
}),
Expand Down
Loading