Upgrade and clean up dependencies
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / service / PdpStatisticsService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Bell Canada. All rights reserved.
4  * Modifications Copyright (C) 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
33 import lombok.RequiredArgsConstructor;
34 import org.onap.policy.common.parameters.BeanValidationResult;
35 import org.onap.policy.common.utils.services.Registry;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.pdp.concepts.PdpStatistics;
38 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
39 import org.onap.policy.pap.main.PapConstants;
40 import org.onap.policy.pap.main.repository.PdpStatisticsRepository;
41 import org.onap.policy.pap.main.rest.PapStatisticsManager;
42 import org.onap.policy.pap.main.rest.StatisticsReport;
43 import org.onap.policy.pap.main.startstop.PapActivator;
44 import org.springframework.data.domain.PageRequest;
45 import org.springframework.data.domain.Pageable;
46 import org.springframework.data.domain.Sort;
47 import org.springframework.stereotype.Service;
48 import org.springframework.transaction.annotation.Transactional;
49
50 @Service
51 @Transactional
52 @RequiredArgsConstructor
53 public class PdpStatisticsService {
54
55     private static final String TIMESTAMP = "timeStamp";
56     private static final int DEFAULT_RECORD_COUNT = 10;
57     private static final int MAX_RECORD_COUNT = 100;
58
59     private final PdpStatisticsRepository pdpStatisticsRepository;
60
61     /**
62      * Returns the current statistics of pap component.
63      *
64      * @return Report containing statistics of pap component
65      */
66     public StatisticsReport fetchCurrentStatistics() {
67         final var report = new StatisticsReport();
68         report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500);
69
70         PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
71         report.setTotalPdpCount(mgr.getTotalPdpCount());
72         report.setTotalPdpGroupCount(mgr.getTotalPdpGroupCount());
73         report.setTotalPolicyDownloadCount(mgr.getTotalPolicyDownloadCount());
74         report.setPolicyDownloadSuccessCount(mgr.getPolicyDownloadSuccessCount());
75         report.setPolicyDownloadFailureCount(mgr.getPolicyDownloadFailureCount());
76         report.setTotalPolicyDeployCount(mgr.getTotalPolicyDeployCount());
77         report.setPolicyDeploySuccessCount(mgr.getPolicyDeploySuccessCount());
78         report.setPolicyDeployFailureCount(mgr.getPolicyDeployFailureCount());
79
80         return report;
81     }
82
83     /**
84      * Creates PDP statistics.
85      *
86      * @param pdpStatisticsList a specification of the PDP statistics to create
87      * @return the PDP statistics created
88      */
89     public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList) {
90         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
91             var jpaPdpStatistics = new JpaPdpStatistics();
92             jpaPdpStatistics.fromAuthorative(pdpStatistics);
93             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
94             if (!validationResult.isValid()) {
95                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
96             }
97             pdpStatisticsRepository.saveAndFlush(jpaPdpStatistics);
98             pdpStatistics.setGeneratedId(jpaPdpStatistics.getGeneratedId());
99         }
100
101         // Return the created PDP statistics
102         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
103
104         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
105             @SuppressWarnings("deprecation")
106             var jpaPdpStatistics = pdpStatisticsRepository.getById(pdpStatisticsItem.getGeneratedId());
107             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
108         }
109         return pdpStatistics;
110     }
111
112     /**
113      * Fetch PdpStatistics from db.
114      *
115      * @param pdpGroup the name of the group
116      * @param pdpSubGroup the name of the subgroup
117      * @param pdp the pdp instance id
118      * @param recordCount the number of records to return
119      * @param startTime start time of the records to be returned
120      * @param endTime end time of the records to be returned
121      * @return pdpStatistics grouped by pdpGroup
122      */
123     public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(@NonNull String pdpGroup,
124         @NonNull String pdpSubGroup, @NonNull String pdp, int recordCount, Instant startTime, Instant endTime) {
125
126         Pageable recordSize = getRecordSize(recordCount);
127         if (startTime != null && endTime != null) {
128             return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository
129                 .findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampBetween(pdpGroup, pdpSubGroup, pdp,
130                     convertInstantToDate(startTime), convertInstantToDate(endTime), recordSize)));
131         } else if (startTime == null && endTime == null) {
132             return generatePdpStatistics(
133                 asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndName(pdpGroup,
134                     pdpSubGroup, pdp, recordSize)));
135         } else if (startTime != null) {
136             return generatePdpStatistics(asPdpStatisticsList(
137                 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampGreaterThanEqual(
138                     pdpGroup, pdpSubGroup, pdp, convertInstantToDate(startTime), recordSize)));
139         } else {
140             return generatePdpStatistics(asPdpStatisticsList(
141                 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampLessThanEqual(
142                     pdpGroup, pdpSubGroup, pdp, convertInstantToDate(endTime), recordSize)));
143         }
144     }
145
146     /**
147      * Fetch PdpStatistics from db.
148      *
149      * @param pdpGroup the name of the group
150      * @param pdpSubGroup the name of the subgroup
151      * @param recordCount the number of records to return
152      * @param startTime start time of the records to be returned
153      * @param endTime end time of the records to be returned
154      * @return pdpStatistics grouped by pdpGroup
155      */
156     public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(@NonNull String pdpGroup,
157         @NonNull String pdpSubGroup, int recordCount, Instant startTime, Instant endTime) {
158
159         Pageable recordSize = getRecordSize(recordCount);
160         if (startTime != null && endTime != null) {
161             return generatePdpStatistics(asPdpStatisticsList(
162                 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndTimeStampBetween(pdpGroup, pdpSubGroup,
163                     convertInstantToDate(startTime), convertInstantToDate(endTime), recordSize)));
164         } else if (startTime == null && endTime == null) {
165             return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository
166                 .findByPdpGroupNameAndPdpSubGroupName(pdpGroup, pdpSubGroup, recordSize)));
167         } else if (startTime != null) {
168             return generatePdpStatistics(asPdpStatisticsList(
169                 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndTimeStampGreaterThanEqual(pdpGroup,
170                     pdpSubGroup, convertInstantToDate(startTime), recordSize)));
171         } else {
172             return generatePdpStatistics(asPdpStatisticsList(
173                 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndTimeStampLessThanEqual(pdpGroup,
174                     pdpSubGroup, convertInstantToDate(endTime), recordSize)));
175         }
176     }
177
178     /**
179      * Fetch PdpStatistics from db.
180      *
181      * @param pdpGroup the name of the group
182      * @param recordCount the number of records to return
183      * @param startTime start time of the records to be returned
184      * @param endTime end time of the records to be returned
185      * @return pdpStatistics grouped by pdpGroup
186      */
187     public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(@NonNull String pdpGroup,
188         int recordCount, Instant startTime, Instant endTime) {
189
190         Pageable recordSize = getRecordSize(recordCount);
191         if (startTime != null && endTime != null) {
192             return generatePdpStatistics(
193                 asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndTimeStampBetween(pdpGroup,
194                     convertInstantToDate(startTime), convertInstantToDate(endTime), recordSize)));
195         } else if (startTime == null && endTime == null) {
196             return generatePdpStatistics(
197                 asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupName(pdpGroup, recordSize)));
198         } else if (startTime != null) {
199             return generatePdpStatistics(
200                 asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndTimeStampGreaterThanEqual(pdpGroup,
201                     convertInstantToDate(startTime), recordSize)));
202         } else {
203             return generatePdpStatistics(
204                 asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndTimeStampLessThanEqual(pdpGroup,
205                     convertInstantToDate(endTime), recordSize)));
206         }
207     }
208
209     /**
210      * Fetch PdpStatistics from db.
211      *
212      * @param recordCount the number of records to return
213      * @param startTime start time of the records to be returned
214      * @param endTime end time of the records to be returned
215      * @return pdpStatistics grouped by pdpGroup
216      */
217     public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(int recordCount, Instant startTime,
218         Instant endTime) {
219
220         Pageable recordSize = getRecordSize(recordCount);
221         if (startTime != null && endTime != null) {
222             return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository.findByTimeStampBetween(
223                 convertInstantToDate(startTime), convertInstantToDate(endTime), recordSize)));
224         } else if (startTime == null && endTime == null) {
225             return generatePdpStatistics(
226                 asPdpStatisticsList(pdpStatisticsRepository.findAll(recordSize).toList()));
227         } else if (startTime != null) {
228             return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository
229                 .findByTimeStampGreaterThanEqual(convertInstantToDate(startTime), recordSize)));
230         } else {
231             return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository
232                 .findByTimeStampLessThanEqual(convertInstantToDate(endTime), recordSize)));
233         }
234     }
235
236     private Pageable getRecordSize(int recordCount) {
237         if (recordCount < 1) {
238             recordCount = DEFAULT_RECORD_COUNT;
239         } else if (recordCount > MAX_RECORD_COUNT) {
240             recordCount = MAX_RECORD_COUNT;
241         }
242         return PageRequest.of(0, recordCount, Sort.by(TIMESTAMP).descending());
243     }
244
245     /**
246      * generate the statistics of pap component by group/subgroup.
247      *
248      */
249     private Map<String, Map<String, List<PdpStatistics>>> generatePdpStatistics(List<PdpStatistics> pdpStatisticsList) {
250         Map<String, Map<String, List<PdpStatistics>>> groupMap = new HashMap<>();
251         if (pdpStatisticsList != null) {
252             pdpStatisticsList.stream().forEach(s -> {
253                 String curGroup = s.getPdpGroupName();
254                 String curSubGroup = s.getPdpSubGroupName();
255                 groupMap.computeIfAbsent(curGroup, curGroupMap -> new HashMap<>())
256                     .computeIfAbsent(curSubGroup, curSubGroupList -> new ArrayList<>()).add(s);
257             });
258         }
259         return groupMap;
260     }
261
262     /**
263      * Convert JPA PDP statistics list to an PDP statistics list.
264      *
265      * @param jpaPdpStatisticsList the list to convert
266      * @return the PDP statistics list
267      */
268     private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
269         return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());
270     }
271
272     private Date convertInstantToDate(Instant instant) {
273         return (instant == null ? null : Date.from(instant));
274     }
275 }