Set all cross references of policy/pap
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / StatisticsRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021-2022 Bell Canada. 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
23 package org.onap.policy.pap.main.rest;
24
25 import java.time.Instant;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.models.pdp.concepts.PdpStatistics;
31 import org.onap.policy.pap.main.service.PdpStatisticsService;
32 import org.springframework.context.annotation.Profile;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.RestController;
35
36 /**
37  * Class to provide REST endpoints for PAP component statistics.
38  *
39  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40  */
41 @RestController
42 @RequiredArgsConstructor
43 @Profile("default")
44 public class StatisticsRestControllerV1 extends PapRestControllerV1
45     implements StatisticsRestControllerV1Api {
46
47     private final PdpStatisticsService pdpStatisticsService;
48
49     /**
50      * get statistics of PAP.
51      *
52      *
53      * @return a response
54      */
55     @Override
56     public ResponseEntity<StatisticsReport> statistics(UUID requestId) {
57         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
58             .body(pdpStatisticsService.fetchCurrentStatistics());
59     }
60
61     /**
62      * get all statistics of PDP groups.
63      *
64      * @return a response
65      */
66     @Override
67     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpStatistics(
68             UUID requestId,
69             Integer recordCount,
70             Long startTime,
71             Long endTime) {
72         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(pdpStatisticsService
73             .fetchDatabaseStatistics(recordCount.intValue(), convertEpochtoInstant(startTime),
74                     convertEpochtoInstant(endTime)));
75     }
76
77
78     /**
79      * get all statistics of a PDP group.
80      *
81      * @param groupName name of the PDP group
82      * @return a response
83      */
84     @Override
85     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpGroupStatistics(
86             String groupName,
87             UUID requestId,
88             Integer recordCount,
89             Long startTime,
90             Long endTime) {
91         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
92             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, recordCount.intValue(),
93                     convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
94     }
95
96     /**
97      * get all statistics of sub PDP group.
98      *
99      * @param groupName name of the PDP group
100      * @param subType type of the sub PDP group
101      * @return a response
102      */
103     @Override
104     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpSubGroupStatistics(
105             String groupName,
106             String subType,
107             UUID requestId,
108             Integer recordCount,
109             Long startTime,
110             Long endTime) {
111         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
112             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, recordCount.intValue(),
113                 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
114     }
115
116     /**
117      * get all statistics of one PDP.
118      *
119      * @param groupName name of the PDP group
120      * @param subType type of the sub PDP group
121      * @param pdpName the name of the PDP
122      * @param recordCount the count of the query response, optional, default return all statistics stored
123      * @return a response
124      */
125     @Override
126     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpInstanceStatistics(
127             String groupName,
128             String subType,
129             String pdpName,
130             UUID requestId,
131             Integer recordCount,
132             Long startTime,
133             Long endTime) {
134         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
135             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, pdpName, recordCount.intValue(),
136                 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
137     }
138
139     private Instant convertEpochtoInstant(Long epochSecond) {
140         return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
141     }
142
143 }