Merge "YANG Model update for A1 Adapter"
[ccsdk/features.git] / sdnr / wt / 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 * as React from "react";
20 import { faBook } from '@fortawesome/free-solid-svg-icons';
21
22 import applicationManager from '../../../framework/src/services/applicationManager';
23
24 import { withRouter, RouteComponentProps, Route, Switch, Redirect } from 'react-router-dom';
25 import performanceHistoryRootHandler from './handlers/performanceHistoryRootHandler';
26 import { PmDataInterval } from './models/performanceDataType';
27 import PerformanceHistoryApplication from './views/performanceHistoryApplication';
28 import { ApplicationStore } from '../../../framework/src/store/applicationStore';
29
30 import connect, { Connect, IDispatcher } from '../../../framework/src/flux/connect';
31 import { IApplicationStoreState } from "../../../framework/src/store/applicationStore";
32 import { updateMountIdActionCreator } from "./actions/deviceListActions";
33
34 let api: {
35   readonly applicationStore: ApplicationStore | null;
36   readonly applicationStoreInitialized: Promise<ApplicationStore>;
37 }
38
39 const mapProps = (state: IApplicationStoreState) => ({
40 });
41
42 const mapDisp = (dispatcher: IDispatcher) => ({
43   updateMountId: (mountId: string) => dispatcher.dispatch(updateMountIdActionCreator(mountId))
44 });
45
46 let currentMountId: string | null = null;
47 let lastUrl: string = "/performanceHistory";
48 const PerformanceHistoryApplicationRouteAdapter = connect(mapProps, mapDisp)((props: RouteComponentProps<{ mountId?: string}> & Connect<typeof mapProps, typeof mapDisp>) => {
49   let mountId: string = "";
50   if (props.location.pathname !== lastUrl) {
51     // ensure the asynchronus update will only be called once per path
52     lastUrl = props.location.pathname;
53     let index = lastUrl.lastIndexOf("performanceHistory/");
54     if(index >= 0) {
55       mountId = lastUrl.substr(index+19);
56     } else {
57       mountId = "";
58     }
59
60     window.setTimeout(async () => {
61       // check if the mountId has changed
62       if (currentMountId !== mountId) {
63         currentMountId = mountId;
64         await props.updateMountId(currentMountId);
65       }
66     });
67   }
68   return (
69     <PerformanceHistoryApplication />
70   );
71 });
72
73 const PerformanceHistoryRouterApp = withRouter((props: RouteComponentProps) => {
74   props.history.action = "POP";
75   return (
76   <Switch>
77     <Route path={`${props.match.path}/:mountId`} component={PerformanceHistoryApplicationRouteAdapter} />
78     <Route path={`${props.match.path}`} component={PerformanceHistoryApplicationRouteAdapter} />
79     <Redirect to={`${props.match.path}`} />
80   </Switch>
81   )
82 });
83
84 export function register() {
85   api = applicationManager.registerApplication({
86     name: "performanceHistory",
87     icon: faBook,
88     rootComponent: PerformanceHistoryRouterApp,
89     rootActionHandler: performanceHistoryRootHandler,
90     menuEntry: "Performance"
91   });
92 }
93
94 export function setPmDataInterval(pmDataInterval: PmDataInterval): boolean {
95   let reload: boolean = true;
96   if (api && api.applicationStore) {
97     if (api.applicationStore.state.performanceHistory.pmDataIntervalType !== pmDataInterval) {
98       reload = true;
99     }
100     api.applicationStore.state.performanceHistory.pmDataIntervalType = pmDataInterval;
101   }
102   return reload;
103 }
104
105
106 export function getPmDataInterval(): PmDataInterval {
107   let result = api && api.applicationStore
108     ? api.applicationStore.state.performanceHistory.pmDataIntervalType
109     : PmDataInterval.pmInterval15Min;
110   return result ? result : PmDataInterval.pmInterval15Min;
111 }