blob: e2f295954aed0b70f72e6a61eeaa63abbfb87dc1 [file] [log] [blame]
Jian Lia9dd0192016-04-18 23:15:17 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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
27 var $log, $interval, $timeout, fs, wss, ls;
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 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',
132 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
133 'LoadingService',
134
135 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) {
136 $log = _$log_;
137 $interval = _$interval_;
138 $timeout = _$timeout_;
139 fs = _fs_;
140 wss = _wss_;
141 ls = _ls_;
142
143 return {
144 buildChart: buildChart
145 };
146 }]);
147}());