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