SO Monitoring UI
[so.git] / so-monitoring / so-monitoring-ui / src / main / frontend / src / app / model / searchData.model.ts
1 import { ToastrNotificationService } from "../toastr-notification-service.service";\r
2 \r
3 /**\r
4 ============LICENSE_START=======================================================\r
5  Copyright (C) 2018 Ericsson. All rights reserved.\r
6 ================================================================================\r
7 Licensed under the Apache License, Version 2.0 (the "License");\r
8 you may not use this file except in compliance with the License.\r
9 You may obtain a copy of the License at\r
10 \r
11     http://www.apache.org/licenses/LICENSE-2.0\r
12 \r
13 Unless required by applicable law or agreed to in writing, software\r
14 distributed under the License is distributed on an "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 See the License for the specific language governing permissions and\r
17  limitations under the License.\r
18 \r
19 SPDX-License-Identifier: Apache-2.0\r
20 ============LICENSE_END=========================================================\r
21 \r
22 @authors: ronan.kenny@ericsson.com, waqas.ikram@ericsson.com\r
23 */\r
24 \r
25 import { Observable, throwError, of } from 'rxjs';\r
26 import { SearchRequest } from "./SearchRequest.model";\r
27 export class SearchData {\r
28 \r
29   public selectedValueSII = "EQ";\r
30   public selectedValueRI = "EQ";\r
31   public selectedValueSN = "EQ";\r
32   public selectedValueSTATUS = "ALL";\r
33 \r
34   private now = Date.now();\r
35   // Minus 1 hour from current time for start date\r
36   public startDate = new Date(this.now - (1 * 60 * 60 * 1000));\r
37   public selectedStartHour = this.getNumberAsString(this.startDate.getHours());\r
38   public selectedStartMinute = this.getNumberAsString(this.startDate.getMinutes());\r
39 \r
40   public endDate = new Date(this.now);\r
41   public selectedEndHour = this.getNumberAsString(this.endDate.getHours());\r
42   public selectedEndMinute = this.getNumberAsString(this.endDate.getMinutes());\r
43 \r
44 \r
45   public serviceInstanceId: string;\r
46   public requestId: string;\r
47   public serviceInstanceName: string;\r
48 \r
49   private startTimeInMilliseconds: number;\r
50   private endTimeInMilliseconds: number;\r
51 \r
52   constructor() {\r
53   }\r
54 \r
55   public getSearchRequest(): Observable<SearchRequest> {\r
56     var searchFields = {};\r
57     if ((!this.startDate || this.startDate === null) || (!this.endDate || this.endDate === null)) {\r
58       console.error("Found either start time or end time null or undefined");\r
59       return throwError("Found end or start date empty, Please enter start and end date");\r
60     }\r
61 \r
62     this.startDate.setHours(parseInt(this.selectedStartHour));\r
63     this.startDate.setMinutes(parseInt(this.selectedStartMinute));\r
64 \r
65     this.endDate.setHours(parseInt(this.selectedEndHour));\r
66     this.endDate.setMinutes(parseInt(this.selectedEndMinute));\r
67 \r
68     this.startTimeInMilliseconds = this.startDate.getTime();\r
69     this.endTimeInMilliseconds = this.endDate.getTime();\r
70 \r
71     if (this.startTimeInMilliseconds > this.endTimeInMilliseconds) {\r
72       console.error("End time: " + this.endDate + " can not be greater then start time: " + this.startDate);\r
73       return throwError("End time: " + this.endDate + " can not be greater then start time: " + this.startDate);\r
74     }\r
75 \r
76 \r
77     if (!this.isEmpty(this.selectedValueSII) && !this.isEmpty(this.serviceInstanceId)) {\r
78       searchFields["serviceInstanceId"] = [this.selectedValueSII, this.serviceInstanceId]\r
79     }\r
80     if (!this.isEmpty(this.selectedValueRI) && !this.isEmpty(this.requestId)) {\r
81       searchFields["requestId"] = [this.selectedValueRI, this.requestId]\r
82     }\r
83     if (!this.isEmpty(this.selectedValueSN) && !this.isEmpty(this.serviceInstanceName)) {\r
84       searchFields["serviceInstanceName"] = [this.selectedValueSN, this.serviceInstanceName]\r
85     }\r
86 \r
87     if (!this.isEmpty(this.selectedValueSTATUS) && this.selectedValueSTATUS !== "ALL") {\r
88       searchFields["requestStatus"] = ["EQ", this.selectedValueSTATUS]\r
89     }\r
90 \r
91     return of(new SearchRequest(searchFields, this.startTimeInMilliseconds, this.endTimeInMilliseconds));\r
92   }\r
93 \r
94   private isEmpty(str) {\r
95     return (!str || 0 === str.length);\r
96   }\r
97 \r
98   private getNumberAsString(num: number) {\r
99     if (num <= 9) {\r
100       return "0" + num;\r
101     }\r
102     return "" + num;\r
103   }\r
104 \r
105 }\r