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