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

Dashboard metrics implementation #202

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/images/ic_equal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/javascripts/controllers/admin-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ angular.module('jasmic.controllers')
$scope.supplier = obj;
break;
default: showDialog($mdDialog,{statusText: "Error"}, false);
break
break;
}
};
/**
Expand Down
36 changes: 34 additions & 2 deletions public/javascripts/controllers/dashboard-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
angular.module('jasmic.controllers')
.controller('DashboardCtrl', ['$scope','$location','$routeParams', '$mdDialog','CurrentDemandsFactory',
'OpenTransactionsFactory', 'TransactionsFactory','CallLogsFactory', 'UserProfileFactory',
'CallTypesFactory', 'ParishesFactory', 'SuppliersFactory', 'InputsFactory',
'CallTypesFactory', 'ParishesFactory', 'SuppliersFactory', 'InputsFactory','StatisticsFactory',
function ($scope, $location, $routeParams, $mdDialog, CurrentDemandsFactory, OpenTransactionsFactory,
TransactionsFactory, CallLogsFactory, UserProfileFactory, CallTypesFactory,
ParishesFactory, SuppliersFactory, InputsFactory) {
ParishesFactory, SuppliersFactory, InputsFactory, StatisticsFactory) {
/**
* Gets all calls associated with the logged in
* user id.
Expand All @@ -17,6 +17,8 @@ angular.module('jasmic.controllers')
*/
$scope.isAdmin = false;
$scope.parish_label = "", $scope.store_label = "";
$scope.metric = {};
var icons = {increase: "/images/ic_arrow_up_24px.svg", decrease: "/images/ic_arrow_down_24px.svg", neutral: "/images/icons/icons_star.svg" };
UserProfileFactory.show(function(user){
CallLogsFactory.query({us_user_id: user._id}, function(calls){
if(calls.length > 0){
Expand All @@ -39,6 +41,36 @@ angular.module('jasmic.controllers')
});
};
lookupCallsForToday();
/**
* Gets performance metrics to be displayed.
*/
getStatistics = function(){
StatisticsFactory.show({}, function(stats){
$scope.stats = stats;
showPerformanceMetricIcons();
})
}
getStatistics();
/**
* Determines if a up, down or equal arrow is to be shown.
*/
showPerformanceMetricIcons = function(){
if($scope.stats.call.changes.change === "none"){
$scope.metric.calls = icons.neutral;
}else if($scope.stats.call.changes.change === "increase"){
$scope.metric.calls = icons.increase;
}else{
$scope.metric.calls = icons.decrease;
}

if($scope.stats.completed_trans.changes.change === "none"){
$scope.metric.closed_trans = icons.neutral;
}else if($scope.stats.completed_trans.changes.change === "increase"){
$scope.metric.closed_trans = icons.increase;
}else{
$scope.metric.closed_trans = icons.decrease;
}
}
/**
* looks up current demands
*/
Expand Down
1 change: 0 additions & 1 deletion public/javascripts/controllers/navigation-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ function showNewCallInputDialog($mdDialog, $scope){
$mdDialog.hide();
showDialog($mdDialog, {statusText:"New Call Added!"}, false);
clearCallFormData();
loadCalls();
}, function(fail){
$mdDialog.hide();
showDialog($mdDialog, error, true);
Expand Down
2 changes: 0 additions & 2 deletions public/javascripts/controllers/search-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ angular.module('jasmic.controllers')
$scope.commoditySelected = false;
$scope.inputSelected = true;
$scope.selectedInput = obj;
} else {
console.log('Mi nuh know weh you click pan boss man');
}
};

Expand Down
6 changes: 6 additions & 0 deletions public/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,9 @@ services.factory('EmailFactory', function($resource){
create: { method: 'POST'}
});
});

services.factory('StatisticsFactory', function($resource){
return $resource('/common/stats', {}, {
show: { method: 'GET'}
});
})
21 changes: 11 additions & 10 deletions public/partials/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@
<div class="infoStat" layout-align="center center" flex layout-fill layout="row">
<div flex="20"><md-icon md-svg-src="/images/ic_shopping_cart_24px.svg"></md-icon></div>
<div class="textLabel" flex>Opened Demands</div>
<div><md-icon md-svg-src="/images/ic_arrow_up_24px.svg"></md-icon></div>
<div><h1>{{demands.length}}</h1></div>
<div>
</div>
<div><h1>{{ demands.length }}</h1></div>
</div>
</div>
<div layout-padding flex ng-click="setSelected('transactions')" layout-align="end end" layout="column" >
<div flex="10">&nbsp;</div>
<div class="infoStat" layout-align="center center" flex layout-fill layout="row">
<div flex="20"><md-icon md-svg-src="/images/ic_money_off_24px.svg"></md-icon></div>
<div class="textLabel" flex>Pending Transactions</div>
<div><md-icon md-svg-src="/images/ic_arrow_down_24px.svg"></md-icon></div>
<div><h1>{{open_transactions.length}}</h1></div>
<div></div>
<div><h1>{{ open_transactions.length }}</h1></div>
</div>
</div>
<div layout-padding flex ng-click="setSelected('closed_transactions')" layout-align="end end" layout="column" >
<div flex="10">This Week</div>
<div flex="10"><strong>This Week</strong></div>
<div class="infoStat" layout-align="center center" flex layout-fill layout="row">
<div flex="20"><md-icon md-svg-src="/images/ic_attach_money_24px.svg"></md-icon></div>
<div class="textLabel" flex>Closed Transactions</div>
<div><md-icon md-svg-src="/images/ic_arrow_up_24px.svg"></md-icon></div>
<div><h1>{{closed_transactions.length}}</h1></div>
<div><md-icon md-svg-src="{{metric.closed_trans}}"></md-icon></div>
<div><h1>{{stats.completed_trans.current_week}}</h1></div>
</div>
</div>
<div layout-padding flex ng-click="setSelected('calls')" layout-align="end end" layout="column">
<div flex="10">Today</div>
<div flex="10"><strong>This Week</strong></div>
<div class="infoStat" layout-align="center center" flex layout-fill layout="row">
<div flex="20"><md-icon md-svg-src="/images/ic_call_24px.svg"></md-icon></div>
<div class="textLabel" flex>Calls Made</div>
<div><md-icon md-svg-src="/images/ic_arrow_down_24px.svg"></md-icon></div>
<div><h1>{{total_calls.length}}</h1></div>
<div><md-icon md-svg-src="{{metric.calls}}"></md-icon></div>
<div><h1>{{stats.call.current_week}}</h1></div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion routes/buyer/buyer_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ module.exports = router;
*/
function dummyStub(req, res) {
res.end({info: "unimplemented"});
}
}
4 changes: 2 additions & 2 deletions routes/calls/call_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var CallLogs = require('./calls');
var router = express.Router();

/**
* End Points relevant to CallLogs and
* End Points relevant to CallLogs and
* Call Types.
*/
router.get('/calls', CallLogs.searchCalls);
Expand All @@ -12,4 +12,4 @@ router.get('/calltypes', CallLogs.getCallTypes);
router.post('/calltype', CallLogs.createCallType);


module.exports = router;
module.exports = router;
7 changes: 4 additions & 3 deletions routes/calls/calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ exports.searchCalls = function(req, res){
if(common.isAuthenticated(req, res)) {
var query = req.query;
if(req.query.today === 'true'){
Copy link
Contributor

Choose a reason for hiding this comment

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

@tremainebuchanan you need some documentation here... be careful how you use the entity comparison. If you are expecting boolean try and use boolean true and the == comparator.

var today = moment().startOf('day');
var tomorrow = moment(today).add(1,'days');
query = {cc_date: {$gte: today.toDate(), $lt: tomorrow.toDate()}};
var start_of_week = moment().startOf('week');
var now = moment();
query = {cc_date: {$gte: start_of_week.toDate(), $lt: now.toDate()}};
}
CallLog.find(query)
.populate('ct_call_type')
Expand All @@ -33,6 +33,7 @@ exports.searchCalls = function(req, res){
});
}
};

/**
* Routes call creation based on an
* entity type.
Expand Down
96 changes: 86 additions & 10 deletions routes/common/common.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/**
* Created by matjames007 on 4/29/15.
*/
var model = require('../../models/db');
var Address = model.Address;
var Parish = model.Parish;
var Farmer = model.Farmer;
var Buyer = model.Buyer;
var Unit = model.Unit;
var Demand = model.Demand;
var Crop = model.Crop;
var Commodity = model.Commodity;
var District = model.District;
var model = require('../../models/db'),
Address = model.Address,
Parish = model.Parish,
Farmer = model.Farmer,
Buyer = model.Buyer,
Unit = model.Unit,
Demand = model.Demand,
Crop = model.Crop,
Commodity = model.Commodity,
District = model.District,
CallLog = model.CallLog,
Transaction = model.Transaction,
moment = require('moment');

/**
* Check if user is logged in.
Expand Down Expand Up @@ -397,3 +400,76 @@ exports.batchPushDistricts = function(req, res) {
}
})
};
/**
* Generates the dashboard performance metrics from the database
* by counting the number of calls, demands or transactions
* made within a period of time.
* @param {[type]} req [description]
* @param {[type]} res [description]
*/
exports.getStats = function(req, res){
Copy link
Contributor

Choose a reason for hiding this comment

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

@paradoxxjm gosh we need to put in a test suite on this... hard to go through this in the code review :(
I will get this done on a weekend.

var start_of_prev_week = moment().startOf('day').day(1).subtract(7, 'days'),
last_week = moment().day(moment().day()).subtract(7, 'days'),
current_week = moment().startOf('week'),
today = moment(),
prev_week_query = {$gte: start_of_prev_week.toDate(), $lt:last_week.toDate()},
current_week_query = {$gte: current_week.toDate(), $lt:today.toDate()};
CallLog.count({cc_date: prev_week_query}).exec(function(err, prev_call_count){
if(err){
handleDBError(err, res);
}else{
CallLog.count({cc_date: current_week_query}).exec(function(err, current_call_count){
if(err){
handleDBError(err, res);
}else{
Transaction
.count({tr_status: 'Completed', tr_date_created: prev_week_query})
.exec(function(err, prev_completed_trans_count){
if(err){
handleDBError(err, res);
}else{
Transaction
.count({tr_status: 'Completed', tr_date_created: current_week_query})
.exec(function(err, current_completed_trans_count){
if(err){
handleDBError(err, res);
}else{
var stats = { completed_trans: { current_week: current_completed_trans_count,
previous_week: prev_completed_trans_count },
call: { current_week: current_call_count,
previous_week: prev_call_count }}//end of object
formatandSendStats(stats, res);
}
});
}
});
}
});
}
});
}
/**
* Determines if there has been any change week on week
* for performance metrics.
* @param {[type]} stats Calculated performance metrics
* @param {[type]} res [description]
*/
formatandSendStats = function(stats, res){
stats.completed_trans.changes = getStatisticChange(stats.completed_trans.current_week, stats.completed_trans.previous_week);
stats.call.changes = getStatisticChange(stats.call.current_week, stats.call.previous_week);
res.send(stats);
}
/**
* Determines change in performance metrics. The function
* attempts to return whether there is an increase, decrease or
* no change in a performance metric.
* @param {[type]} current Current week's count of a particular metric
* @param {[type]} prev Previous week's count of a particular metric
*/
getStatisticChange = function(current, prev){
var stat_change = {};
current === prev ? stat_change.change = "none" :
current > prev ? stat_change.change = "increase" : stat_change.change = "decrease";
stat_change.changed_by = Math.abs(current - prev);
return stat_change;
}
3 changes: 2 additions & 1 deletion routes/common/common_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ router.get('/search', Common.searchAll);

router.get('/units', Common.findUnits);
router.post('/unit', Common.createUnit);
router.get('/stats', Common.getStats);


module.exports = router;
module.exports = router;