RIFT-15726 - optimize download size -> lodash usage in UI
[osm/UI.git] / skyquake / framework / widgets / skyquake_container / skyquakeContainerStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 //This will reach out to the global routes endpoint
19
20 import Alt from './skyquakeAltInstance.js';
21 import SkyquakeContainerSource from './skyquakeContainerSource.js';
22 import SkyquakeContainerActions from './skyquakeContainerActions';
23 import _indexOf from 'lodash/indexOf';
24 //Temporary, until api server is on same port as webserver
25 import rw from 'utils/rw.js';
26
27 var API_SERVER = rw.getSearchParams(window.location).api_server;
28 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
29
30 class SkyquakeContainerStore {
31 constructor() {
32 this.currentPlugin = getCurrentPlugin();
33 this.nav = {};
34 this.notifications = [];
35 this.socket = null;
36 //Notification defaults
37 this.notificationMessage = '';
38 this.displayNotification = false;
39 this.notificationType = 'error';
40 //Screen Loader default
41 this.displayScreenLoader = false;
42 this.bindActions(SkyquakeContainerActions);
43 this.exportAsync(SkyquakeContainerSource);
44
45
46 this.exportPublicMethods({
47 // getNav: this.getNav
48 });
49
50 }
51 getSkyquakeNavSuccess = (data) => {
52 var self = this;
53 this.setState({
54 nav: decorateAndTransformNav(data, self.currentPlugin)
55 })
56 }
57
58 closeSocket = () => {
59 if (this.socket) {
60 window.multiplexer.channel(this.channelId).close();
61 }
62 this.setState({
63 socket: null
64 });
65 }
66 //Remove once logging plugin is implemented
67 getSysLogViewerURLSuccess(data){
68 window.open(data.url);
69 }
70 getSysLogViewerURLError(data){
71 console.log('failed', data)
72 }
73
74 openNotificationsSocketLoading = () => {
75 this.setState({
76 isLoading: true
77 })
78 }
79
80 openNotificationsSocketSuccess = (data) => {
81 var self = this;
82
83 let connection = data.connection;
84 let streamSource = data.streamSource;
85 console.log('Success opening notification socket for stream ', streamSource);
86
87 let ws = window.multiplexer.channel(connection);
88
89 if (!connection) return;
90 self.setState({
91 socket: ws.ws,
92 isLoading: false,
93 channelId: connection
94 });
95
96 ws.onmessage = (socket) => {
97 try {
98 var data = JSON.parse(socket.data);
99 if (!data.notification) {
100 console.warn('No notification in the received payload: ', data);
101 } else {
102 // Temp to test before adding multi-sources
103 data.notification.source = streamSource;
104 if (_indexOf(self.notifications, data.notification) == -1) {
105 // newly appreared event.
106 // Add to the notifications list and setState
107 self.notifications.unshift(data.notification);
108 self.setState({
109 newNotificationEvent: true,
110 newNotificationMsg: data.notification,
111 notifications: self.notifications,
112 isLoading: false
113 });
114 }
115 }
116 } catch(e) {
117 console.log('Error in parsing data on notification socket');
118 }
119 };
120
121 ws.onclose = () => {
122 self.closeSocket();
123 };
124 }
125
126 openNotificationsSocketError = (data) => {
127 console.log('Error opening notification socket', data);
128 }
129
130 getEventStreamsLoading = () => {
131 this.setState({
132 isLoading: true
133 });
134 }
135
136 getEventStreamsSuccess = (streams) => {
137 console.log('Found streams: ', streams);
138 let self = this;
139
140 streams['ietf-restconf-monitoring:streams'] &&
141 streams['ietf-restconf-monitoring:streams']['stream'] &&
142 streams['ietf-restconf-monitoring:streams']['stream'].map((stream) => {
143 stream['access'] && stream['access'].map((streamLocation) => {
144 if (streamLocation['encoding'] == 'ws_json') {
145 setTimeout(() => {
146 self.getInstance().openNotificationsSocket(streamLocation['location'], stream['name']);
147 }, 0);
148 }
149 })
150 })
151
152 this.setState({
153 isLoading: true,
154 streams: streams
155 })
156 }
157
158 getEventStreamsError = (error) => {
159 console.log('Failed to get streams object');
160 this.setState({
161 isLoading: false
162 })
163 }
164
165 //Notifications
166 showNotification = (data) => {
167 let state = {
168 displayNotification: true,
169 notificationMessage: data,
170 notificationType: 'error',
171 displayScreenLoader: false
172 }
173 if(typeof(data) == 'string') {
174
175 } else {
176 state.notificationMessage = data.msg;
177 if(data.type) {
178 state.notificationType = data.type;
179 }
180 }
181 this.setState(state);
182 }
183 hideNotification = () => {
184 this.setState({
185 displayNotification: false
186 })
187 }
188 //ScreenLoader
189 showScreenLoader = () => {
190 this.setState({
191 displayScreenLoader: true
192 });
193 }
194 hideScreenLoader = () => {
195 this.setState({
196 displayScreenLoader: false
197 })
198 }
199
200 }
201
202 /**
203 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
204 * @param {object} nav Nav item from /nav endoingpoint
205 * @param {string} currentPlugin Current plugin name taken from url path.
206 * @return {array} Returns list of constructed nav items.
207 */
208 function decorateAndTransformNav(nav, currentPlugin) {
209 for ( let k in nav) {
210 nav[k].pluginName = k;
211 if (k != currentPlugin) {
212 nav[k].routes.map(function(route, i) {
213 if (API_SERVER) {
214 route.route = '/' + k + '/index.html?api_server=' + API_SERVER + '&upload_server=' + UPLOAD_SERVER + '#' + route.route;
215 } else {
216 route.route = '/' + k + '/#' + route.route;
217 }
218 route.isExternal = true;
219 })
220 }
221 }
222 return nav;
223 }
224
225 function getCurrentPlugin() {
226 var paths = window.location.pathname.split('/');
227 var currentPath = null;
228 if (paths[0] != "") {
229 currentPath = paths[0]
230 } else {
231 currentPath = paths[1];
232 }
233 if (currentPath != null) {
234 return currentPath;
235 } else {
236 console.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
237 }
238 }
239
240 export default Alt.createStore(SkyquakeContainerStore, 'SkyquakeContainerStore');