bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/static/js/put.ts (about) 1 class Tag { 2 k: string; 3 v: string; 4 } 5 6 class DP { 7 k: any; 8 v: any; 9 } 10 11 interface IPutScope extends ng.IScope { 12 error: string; 13 running: string; 14 success: string; 15 metrics: string[]; 16 metric: string; 17 tags: Tag[]; 18 dps: DP[]; 19 Submit: () => void; 20 GetTagKByMetric: () => void; 21 AddTag: () => void; 22 AddDP: () => void; 23 } 24 25 bosunControllers.controller('PutCtrl', ['$scope', '$http', '$route', function($scope: IPutScope, $http: ng.IHttpService, $route: ng.route.IRouteService) { 26 $scope.tags = [new Tag]; 27 var dp = new DP; 28 dp.k = moment().utc().format(); 29 $scope.dps = [dp]; 30 $http.get('/api/metric') 31 .success(function(data: string[]) { 32 $scope.metrics = data; 33 }) 34 .error(function(error) { 35 $scope.error = 'Unable to fetch metrics: ' + error; 36 }); 37 $scope.Submit = () => { 38 var data: any = []; 39 var tags: any = {}; 40 angular.forEach($scope.tags, (v, k) => { 41 if (v.k || v.v) { 42 tags[v.k] = v.v; 43 } 44 }); 45 angular.forEach($scope.dps, (v, k) => { 46 if (v.k && v.v) { 47 var ts = parseInt(moment.utc(v.k, tsdbDateFormat).format('X')); 48 data.push({ 49 metric: $scope.metric, 50 timestamp: ts, 51 value: parseFloat(v.v), 52 tags: tags, 53 }); 54 } 55 }); 56 $scope.running = 'submitting data...'; 57 $scope.success = ''; 58 $scope.error = ''; 59 $http.post('/api/put', data) 60 .success(() => { 61 $scope.running = ''; 62 $scope.success = 'Data Submitted'; 63 }) 64 .error((error: any) => { 65 $scope.running = ''; 66 $scope.error = error.error.message; 67 }); 68 } 69 $scope.AddTag = () => { 70 var last = $scope.tags[$scope.tags.length - 1]; 71 if (last.k && last.v) { 72 $scope.tags.push(new Tag); 73 } 74 } 75 $scope.AddDP = () => { 76 var last = $scope.dps[$scope.dps.length - 1]; 77 if (last.k && last.v) { 78 var dp = new DP; 79 dp.k = moment.utc(last.k, tsdbDateFormat).add(15, 'seconds').format(); 80 $scope.dps.push(dp); 81 } 82 } 83 $scope.GetTagKByMetric = () => { 84 $http.get('/api/tagk/' + $scope.metric) 85 .success(function(data: string[]) { 86 if (!angular.isArray(data)) { 87 return; 88 } 89 $scope.tags = [new Tag]; 90 for (var i = 0; i < data.length; i++) { 91 var t = new Tag; 92 t.k = data[i]; 93 $scope.tags.push(t); 94 } 95 }) 96 .error(function(error) { 97 $scope.error = 'Unable to fetch metrics: ' + error; 98 }); 99 }; 100 }]);