Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.factory("kpiFac", function($rootScope, $http) {
  2.     const getMaxObj = function(arr, val) {
  3.             //since math.max doesn't REALLY work on objects, and either way, it returns ONLY the max val (not all associated data);
  4.             var currMax = null;
  5.             console.log('max of', arr, 'at', val)
  6.             for (var i = 0; i < arr.length; i++) {
  7.                 if (!currMax || currMax[val] < arr[i][val]) {
  8.                     currMax = arr[i];
  9.                 }
  10.             }
  11.             return currMax;
  12.         },
  13.         dayDur = 1000 * 3600 * 24,
  14.         getMinObj = function(arr, val) {
  15.             //since math.min doesn't REALLY work on objects, and either way, it returns ONLY the min val (not all associated data);
  16.             var currMin = null;
  17.             for (var i = 0; i < arr.length; i++) {
  18.                 if (!currMin || currMin[val] > arr[i][val]) {
  19.                     currMin = arr[i];
  20.                 }
  21.             }
  22.             return currMin;
  23.         };
  24.     return {
  25.         getKPI: function(dev) {
  26.             let lastTime = devs[0].record_date
  27.             if (testMode) {
  28.                 now = new Date(lastTime)
  29.             }
  30.             if (req.params.pol && req.params.pol.length > 0) {
  31.                 devs = devs.filter(x => { return x.sensor_type.toLowerCase() == req.params.pol.toLowerCase() })
  32.             }
  33.             let kpiRez = {
  34.                     highWeek: null,
  35.                     lowWeek: null,
  36.                     highDay: null,
  37.                     lowDay: null,
  38.                     highVarDay: { score: 0, time: null },
  39.                     highVarWk: { score: 0, time: null },
  40.                     maxDevsDay: 1
  41.                 },
  42.                 day = devs.filter(x => {
  43.                     // console.log('DATE', x.record_date, new Date(x.record_date).getTime())
  44.                     return now - new Date(x.record_date).getTime() < 86400000;
  45.                 }),
  46.                 wk = devs.filter(y => {
  47.                     return now - new Date(y.record_date).getTime() < 604800000;
  48.                 });
  49.             kpiRez.highWeek = getMaxObj(wk, 'score');
  50.             kpiRez.lowWeek = getMinObj(wk, 'score');
  51.             kpiRez.highDay = getMaxObj(day, 'score');
  52.             kpiRez.lowDay = getMinObj(day, 'score');
  53.             kpiRez.highVarDay.score = -99999;
  54.             kpiRez.highVarWk.score = -99999;
  55.             for (var i = 0; i < wk.length - 1; i++) {
  56.                 if (Math.abs(wk[i + 1].score - wk[i].score) > kpiRez.highVarWk.score) {
  57.                     kpiRez.highVarWk.score = Math.abs(wk[i + 1].score - wk[i].score);
  58.                     kpiRez.highVarWk.time = wk[i].record_date;
  59.                 }
  60.             }
  61.             for (var i = 0; i < day.length - 1; i++) {
  62.                 if (Math.abs(day[i + 1].score - day[i].score) > kpiRez.highVarDay.score) {
  63.                     kpiRez.highVarDay.score = Math.abs(day[i + 1].score - day[i].score);
  64.                     kpiRez.highVarDay.time = day[i].record_date;
  65.                 }
  66.             }
  67.             return kpiRez;
  68.         }
  69.     };
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement