blob: dc8e2ba4d4fbd9f6da8829915fad0aef7489d8ba [file] [log] [blame]
Jian Lia9dd0192016-04-18 23:15:17 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Lia9dd0192016-04-18 23:15:17 -07003 *
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 Burrows1c2a9682017-07-14 16:52:46 +010027 var $log, $interval, fs, wss, ls;
Jian Lia9dd0192016-04-18 23:15:17 -070028
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 Hunt94f36fc2017-07-12 17:47:17 -070054 o.scope.autoRefreshTip = o.lion_toggle_auto_refresh || 'Toggle auto refresh';
Jian Lia9dd0192016-04-18 23:15:17 -070055
56 // === websocket functions ===
57 // response
58 function respCb(data) {
59 ls.stop();
60 o.scope.chartData = data[root];
Jian Li2d8d3d62016-05-04 16:59:01 -070061 o.scope.annots = data.annots;
Jian Lia9dd0192016-04-18 23:15:17 -070062 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 Burrows1c2a9682017-07-14 16:52:46 +0100132 ['$log', '$interval', 'FnService', 'WebSocketService',
Jian Lia9dd0192016-04-18 23:15:17 -0700133 'LoadingService',
134
Steven Burrows1c2a9682017-07-14 16:52:46 +0100135 function (_$log_, _$interval_, _fs_, _wss_, _ls_) {
Jian Lia9dd0192016-04-18 23:15:17 -0700136 $log = _$log_;
137 $interval = _$interval_;
Jian Lia9dd0192016-04-18 23:15:17 -0700138 fs = _fs_;
139 wss = _wss_;
140 ls = _ls_;
141
142 return {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100143 buildChart: buildChart,
Jian Lia9dd0192016-04-18 23:15:17 -0700144 };
145 }]);
146}());