Minor functional fixes in odlux apps
[ccsdk/features.git] / sdnr / wt / 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 * 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 { TransmissionPowerDataType } from '../models/transmissionPowerDataType';
27 import { IDataSet, IDataSetsObject } from '../models/chartTypes';
28 import { createTransmissionPowerProperties, createTransmissionPowerActions } from '../handlers/transmissionPowerHandler';
29 import { lineChart, sortDataByTimeStamp } from '../utils/chartUtils';
30 import { addColumnLabels } from '../utils/tableUtils';
31
32 const mapProps = (state: IApplicationStoreState) => ({
33   transmissionPowerProperties: createTransmissionPowerProperties(state),
34 });
35
36 const mapDisp = (dispatcher: IDispatcher) => ({
37   transmissionPowerActions: createTransmissionPowerActions(dispatcher.dispatch),
38 });
39
40 type TransmissionPowerComponentProps = RouteComponentProps & Connect<typeof mapProps, typeof mapDisp> & {
41   selectedTimePeriod: string
42 }
43
44 const TransmissionPowerTable = MaterialTable as MaterialTableCtorType<TransmissionPowerDataType>;
45
46 /**
47  * The Component which gets the transmission power data from the database based on the selected time period.
48  */
49 class TransmissionPowerComponent extends React.Component<TransmissionPowerComponentProps>{
50   render(): JSX.Element {
51     const properties = this.props.transmissionPowerProperties
52     const actions = this.props.transmissionPowerActions
53
54     const chartPagedData = this.getChartDataValues(properties.rows);
55
56     const transmissionColumns: ColumnModel<TransmissionPowerDataType>[] = [
57       { property: "radioSignalId", title: "Radio signal", type: ColumnType.text },
58       { property: "scannerId", title: "Scanner ID", type: ColumnType.text },
59       { property: "utcTimeStamp", title: "End Time", type: ColumnType.text, disableFilter: true },
60       {
61         property: "suspectIntervalFlag", title: "Suspect Interval", type: ColumnType.custom, customControl: ({ rowData }) => {
62           const suspectIntervalFlag = rowData["suspectIntervalFlag"].toString();
63           return <div >{suspectIntervalFlag} </div>
64         }
65       }
66     ];
67
68     chartPagedData.datasets.forEach(ds => {
69       transmissionColumns.push(addColumnLabels<TransmissionPowerDataType>(ds.name, ds.columnLabel));
70     });
71
72     return (
73       <>
74         {lineChart(chartPagedData)}
75         <TransmissionPowerTable idProperty={"_id"} columns={transmissionColumns} {...properties} {...actions} />
76       </>
77     );
78   };
79
80   /**
81    * This function gets the performance values for TransmissionPower according on the chartjs dataset structure 
82    * which is to be sent to the chart.
83    */
84
85   private getChartDataValues = (rows: TransmissionPowerDataType[]): IDataSetsObject => {
86     const _rows = [...rows];
87     sortDataByTimeStamp(_rows);
88
89     const datasets: IDataSet[] = [{
90       name: "txLevelMin",
91       label: "tx-level-min",
92       borderColor: '#0e17f3de',
93       bezierCurve: false,
94       lineTension: 0,
95       fill: false,
96       data: [],
97       columnLabel: "Tx min"
98     }, {
99       name: "txLevelAvg",
100       label: "tx-level-avg",
101       borderColor: '#08edb6de',
102       bezierCurve: false,
103       lineTension: 0,
104       fill: false,
105       data: [],
106       columnLabel: "Tx avg"
107     }, {
108       name: "txLevelMax",
109       label: "tx-level-max",
110       borderColor: '#b308edde',
111       bezierCurve: false,
112       lineTension: 0,
113       fill: false,
114       data: [],
115       columnLabel: "Tx max"
116     }];
117
118     _rows.forEach(row => {
119       datasets.forEach(ds => {
120         ds.data.push({
121           x: row["utcTimeStamp" as keyof TransmissionPowerDataType] as string,
122           y: row[ds.name as keyof TransmissionPowerDataType] as string
123         });
124       });
125     });
126     return {
127       datasets: datasets
128     };
129   }
130 }
131
132 const TransmissionPower = withRouter(connect(mapProps, mapDisp)(TransmissionPowerComponent));
133 export default TransmissionPower;