bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/static/js/host.ts (about) 1 interface IHostScope extends ng.IScope { 2 cpu: any; 3 host: string; 4 time: string; 5 tab: string; 6 metrics: string[]; 7 mlink: (m: string) => GraphRequest; 8 setTab: (m: string) => void; 9 idata: any; 10 fsdata: any; 11 mem: any; 12 mem_total: number; 13 error: string; 14 running: string; 15 filterMetrics: string; 16 metadata: any; 17 } 18 19 bosunControllers.controller('HostCtrl', ['$scope', '$http', '$location', '$route', function($scope: IHostScope, $http: ng.IHttpService, $location: ng.ILocationService, $route: ng.route.IRouteService) { 20 var search = $location.search(); 21 $scope.host = search.host; 22 $scope.time = search.time; 23 $scope.tab = search.tab || "stats"; 24 $scope.fsdata = []; 25 $scope.metrics = []; 26 var currentURL = $location.url(); 27 $scope.mlink = (m: string) => { 28 var r = new GraphRequest(); 29 var q = new Query(false); 30 q.metric = m; 31 q.tags = { 'host': $scope.host }; 32 r.queries.push(q); 33 return r; 34 }; 35 $scope.setTab = function(t: string) { 36 $location.search('tab', t); 37 $scope.tab = t; 38 }; 39 $http.get('/api/metric/host/' + $scope.host) 40 .success(function(data: string[]) { 41 $scope.metrics = data || []; 42 }); 43 var start = moment().utc().subtract(parseDuration($scope.time)); 44 function parseDuration(v: string) { 45 var pattern = /(\d+)(d|y|n|h|m|s)-ago/; 46 var m = pattern.exec(v); 47 return moment.duration(parseInt(m[1]), m[2].replace('n', 'M')) 48 } 49 $http.get('/api/metadata/get?tagk=host&tagv=' + encodeURIComponent($scope.host)) 50 .success((data: any) => { 51 $scope.metadata = _.filter(data, function(i: any) { 52 return moment.utc(i.Time) > start; 53 }); 54 }); 55 var autods = '&autods=100'; 56 var cpu_r = new GraphRequest(); 57 cpu_r.start = $scope.time; 58 cpu_r.queries = [ 59 new Query(false, { 60 metric: 'os.cpu', 61 derivative: 'counter', 62 tags: { host: $scope.host }, 63 }) 64 ]; 65 $http.get('/api/graph?' + 'json=' + encodeURIComponent(JSON.stringify(cpu_r)) + autods) 66 .success((data: any) => { 67 if (!data.Series) { 68 return; 69 } 70 data.Series[0].Name = 'Percent Used'; 71 $scope.cpu = data.Series; 72 }); 73 var mem_r = new GraphRequest(); 74 mem_r.start = $scope.time; 75 mem_r.queries.push(new Query(false, { 76 metric: "os.mem.total", 77 tags: { host: $scope.host }, 78 })); 79 mem_r.queries.push(new Query(false, { 80 metric: "os.mem.used", 81 tags: { host: $scope.host }, 82 })); 83 $http.get('/api/graph?' + 'json=' + encodeURIComponent(JSON.stringify(mem_r)) + autods) 84 .success((data: any) => { 85 if (!data.Series) { 86 return; 87 } 88 data.Series[1].Name = "Used"; 89 $scope.mem_total = Math.max.apply(null, data.Series[0].Data.map((d: any) => { return d[1]; })); 90 $scope.mem = [data.Series[1]]; 91 }); 92 var net_bytes_r = new GraphRequest(); 93 net_bytes_r.start = $scope.time; 94 net_bytes_r.queries = [ 95 new Query(false, { 96 metric: "os.net.bytes", 97 rate: true, 98 rateOptions: { counter: true, resetValue: 1 }, 99 tags: { host: $scope.host, iface: "*", direction: "*" }, 100 }) 101 ]; 102 $http.get('/api/graph?' + 'json=' + encodeURIComponent(JSON.stringify(net_bytes_r)) + autods) 103 .success((data: any) => { 104 if (!data.Series) { 105 return; 106 } 107 var tmp = []; 108 var ifaceSeries = {}; 109 angular.forEach(data.Series, function(series, idx) { 110 series.Data = series.Data.map((dp: any) => { return [dp[0], dp[1] * 8]; }); 111 if (series.Tags.direction == "out") { 112 series.Data = series.Data.map((dp: any) => { return [dp[0], dp[1] * -1]; }); 113 } 114 if (!ifaceSeries.hasOwnProperty(series.Tags.iface)) { 115 ifaceSeries[series.Tags.iface] = [series]; 116 } else { 117 ifaceSeries[series.Tags.iface].push(series); 118 tmp.push(ifaceSeries[series.Tags.iface]); 119 } 120 }); 121 $scope.idata = tmp; 122 }); 123 var fs_r = new GraphRequest(); 124 fs_r.start = $scope.time 125 fs_r.queries = [ 126 new Query(false, { 127 metric: "os.disk.fs.space_total", 128 tags: { host: $scope.host, disk: "*"}, 129 }), 130 new Query(false, { 131 metric: "os.disk.fs.space_used", 132 tags: { host: $scope.host, disk: "*"}, 133 }) 134 ]; 135 $http.get('/api/graph?' + 'json=' + encodeURIComponent(JSON.stringify(fs_r)) + autods) 136 .success((data: any) => { 137 if (!data.Series) { 138 return; 139 } 140 var tmp = []; 141 var fsSeries = {}; 142 angular.forEach(data.Series, function(series, idx) { 143 var stat = series.Data[series.Data.length-1][1]; 144 var prop = ""; 145 if (series.Metric == "os.disk.fs.space_total") { 146 prop = "total"; 147 } else { 148 prop = "used"; 149 } 150 if (!fsSeries.hasOwnProperty(series.Tags.disk)) { 151 fsSeries[series.Tags.disk] = [series]; 152 fsSeries[series.Tags.disk][prop] = stat; 153 } else { 154 fsSeries[series.Tags.disk].push(series); 155 fsSeries[series.Tags.disk][prop] = stat; 156 tmp.push(fsSeries[series.Tags.disk]); 157 } 158 }); 159 $scope.fsdata = tmp; 160 }); 161 }]);