All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.imunity.furms.ui.charts.service.CommunityAllocUsageSeriesGenerator Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/*
 * Copyright (c) 2020 Bixbit s.c. All rights reserved.
 * See LICENSE file for licensing information.
 */

package io.imunity.furms.ui.charts.service;

import io.imunity.furms.domain.project_allocation.ProjectAllocationId;
import io.imunity.furms.domain.resource_usage.ResourceUsage;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BinaryOperator;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

@Component
class CommunityAllocUsageSeriesGenerator {

	List prepareYValuesForCommunityAllocationUsageLine(List xArguments,
	                                                           Set allResourceUsageHistory) {
		Collection> allocationsUsageValuesByProbedAtDates =
			prepareAllocationsUsageValuesByProbedAtDates(allResourceUsageHistory);
		List> yValuesForEachProjectAllocation = allocationsUsageValuesByProbedAtDates.stream()
			.map(usageValuesAtTime -> prepareYPointsForUsageLine(xArguments, usageValuesAtTime))
			.collect(toList());

		return sumToOneLine(xArguments, yValuesForEachProjectAllocation);
	}

	private Collection> prepareAllocationsUsageValuesByProbedAtDates(Set allResourceUsageHistory) {
		Map> usageDataByAllocationId = allResourceUsageHistory.stream()
			.collect(
				groupingBy(
					usage -> usage.projectAllocationId,
					collectingAndThen(
						toMap(usage ->
								usage.utcProbedAt.toLocalDate(),
							identity(),
							getOlderUsageMerger()
						),
						usageMap -> usageMap.entrySet().stream()
							.collect(toMap(Map.Entry::getKey,
								entry -> entry.getValue().cumulativeConsumption.doubleValue()))
					))
			);
		return usageDataByAllocationId.values();

	}

	private BinaryOperator getOlderUsageMerger() {
		return (usage, usage1) -> usage.utcProbedAt.isAfter(usage1.utcProbedAt) ? usage : usage1;
	}

	private List prepareYPointsForUsageLine(List xAxisPoints,
	                                                       Map usageValuesAtTime) {
		double lastValue = 0;
		List yValues = new ArrayList<>();
		for (LocalDate xPoint : xAxisPoints) {
			double value = usageValuesAtTime.getOrDefault(xPoint, lastValue);
			yValues.add(value);
			lastValue = value;
		}
		return yValues;
	}

	private List sumToOneLine(List xArguments, List> yValesForEachProjectAllocation) {
		List values = new ArrayList<>();
		for (int i = 0; i < xArguments.size(); i++) {
			double tmp = 0;
			for (List yPoints : yValesForEachProjectAllocation) {
				tmp += yPoints.get(i);
			}
			values.add(tmp);
		}
		return values;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy