update odlux stage 3
[ccsdk/features.git] / sdnr / wt / odlux / apps / performanceHistoryApp / src / components / signalToInterference.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 import * as React from 'react';
19
20 import { withRouter, RouteComponentProps } from 'react-router-dom';
21
22 import { MaterialTable, ColumnType, ColumnModel, MaterialTableCtorType } from '../../../../framework/src/components/material-table';
23 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
24 import connect, { Connect, IDispatcher } from '../../../../framework/src/flux/connect';
25
26 import { SignalToInterferenceDataType, SignalToInterferenceDatabaseDataType } from '../models/signalToInteferenceDataType';
27 import { IDataSet, IDataSetsObject } from '../models/chartTypes';
28 import { createSignalToInterferenceProperties, createSignalToInterferenceActions } from '../handlers/signalToInterferenceHandler';
29 import { lineChart, sortDataByTimeStamp } from '../utils/chartUtils';
30 import { addColumnLabels } from '../utils/tableUtils';
31 import { SetSubViewAction, SetFilterVisibility } from '../actions/toggleActions';
32 import ToggleContainer from './toggleContainer';
33
34 const mapProps = (state: IApplicationStoreState) => ({
35   signalToInterferenceProperties: createSignalToInterferenceProperties(state),
36   currentView: state.performanceHistory.subViews.SINR.subView,
37   isFilterVisible: state.performanceHistory.subViews.SINR.isFilterVisible,
38   existingFilter: state.performanceHistory.signalToInterference.filter
39 });
40
41 const mapDisp = (dispatcher: IDispatcher) => ({
42   signalToInterferenceActions: createSignalToInterferenceActions(dispatcher.dispatch),
43   setSubView: (value: "chart" | "table") => dispatcher.dispatch(new SetSubViewAction("SINR", value)),
44   toggleFilterButton: (value: boolean) => { dispatcher.dispatch(new SetFilterVisibility("SINR", value)) },
45 });
46
47 type SignalToInterferenceComponentProps = RouteComponentProps & Connect<typeof mapProps, typeof mapDisp> & {
48   selectedTimePeriod: string
49 };
50
51 const SignalToInterferenceTable = MaterialTable as MaterialTableCtorType<SignalToInterferenceDataType>;
52
53 /**
54  * The Component which gets the signal to interference data from the database based on the selected time period.
55  */
56 class SignalToInterferenceComponent extends React.Component<SignalToInterferenceComponentProps>{
57
58   onToggleFilterButton = () => {
59     this.props.toggleFilterButton(!this.props.isFilterVisible);
60   }
61
62   onChange = (value: "chart" | "table") => {
63     this.props.setSubView(value);
64   }
65
66   render(): JSX.Element {
67     const properties = this.props.signalToInterferenceProperties;
68     const actions = this.props.signalToInterferenceActions;
69
70     const chartPagedData = this.getChartDataValues(properties.rows);
71
72     const sinrColumns: ColumnModel<SignalToInterferenceDataType>[] = [
73       { property: "radioSignalId", title: "Radio signal", type: ColumnType.text },
74       { property: "scannerId", title: "Scanner ID", type: ColumnType.text },
75       { property: "timeStamp", title: "End Time", type: ColumnType.text },
76       {
77         property: "suspectIntervalFlag", title: "Suspect Interval", type: ColumnType.boolean
78       }
79     ];
80
81     chartPagedData.datasets.forEach(ds => {
82       sinrColumns.push(addColumnLabels<SignalToInterferenceDataType>(ds.name, ds.columnLabel));
83     });
84     return (
85       <>
86         <ToggleContainer onToggleFilterButton={this.onToggleFilterButton} showFilter={this.props.isFilterVisible} existingFilter={this.props.signalToInterferenceProperties.filter} onFilterChanged={this.props.signalToInterferenceActions.onFilterChanged} selectedValue={this.props.currentView} onChange={this.onChange}>
87           {lineChart(chartPagedData)}
88           <SignalToInterferenceTable stickyHeader idProperty={"_id"} columns={sinrColumns} {...properties} {...actions}
89           />
90         </ToggleContainer>
91       </>
92     );
93   };
94
95   /**
96    * This function gets the performance values for SINR according on the chartjs dataset structure 
97    * which is to be sent to the chart.
98    */
99
100   private getChartDataValues = (rows: SignalToInterferenceDataType[]): IDataSetsObject => {
101     const _rows = [...rows];
102     sortDataByTimeStamp(_rows);
103
104     const datasets: IDataSet[] = [{
105       name: "snirMin",
106       label: "snir-min",
107       borderColor: '#0e17f3de',
108       bezierCurve: false,
109       lineTension: 0,
110       fill: false,
111       data: [],
112       columnLabel: "SINR (min)[db]"
113     }, {
114       name: "snirAvg",
115       label: "snir-avg",
116       borderColor: '#08edb6de',
117       bezierCurve: false,
118       lineTension: 0,
119       fill: false,
120       data: [],
121       columnLabel: "SINR (avg)[db]"
122     }, {
123       name: "snirMax",
124       label: "snir-max",
125       borderColor: '#b308edde',
126       bezierCurve: false,
127       lineTension: 0,
128       fill: false,
129       data: [],
130       columnLabel: "SINR (max)[db]"
131     }];
132
133     _rows.forEach(row => {
134       row.snirMin = row.performanceData.snirMin;
135       row.snirAvg = row.performanceData.snirAvg;
136       row.snirMax = row.performanceData.snirMax;
137       datasets.forEach(ds => {
138         ds.data.push({
139           x: row["timeStamp" as keyof SignalToInterferenceDataType] as string,
140           y: row.performanceData[ds.name as keyof SignalToInterferenceDatabaseDataType] as string
141         });
142       });
143     });
144     return {
145       datasets: datasets
146     };
147   }
148 }
149
150 const SignalToInterference = withRouter(connect(mapProps, mapDisp)(SignalToInterferenceComponent));
151 export default SignalToInterference;