Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Widget -- Chart Service |
| 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | // injected references |
| 24 | // fs -> FnService |
| 25 | // wss -> WebSocketService |
| 26 | // ls -> LoadingService |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 27 | var $log, $interval, fs, wss, ls; |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 28 | |
| 29 | // constants |
| 30 | var refreshInterval = 2000; |
| 31 | |
| 32 | // example params to buildChart: |
| 33 | // { |
| 34 | // scope: $scope, <- controller scope |
| 35 | // tag: 'device', <- chart identifier |
| 36 | // respCb: respCb, <- websocket response callback (optional) |
| 37 | // query: params <- query parameters in URL (optional) |
| 38 | // } |
| 39 | // Note: query is always an object (empty or containing properties) |
| 40 | // it comes from $location.search() |
| 41 | function buildChart(o) { |
| 42 | var handlers = {}, |
| 43 | root = o.tag + 's', |
| 44 | req = o.tag + 'DataRequest', |
| 45 | resp = o.tag + 'DataResponse', |
| 46 | onResp = fs.isF(o.respCb), |
| 47 | oldChartData = [], |
| 48 | refreshPromise; |
| 49 | |
| 50 | o.scope.chartData = []; |
| 51 | o.scope.changedData = []; |
| 52 | o.scope.reqParams = o.reqParams || {}; |
| 53 | o.scope.autoRefresh = true; |
Simon Hunt | 94f36fc | 2017-07-12 17:47:17 -0700 | [diff] [blame] | 54 | o.scope.autoRefreshTip = o.lion_toggle_auto_refresh || 'Toggle auto refresh'; |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 55 | |
| 56 | // === websocket functions === |
| 57 | // response |
| 58 | function respCb(data) { |
| 59 | ls.stop(); |
| 60 | o.scope.chartData = data[root]; |
Jian Li | 2d8d3d6 | 2016-05-04 16:59:01 -0700 | [diff] [blame] | 61 | o.scope.annots = data.annots; |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 62 | onResp && onResp(); |
| 63 | |
| 64 | // check if data changed |
| 65 | if (!angular.equals(o.scope.chartData, oldChartData)) { |
| 66 | o.scope.changedData = []; |
| 67 | // only refresh the chart if there are new changes |
| 68 | if (oldChartData.length) { |
| 69 | angular.forEach(o.scope.chartData, function (item) { |
| 70 | if (!fs.containsObj(oldChartData, item)) { |
| 71 | o.scope.changedData.push(item); |
| 72 | } |
| 73 | }); |
| 74 | } |
| 75 | angular.copy(o.scope.chartData, oldChartData); |
| 76 | } |
| 77 | o.scope.$apply(); |
| 78 | } |
| 79 | handlers[resp] = respCb; |
| 80 | wss.bindHandlers(handlers); |
| 81 | |
| 82 | // request |
| 83 | function requestCb(params) { |
| 84 | var p = angular.extend({}, params, o.query); |
| 85 | if (wss.isConnected()) { |
| 86 | wss.sendEvent(req, p); |
| 87 | ls.start(); |
| 88 | } |
| 89 | } |
| 90 | o.scope.requestCallback = requestCb; |
| 91 | |
| 92 | // === autoRefresh functions === |
| 93 | function fetchDataIfNotWaiting() { |
| 94 | if (!ls.waiting()) { |
| 95 | if (fs.debugOn('widget')) { |
| 96 | $log.debug('Refreshing ' + root + ' page'); |
| 97 | } |
| 98 | requestCb(o.scope.reqParams); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function startRefresh() { |
| 103 | refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval); |
| 104 | } |
| 105 | |
| 106 | function stopRefresh() { |
| 107 | if (refreshPromise) { |
| 108 | $interval.cancel(refreshPromise); |
| 109 | refreshPromise = null; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function toggleRefresh() { |
| 114 | o.scope.autoRefresh = !o.scope.autoRefresh; |
| 115 | o.scope.autoRefresh ? startRefresh() : stopRefresh(); |
| 116 | } |
| 117 | o.scope.toggleRefresh = toggleRefresh; |
| 118 | |
| 119 | // === Cleanup on destroyed scope === |
| 120 | o.scope.$on('$destroy', function () { |
| 121 | wss.unbindHandlers(handlers); |
| 122 | stopRefresh(); |
| 123 | ls.stop(); |
| 124 | }); |
| 125 | |
| 126 | requestCb(o.scope.reqParams); |
| 127 | startRefresh(); |
| 128 | } |
| 129 | |
| 130 | angular.module('onosWidget') |
| 131 | .factory('ChartBuilderService', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 132 | ['$log', '$interval', 'FnService', 'WebSocketService', |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 133 | 'LoadingService', |
| 134 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 135 | function (_$log_, _$interval_, _fs_, _wss_, _ls_) { |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 136 | $log = _$log_; |
| 137 | $interval = _$interval_; |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 138 | fs = _fs_; |
| 139 | wss = _wss_; |
| 140 | ls = _ls_; |
| 141 | |
| 142 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 143 | buildChart: buildChart, |
Jian Li | a9dd019 | 2016-04-18 23:15:17 -0700 | [diff] [blame] | 144 | }; |
| 145 | }]); |
| 146 | }()); |