|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var NG_DASH_API_ROOT = "https://ng-dash.appspot.com/_ah/api"; |
| 4 | +var clientId = '731555738015-hna1v9or40ml5saoqh0b87t3j6fh6juv.apps.googleusercontent.com'; |
| 5 | +var scopes = 'https://www.googleapis.com/auth/userinfo.email'; |
| 6 | + |
| 7 | +var G = { |
| 8 | + runs: [], |
| 9 | + errorMsg: "" |
| 10 | +}; |
| 11 | + |
| 12 | +function defaultCompare(a, b) { |
| 13 | + return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +function flattenNameValues(data, result, prefix) { |
| 18 | + result = result ? result : []; |
| 19 | + prefix = prefix ? prefix : ""; |
| 20 | + Object.keys(data).forEach(function(name) { |
| 21 | + var value = data[name]; |
| 22 | + if (value.__proto__ === Object.prototype) { |
| 23 | + flattenNameValues(value, result, prefix + name + "."); |
| 24 | + } else { |
| 25 | + result.push({name: prefix + name, value: value}); |
| 26 | + } |
| 27 | + }); |
| 28 | + result.sort(function(a, b) { |
| 29 | + return defaultCompare(a.name, b.name); |
| 30 | + }); |
| 31 | + return result; |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +function processRunData(data) { |
| 36 | + data.dimensions = flattenNameValues(JSON.parse(data.dimensions_json)); |
| 37 | + delete data.dimensions_json; |
| 38 | + data.metrics = flattenNameValues(JSON.parse(data.metrics_json)); |
| 39 | + delete data.metrics_json; |
| 40 | + if (data.children) { |
| 41 | + data.children.forEach(processRunData); |
| 42 | + } else { |
| 43 | + data.children = []; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function processRun(run) { |
| 48 | + if (run.data) { |
| 49 | + processRunData(run.data); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +function processRuns(runs) { |
| 55 | + runs.sort(function compareCreationTimestamp(a, b) { |
| 56 | + return b.creation_timestamp - a.creation_timestamp; |
| 57 | + }); |
| 58 | + runs.forEach(processRun); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +function onNgDashApiLoaded() { |
| 63 | + gapi.client.ngdash.run.listRuns({}).execute(function(resp) { |
| 64 | + if (!resp.code) { |
| 65 | + G.runs = resp.items; |
| 66 | + processRuns(G.runs); |
| 67 | + } else { |
| 68 | + console.error(resp.code); |
| 69 | + G.errorMsg = "Error loading runs: " + resp.code; |
| 70 | + } |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +function onGapiLoad() { |
| 76 | + gapi.client.load('ngdash', 'v0.1', onNgDashApiLoaded, NG_DASH_API_ROOT); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +var module = angular.module('ngDash', []); |
| 81 | + |
| 82 | +module.controller('MainController', ['$scope', function ($scope) { |
| 83 | + $scope.G = G; |
| 84 | + function onFrame() { |
| 85 | + $scope.$digest(); |
| 86 | + window.requestAnimationFrame(onFrame); |
| 87 | + }; |
| 88 | + window.requestAnimationFrame(onFrame); |
| 89 | +}]); |
| 90 | + |
| 91 | +module.directive('run', function() { |
| 92 | + return { |
| 93 | + scope: { run: "=" }, |
| 94 | + templateUrl: '/run.html' |
| 95 | + }; |
| 96 | +}); |
| 97 | + |
| 98 | +module.directive('runData', function() { |
| 99 | + return { |
| 100 | + scope: { data: "=runData" }, |
| 101 | + templateUrl: '/runData.html' |
| 102 | + }; |
| 103 | +}); |
| 104 | + |
| 105 | +module.directive('nameValues', function() { |
| 106 | + return { |
| 107 | + scope: { data: "=nameValues", }, |
| 108 | + templateUrl: '/nameValues.html' |
| 109 | + }; |
| 110 | +}); |
| 111 | + |
| 112 | +module.filter('utcTimestampToLocalDate', function() { |
| 113 | + return function utcTimestampToLocalDate(timestamp) { |
| 114 | + return new Date(timestamp * 1000); |
| 115 | + }; |
| 116 | +}); |
0 commit comments