metricsCtrl.js 3.73 KB
Newer Older
1
/*
2
 * Copyright 2014-2017 the original author or authors.
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16 17
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

18 19 20
module.exports = function ($scope, application) {
  'ngInject';
  $scope.showRichGauges = false;
21
  $scope.metrics = [];
22

23 24 25 26 27 28
  function merge(array, obj) {
    for (var i = 0; i < array.length; i++) {
      if (array[i].name === obj.name) {
        for (var a in obj) {
          if (obj.hasOwnProperty(a)) {
            array[i][a] = obj[a];
29 30
          }
        }
31
        return;
32
      }
33 34 35
    }
    array.push(obj);
  }
36

37 38 39
  application.getMetrics().then(
    function (response) {
      var metricData = {};
40

41 42 43 44 45
      function addData(groupname, valueObj, max) {
        var group = metricData[groupname] || {max: 0, values: []};
        merge(group.values, valueObj);
        if (typeof max !== 'undefined' && max > group.max) {
          group.max = max;
46
        }
47 48
        metricData[groupname] = group;
      }
49

50 51 52 53
      var matchers = [{
        regex: /(gauge\..+)\.val/,
        callback: function (metric, match, value) {
          addData('gauge', {
54 55
            name: match[1],
            value: value
56
          }, value);
57
          $scope.showRichGauges = true;
58 59 60 61 62
        }
      }, {
        regex: /(gauge\..+)\.avg/,
        callback: function (metric, match, value) {
          addData('gauge', {
63 64 65
            name: match[1],
            avg: value.toFixed(2)
          });
66 67 68 69 70
        }
      }, {
        regex: /(gauge\..+)\.min/,
        callback: function (metric, match, value) {
          addData('gauge', {
71 72 73
            name: match[1],
            min: value
          });
74 75 76 77 78
        }
      }, {
        regex: /(gauge\..+)\.max/,
        callback: function (metric, match, value) {
          addData('gauge', {
79 80
            name: match[1],
            max: value
81 82 83 84 85 86
          }, value);
        }
      }, {
        regex: /(gauge\..+)\.count/,
        callback: function (metric, match, value) {
          addData('gauge', {
87 88 89
            name: match[1],
            count: value
          });
90 91 92 93 94 95 96 97 98
        }
      }, {
        regex: /(gauge\..+)\.alpha/,
        callback: function () { /* NOP */
        }
      }, {
        regex: /(gauge\..+)/,
        callback: function (metric, match, value) {
          addData('gauge', {
99 100
            name: match[1],
            value: value
101 102 103 104 105 106 107 108 109 110 111
          }, value);
        }
      }, {
        regex: /^([^.]+).*/,
        callback: function (metric, match, value) {
          addData(match[1], {
            name: metric,
            value: value
          }, value);
        }
      }];
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      var metrics = response.data;
      for (var metric in metrics) {
        if (metrics.hasOwnProperty(metric)) {
          for (var i = 0; i < matchers.length; i++) {
            var match = matchers[i].regex.exec(metric);
            if (match !== null) {
              matchers[i].callback(metric, match, metrics[metric]);
              break;
            }
          }
        }
      }
      for (var groupname in metricData) {
        if (metricData.hasOwnProperty(groupname)) {
          $scope.metrics.push({
            name: groupname,
            values: metricData[groupname].values,
            max: metricData[groupname].max || 0
          });
        }
      }
134 135 136 137
    }
  ).catch(function (response) {
    $scope.error = response.data;
  });
138
};