Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / performanceHistoryApp / src / pluginPerformance.tsx
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import React from 'react';
20 import { Redirect, Route, RouteComponentProps, Switch, withRouter } from 'react-router-dom';
21
22 import { connect, Connect, IDispatcher } from '../../../framework/src/flux/connect';
23 import applicationManager from '../../../framework/src/services/applicationManager';
24 // eslint-disable-next-line @typescript-eslint/no-unused-vars
25 import { IApplicationStoreState } from '../../../framework/src/store/applicationStore';
26 import { ApplicationStore } from '../../../framework/src/store/applicationStore';
27
28 import { updateMountIdActionCreator } from './actions/deviceListActions';
29 import { ResetLtpsAction } from './actions/ltpAction';
30 import { ReloadAction } from './actions/reloadAction';
31 import { ResetAllSubViewsAction } from './actions/toggleActions';
32 import performanceHistoryRootHandler from './handlers/performanceHistoryRootHandler';
33 import { PmDataInterval } from './models/performanceDataType';
34 import PerformanceHistoryApplication from './views/performanceHistoryApplication';
35
36 const appIcon = require('./assets/icons/performanceHistoryAppIcon.svg');  // select app icon
37
38 let api: {
39   readonly applicationStore: ApplicationStore | null;
40   readonly applicationStoreInitialized: Promise<ApplicationStore>;
41 };
42
43 const mapProps = () => ({
44 });
45
46 const mapDisp = (dispatcher: IDispatcher) => ({
47   updateMountId: (mountId: string) => dispatcher.dispatch(updateMountIdActionCreator(mountId)),
48   resetLtps: () => dispatcher.dispatch(new ResetLtpsAction()),
49   resetSubViews: () => dispatcher.dispatch(new ResetAllSubViewsAction()),
50   setScheduleReload: (show: boolean) => dispatcher.dispatch(new ReloadAction(show)),
51 });
52
53 let currentMountId: string | null = null;
54 let lastUrl: string = '/performanceHistory';
55 const PerformanceHistoryApplicationRouteAdapter = connect(mapProps, mapDisp)((props: RouteComponentProps<{ mountId?: string }> & Connect<typeof mapProps, typeof mapDisp>) => {
56   let mountId: string = '';
57
58   const getMountId = (last_url: string) => {
59     let index = last_url.lastIndexOf('performanceHistory/');
60     if (index >= 0) {
61       mountId = last_url.substring(index + 19);
62     } else {
63       mountId = '';
64     }
65
66     return mountId;
67   };
68
69   const scheduleReload = (current_mount_id: string) => {
70     props.updateMountId(current_mount_id);
71     props.resetLtps();
72     props.resetSubViews();
73     props.setScheduleReload(true);
74   };
75   
76   // called when component finished mounting
77   React.useEffect(() => {
78
79     lastUrl = props.location.pathname;
80     mountId = getMountId(lastUrl);
81
82     if (currentMountId !== mountId) { // new element is loaded
83       currentMountId = mountId;
84       scheduleReload(currentMountId);
85     } else
86     if (currentMountId !== '') { // same element is loaded again
87       scheduleReload(currentMountId);
88     }
89   }, []);
90
91   // called when component gets updated
92   React.useEffect(() => {
93
94     lastUrl = props.location.pathname;
95     mountId = getMountId(lastUrl);
96
97     if (currentMountId !== mountId) {
98       currentMountId = mountId;
99       scheduleReload(currentMountId);
100     }
101
102   });
103
104   return (
105     <PerformanceHistoryApplication />
106   );
107 });
108
109 const PerformanceHistoryRouterApp = withRouter((props: RouteComponentProps) => {
110   props.history.action = 'POP';
111   return (
112     <Switch>
113       <Route path={`${props.match.path}/:mountId`} component={PerformanceHistoryApplicationRouteAdapter} />
114       <Route path={`${props.match.path}`} component={PerformanceHistoryApplicationRouteAdapter} />
115       <Redirect to={`${props.match.path}`} />
116     </Switch>
117   );
118 });
119
120 export function register() {
121   api = applicationManager.registerApplication({
122     name: 'performanceHistory',
123     icon: appIcon,
124     rootComponent: PerformanceHistoryRouterApp,
125     rootActionHandler: performanceHistoryRootHandler,
126     menuEntry: 'Performance',
127   });
128 }
129
130 export function setPmDataInterval(pmDataInterval: PmDataInterval): boolean {
131   let reload: boolean = true;
132   if (api && api.applicationStore) {
133     if (api.applicationStore.state.performanceHistory.pmDataIntervalType !== pmDataInterval) {
134       reload = true;
135     }
136     api.applicationStore.state.performanceHistory.pmDataIntervalType = pmDataInterval;
137   }
138   return reload;
139 }
140
141
142 export function getPmDataInterval(): PmDataInterval {
143   let result = api && api.applicationStore
144     ? api.applicationStore.state.performanceHistory.pmDataIntervalType
145     : PmDataInterval.pmInterval15Min;
146   return result ? result : PmDataInterval.pmInterval15Min;
147 }