2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.persistence.provider;
23 import java.time.Instant;
24 import java.util.List;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response.Status;
28 import lombok.AllArgsConstructor;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
31 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAcElementStatistics;
32 import org.onap.policy.clamp.models.acm.persistence.repository.AcElementStatisticsRepository;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfReferenceTimestampKey;
35 import org.onap.policy.models.dao.PfFilterParameters;
36 import org.springframework.stereotype.Service;
37 import org.springframework.transaction.annotation.Transactional;
40 * This class provides the provision of information on automation composition element statistics in the database to
43 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
48 public class AcElementStatisticsProvider {
50 private AcElementStatisticsRepository acElementStatisticsRepository;
53 * Creates automation composition element statistics.
55 * @param acElementStatisticsList a specification of the statistics to create
56 * @return the Element statistics created
57 * @throws PfModelException on initiation errors
59 public List<AcElementStatistics> createAcElementStatistics(
60 @NonNull final List<AcElementStatistics> acElementStatisticsList) throws PfModelException {
63 var jpaAcElementStatisticsList = ProviderUtils.getJpaAndValidateList(acElementStatisticsList,
64 JpaAcElementStatistics::new, "automation composition element statistics");
66 var jpaAcElementStatisticsSaved = acElementStatisticsRepository.saveAll(jpaAcElementStatisticsList);
68 // Return the saved automation composition element statistics
69 return asAcElementStatisticsList(jpaAcElementStatisticsSaved);
70 } catch (IllegalArgumentException e) {
71 throw new PfModelException(Status.BAD_REQUEST, "Error in save automation composition element statistics",
77 * Convert JPA acElement statistics list to acElement statistics list.
79 * @param jpaAcElementStatistics the list to convert
80 * @return the acElement statistics list
82 private List<AcElementStatistics> asAcElementStatisticsList(List<JpaAcElementStatistics> jpaAcElementStatistics) {
83 return jpaAcElementStatistics.stream().map(JpaAcElementStatistics::toAuthorative).collect(Collectors.toList());
87 * Get acElement statistics.
89 * @param name the name of the participant
90 * @param version version of the participant
91 * @param id of the automation composition element
92 * @param timestamp timestamp of the statistics
93 * @return the acElement statistics found
95 @Transactional(readOnly = true)
96 public List<AcElementStatistics> getAcElementStatistics(final String name, final String version, final String id,
97 final Instant timestamp) {
98 if (name != null && version != null && timestamp != null && id != null) {
99 return asAcElementStatisticsList(acElementStatisticsRepository
100 .findAllById(List.of(new PfReferenceTimestampKey(name, version, id, timestamp))));
101 } else if (name != null) {
102 return getFilteredAcElementStatistics(name, version, null, null, null, "DESC", 0);
104 return asAcElementStatisticsList(acElementStatisticsRepository.findAll());
108 * Get filtered acElement statistics.
110 * @param name the acElement name for the statistics to get
111 * @param version the acElement version for the statistics to get
112 * @param startTimeStamp startTimeStamp to filter statistics
113 * @param endTimeStamp endTimeStamp to filter statistics
114 * @param sortOrder sortOrder to query database
115 * @param getRecordNum Total query count from database
116 * @param filterMap the filters to apply to the get operation
117 * @return the acElement statistics found
119 @Transactional(readOnly = true)
120 public List<AcElementStatistics> getFilteredAcElementStatistics(final String name, final String version,
121 final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap, final String sortOrder,
122 final int getRecordNum) {
125 PfFilterParameters filterParams = PfFilterParameters
129 .startTime(startTimeStamp)
130 .endTime(endTimeStamp)
131 .filterMap(filterMap)
132 .sortOrder(sortOrder)
133 .recordNum(getRecordNum)
136 return asAcElementStatisticsList(
137 acElementStatisticsRepository.getFiltered(JpaAcElementStatistics.class, filterParams));