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
6 changes: 6 additions & 0 deletions app/v1/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ app.locals.emailer = emailer;
app.locals.flame = flame;
app.locals.version = path.basename(__dirname);

app.locals.reportingService = new (require('../../lib/reporting/ReportingService'))({db,expirationDays: config.reporting.expirationDays,trackingEnabled: config.reporting.enabled});



// construct base URL, e.g. "http://localhost:3000"
app.locals.baseUrl = "http";
if(app.locals.config.policyServerPort == 443) app.locals.baseUrl += "s";
Expand Down Expand Up @@ -61,6 +65,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 +93,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
19 changes: 19 additions & 0 deletions app/v1/applications/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ const sql = require('./sql.js');
const flow = app.locals.flow;
const async = require('async');


async function getReport(req,res,next) {
try {
let reportData = await helper.getAggregateReportByAppId(req.query.id);
return res.parcel.setStatus(200)
.setData(reportData)
.deliver();
}
catch (err)
{
app.locals.log.error(err);
return res.parcel
.setStatus(500)
.setMessage("Internal server error")
.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 +312,7 @@ function queryAndStoreApplicationsFlow (queryObj, notifyOEM = true) {

module.exports = {
get: get,
getReport: getReport,
actionPost: actionPost,
putServicePermission: putServicePermission,
autoPost: autoPost,
Expand Down
8 changes: 8 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,14 @@ function attemptRetry(milliseconds, retryQueue){
}, milliseconds);
}

async function getAggregateReportByAppId(appId)
{

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

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


async function getReport(req,res)
{
try {
let reportData = await helper.getAggregateReport();
return res.parcel.setStatus(200)
.setData(reportData)
.deliver();
}
catch (err)
{
app.locals.log.error(err);
return res.parcel
.setStatus(500)
.setMessage("Internal server error")
.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,6 +79,7 @@ function post (isProduction, req, res, next) {
}

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

//validation functions

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


async function getAggregateReport () {

let obj = {
report_days: reportingService.expirationDays,
}

let deviceReport = await reportingService.getDeviceReport()
let policyTableUpdatesReport = await reportingService.getPolicyTableUpdatesReport()

Object.assign(obj,
deviceReport,
policyTableUpdatesReport
)

return obj
}

module.exports = {
getAggregateReport: getAggregateReport,
validatePost: validatePost,
getModuleConfigFlow: getModuleConfigFlow
}
}
16 changes: 16 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,16 @@ 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.
(async function () {
try {
let policyTable = req.body.policy_table || {};
await app.locals.reportingService.updateReporting(policyTable, undefined, useLongUuids)
} catch (e) {
app.locals.log.error(e.message)
}
})()
}
}

Expand Down
5 changes: 3 additions & 2 deletions build/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const webpack = require('webpack')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
require('dotenv').config();
require('dotenv').config();
const settings = require('../settings.js')

function resolve (dir) {
Expand All @@ -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
15 changes: 15 additions & 0 deletions custom/databases/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ module.exports = function (log) {
getMany(query, params, callback){
pool.getMany(query, params, callback);
},
doQuery: function(query,callback)
{
pool.query(query, function (err, res) {
if (err) {
log.error(err);
log.error(query);
}
//always return an array
callback(err, (res && res.rows) ? res.rows : []);
});
},
//exported functions. these are required to implement
//this function executes the SQL command in <query> and returns a response using the callback function
//the callback requires an error parameter and a response from the SQL query
Expand Down Expand Up @@ -130,6 +141,10 @@ module.exports = function (log) {
// callback(err, client, done)
pool.connect(callback);
},
end()
{
return pool.end();
},
begin: function (client, callback){
// begin a SQL transaction
// callback(err);
Expand Down
Loading