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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.service;
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;
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;
52 @RequiredArgsConstructor
53 public class PdpStatisticsService {
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;
59 private final PdpStatisticsRepository pdpStatisticsRepository;
62 * Returns the current statistics of pap component.
64 * @return Report containing statistics of pap component
66 public StatisticsReport fetchCurrentStatistics() {
67 final var report = new StatisticsReport();
68 report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500);
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());
84 * Creates PDP statistics.
86 * @param pdpStatisticsList a specification of the PDP statistics to create
87 * @return the PDP statistics created
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());
97 pdpStatisticsRepository.saveAndFlush(jpaPdpStatistics);
98 pdpStatistics.setGeneratedId(jpaPdpStatistics.getGeneratedId());
101 // Return the created PDP statistics
102 List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
104 for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
105 @SuppressWarnings("deprecation")
106 var jpaPdpStatistics = pdpStatisticsRepository.getById(pdpStatisticsItem.getGeneratedId());
107 pdpStatistics.add(jpaPdpStatistics.toAuthorative());
109 return pdpStatistics;
113 * Fetch PdpStatistics from db.
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
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) {
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)));
140 return generatePdpStatistics(asPdpStatisticsList(
141 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampLessThanEqual(
142 pdpGroup, pdpSubGroup, pdp, convertInstantToDate(endTime), recordSize)));
147 * Fetch PdpStatistics from db.
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
156 public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(@NonNull String pdpGroup,
157 @NonNull String pdpSubGroup, int recordCount, Instant startTime, Instant endTime) {
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)));
172 return generatePdpStatistics(asPdpStatisticsList(
173 pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndTimeStampLessThanEqual(pdpGroup,
174 pdpSubGroup, convertInstantToDate(endTime), recordSize)));
179 * Fetch PdpStatistics from db.
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
187 public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(@NonNull String pdpGroup,
188 int recordCount, Instant startTime, Instant endTime) {
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)));
203 return generatePdpStatistics(
204 asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndTimeStampLessThanEqual(pdpGroup,
205 convertInstantToDate(endTime), recordSize)));
210 * Fetch PdpStatistics from db.
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
217 public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(int recordCount, Instant startTime,
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)));
231 return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository
232 .findByTimeStampLessThanEqual(convertInstantToDate(endTime), recordSize)));
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;
242 return PageRequest.of(0, recordCount, Sort.by(TIMESTAMP).descending());
246 * generate the statistics of pap component by group/subgroup.
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);
263 * Convert JPA PDP statistics list to an PDP statistics list.
265 * @param jpaPdpStatisticsList the list to convert
266 * @return the PDP statistics list
268 private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
269 return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());
272 private Date convertInstantToDate(Instant instant) {
273 return (instant == null ? null : Date.from(instant));