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