Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.factory("kpiFac", function($rootScope, $http) {
- const getMaxObj = function(arr, val) {
- //since math.max doesn't REALLY work on objects, and either way, it returns ONLY the max val (not all associated data);
- var currMax = null;
- console.log('max of', arr, 'at', val)
- for (var i = 0; i < arr.length; i++) {
- if (!currMax || currMax[val] < arr[i][val]) {
- currMax = arr[i];
- }
- }
- return currMax;
- },
- dayDur = 1000 * 3600 * 24,
- getMinObj = function(arr, val) {
- //since math.min doesn't REALLY work on objects, and either way, it returns ONLY the min val (not all associated data);
- var currMin = null;
- for (var i = 0; i < arr.length; i++) {
- if (!currMin || currMin[val] > arr[i][val]) {
- currMin = arr[i];
- }
- }
- return currMin;
- };
- return {
- getKPI: function(dev) {
- let lastTime = devs[0].record_date
- if (testMode) {
- now = new Date(lastTime)
- }
- if (req.params.pol && req.params.pol.length > 0) {
- devs = devs.filter(x => { return x.sensor_type.toLowerCase() == req.params.pol.toLowerCase() })
- }
- let kpiRez = {
- highWeek: null,
- lowWeek: null,
- highDay: null,
- lowDay: null,
- highVarDay: { score: 0, time: null },
- highVarWk: { score: 0, time: null },
- maxDevsDay: 1
- },
- day = devs.filter(x => {
- // console.log('DATE', x.record_date, new Date(x.record_date).getTime())
- return now - new Date(x.record_date).getTime() < 86400000;
- }),
- wk = devs.filter(y => {
- return now - new Date(y.record_date).getTime() < 604800000;
- });
- kpiRez.highWeek = getMaxObj(wk, 'score');
- kpiRez.lowWeek = getMinObj(wk, 'score');
- kpiRez.highDay = getMaxObj(day, 'score');
- kpiRez.lowDay = getMinObj(day, 'score');
- kpiRez.highVarDay.score = -99999;
- kpiRez.highVarWk.score = -99999;
- for (var i = 0; i < wk.length - 1; i++) {
- if (Math.abs(wk[i + 1].score - wk[i].score) > kpiRez.highVarWk.score) {
- kpiRez.highVarWk.score = Math.abs(wk[i + 1].score - wk[i].score);
- kpiRez.highVarWk.time = wk[i].record_date;
- }
- }
- for (var i = 0; i < day.length - 1; i++) {
- if (Math.abs(day[i + 1].score - day[i].score) > kpiRez.highVarDay.score) {
- kpiRez.highVarDay.score = Math.abs(day[i + 1].score - day[i].score);
- kpiRez.highVarDay.time = day[i].record_date;
- }
- }
- return kpiRez;
- }
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement