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 int numOfSamples = samples.size();
54 List<MeasurementObject> result = new ArrayList<>();
55 samples.forEach(sample ->
56 sample.forEach(cellMeasObj -> {
57 int index = result.indexOf(cellMeasObj);
59 result.set(index, findSum(result.get(index), cellMeasObj));
62 result.add(cellMeasObj);
66 return findAvg(result, numOfSamples);
72 public MeasurementObject findSum(MeasurementObject existing, MeasurementObject current) {
73 pmNames.forEach(pmName -> {
74 int newValue = current.getPmData().get(pmName) + existing.getPmData().get(pmName);
75 existing.getPmData().put(pmName, newValue);
81 * Calculate the average
83 public List<MeasurementObject> findAvg(List<MeasurementObject> result, int numOfSamples) {
84 result.forEach(cellMeasObj ->
85 pmNames.forEach(pmName -> {
86 int value = (cellMeasObj.getPmData().get(pmName))/numOfSamples;
87 cellMeasObj.getPmData().put(pmName, value);
90 log.debug("Average of measurement data samples {}",result);