Merge "Add SDN-R odlux performance"
[ccsdk/features.git] / sdnr / wt / odlux / apps / performanceHistoryApp / src / actions / ltpAction.ts
1 import { Action } from '../../../../framework/src/flux/action';
2 import { Dispatch } from '../../../../framework/src/flux/store';
3
4 import { Ltp } from '../models/availableLtps';
5 import { PerformanceHistoryService } from '../services/performanceHistoryService';
6
7 /** 
8  * Represents the base action. 
9  */
10 export class BaseAction extends Action { }
11
12 /** 
13  * Represents an action causing the store to load available ltps. 
14  */
15 export class LoadAllAvailableLtpsAction extends BaseAction { }
16
17 /** 
18  * Represents an action causing the store to update available ltps. 
19  */
20 export class AllAvailableLtpsLoadedAction extends BaseAction {
21   /**
22    * Initialize this instance.
23    * @param availableLtps The available ltps which are returned from the database.
24    */
25   constructor(public availableLtps: Ltp[] | null, public error?: string) {
26     super();
27   }
28 }
29
30
31 /** 
32  * Represents an asynchronous thunk action to load available distinctLtps by networkElement from the database and set the returned first Ltp as default. 
33  * @param networkElement The network element sent to database to get its available distinct Ltps.
34  * @param selectedTimePeriod The time period selected sent to database to get the distinct Ltps of the selected network element.
35  * @param selectedLtp The Ltp which is selected in the dropdown.
36  * @param selectFirstLtp The function to get the first ltp returned from the database to be selected as default on selection upon network element.
37  * @param resetLtp The function to verify if the selected ltp is also available in the selected time period database else reset the Ltp dropdown to select.
38  */
39 export const loadDistinctLtpsbyNetworkElementAsync = (networkElement: string, selectedTimePeriod: string, selectedLtp: string, selectFirstLtp?: Function, resetLtp?: Function) => (dispatch: Dispatch) => {
40   if (selectedTimePeriod == "15min") {
41     dispatch(new LoadAllAvailableLtpsAction());
42     PerformanceHistoryService.getDistinctLtpsFrom15minDatabase(networkElement).then(distinctLtps => {
43       if(distinctLtps) {
44         let ltpNotSelected: boolean = true;
45         selectFirstLtp && selectFirstLtp(distinctLtps[0].key);
46         distinctLtps.forEach((value: Ltp) => {
47           if(value.key === selectedLtp) {
48             ltpNotSelected = false;
49           }
50         });
51         resetLtp && resetLtp(ltpNotSelected);
52         dispatch(new AllAvailableLtpsLoadedAction(distinctLtps))
53       }
54     }).catch(error => {
55       dispatch(new AllAvailableLtpsLoadedAction(null, error));
56     });
57   } else {
58     dispatch(new LoadAllAvailableLtpsAction());
59     PerformanceHistoryService.getDistinctLtpsFrom24hoursDatabase(networkElement).then(distinctLtps => {
60       if(distinctLtps) {
61         let ltpNotSelected: boolean = true;
62         selectFirstLtp && selectFirstLtp(distinctLtps[0].key);
63         distinctLtps.forEach((value: Ltp) => {
64           if(value.key === selectedLtp) {
65             ltpNotSelected = false;
66           }
67         });
68         resetLtp && resetLtp(ltpNotSelected);
69         dispatch(new AllAvailableLtpsLoadedAction(distinctLtps))
70       }
71     }).catch(error => {
72       dispatch(new AllAvailableLtpsLoadedAction(null, error));
73     });
74   }
75 };