1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Wipro Limited.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
20 *******************************************************************************/
22 package org.onap.slice.analysis.ms.service;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.annotation.PostConstruct;
29 import org.onap.slice.analysis.ms.models.MeasurementObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
35 * This class has utility methods for calculating the average of samples
38 public class AverageCalculator {
39 private static Logger log = LoggerFactory.getLogger(AverageCalculator.class);
40 private List<String> pmNames;
44 pmNames = new ArrayList<>();
45 pmNames.add("PrbUsedDl");
46 pmNames.add("PrbUsedUl");
50 * Find average of samples
52 public List<MeasurementObject> findAverageOfSamples(List<List<MeasurementObject>> samples) {
53 log.debug("find average for samples {}", samples);
54 int numOfSamples = samples.size();
55 List<MeasurementObject> result = new ArrayList<>();
56 if(!samples.isEmpty()) {
57 for(List<MeasurementObject> sample : samples) {
58 for(MeasurementObject cellMeasObj : sample) {
59 int index = MeasurementObject.findIndex(cellMeasObj.getMeasurementObjectId(), result);
61 result.set(index, findSum(result.get(index), cellMeasObj));
64 result.add(cellMeasObj);
69 return findAvg(result, numOfSamples);
75 public MeasurementObject findSum(MeasurementObject existing, MeasurementObject current) {
76 pmNames.forEach(pmName -> {
77 int newValue = current.getPmData().get(pmName) + existing.getPmData().get(pmName);
78 existing.getPmData().put(pmName, newValue);
84 * Calculate the average
86 public List<MeasurementObject> findAvg(List<MeasurementObject> result, int numOfSamples) {
87 if(!result.isEmpty()) {
88 result.forEach(cellMeasObj ->
89 pmNames.forEach(pmName -> {
90 int value = (cellMeasObj.getPmData().get(pmName))/numOfSamples;
91 cellMeasObj.getPmData().put(pmName, value);
94 log.debug("Average of measurement data samples {}",result);