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