Add statistics for Distribution Set (#1388)

* Add Statistics for Rollouts and Actions count by Status for a Distribution Set

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* remove unused imports

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Refactoring and additional statistics for auto assignments

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Fixed review findings and added tests

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Added tests for the Management API

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Remove unused imports

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* refactoring

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

---------

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>
This commit is contained in:
Denislav Prinov
2023-07-11 08:58:52 +03:00
committed by GitHub
parent 2fe73ee40c
commit 3402808ee9
11 changed files with 505 additions and 2 deletions

View File

@@ -0,0 +1,94 @@
/**
* Copyright (c) 2023 Bosch.IO GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.mgmt.json.model.distributionset;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.Map;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MgmtDistributionSetStatistics {
private static final String TOTAL = "total";
@JsonProperty
private Map<String, Long> totalActionsPerStatus;
@JsonProperty
private Map<String, Long> totalRolloutsPerStatus;
@JsonProperty
private Long totalAutoAssignments;
private MgmtDistributionSetStatistics() {
// Private constructor to enforce the use of the builder pattern
}
public Map<String, Long> getTotalActionsPerStatus() {
return totalActionsPerStatus;
}
public Map<String, Long> getTotalRolloutsPerStatus() {
return totalRolloutsPerStatus;
}
public Long getTotalAutoAssignments() {
return totalAutoAssignments;
}
public static class Builder {
private Map<String, Long> totalActionsPerStatus;
private Map<String, Long> totalRolloutsPerStatus;
private Long totalAutoAssignments;
public Builder() {
totalActionsPerStatus = new HashMap<>();
totalRolloutsPerStatus = new HashMap<>();
}
public Builder addTotalActionPerStatus(String status, Long count) {
totalActionsPerStatus.put(status, count);
return this;
}
public Builder addTotalRolloutPerStatus(String status, Long count) {
totalRolloutsPerStatus.put(status, count);
return this;
}
public Builder addTotalAutoAssignments(Long count) {
totalAutoAssignments = count;
return this;
}
public MgmtDistributionSetStatistics build() {
MgmtDistributionSetStatistics statistics = new MgmtDistributionSetStatistics();
statistics.totalActionsPerStatus = calculateTotalWithStatus(totalActionsPerStatus);
statistics.totalRolloutsPerStatus = calculateTotalWithStatus(totalRolloutsPerStatus);
statistics.totalAutoAssignments = totalAutoAssignments;
return statistics;
}
private Map<String, Long> calculateTotalWithStatus(Map<String, Long> statusMap) {
if (statusMap.isEmpty()) {
return null;
}
long total = statusMap.values().stream().mapToLong(Long::longValue).sum();
statusMap.put(TOTAL, total);
return statusMap;
}
}
}

View File

@@ -18,6 +18,7 @@ import org.eclipse.hawkbit.mgmt.json.model.PagedList;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtDistributionSet;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtDistributionSetRequestBodyPost;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtDistributionSetRequestBodyPut;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtDistributionSetStatistics;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtInvalidateDistributionSetRequestBody;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtTargetAssignmentRequestBody;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtTargetAssignmentResponseBody;
@@ -397,6 +398,62 @@ public interface MgmtDistributionSetRestApi {
@RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_PAGING_LIMIT, defaultValue = MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_LIMIT) int pagingLimitParam,
@RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_SORTING, required = false) String sortParam);
/**
* Handles the GET request of retrieving Rollouts count by Status
* for Distribution Set.
*
* @param distributionSetId
* the ID of the set to retrieve
*
* @return a DistributionSetStatistics with status OK.
*
*/
@GetMapping(value = MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{distributionSetId}/statistics/rollouts", produces = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<MgmtDistributionSetStatistics> getRolloutsCountByStatusForDistributionSet(@PathVariable("distributionSetId") Long distributionSetId);
/**
* Handles the GET request of retrieving Actions count by Status
* for Distribution Set.
*
* @param distributionSetId
* the ID of the set to retrieve
*
* @return a DistributionSetStatistics with status OK.
*
*/
@GetMapping(value = MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{distributionSetId}/statistics/actions", produces = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<MgmtDistributionSetStatistics> getActionsCountByStatusForDistributionSet(@PathVariable("distributionSetId") Long distributionSetId);
/**
* Handles the GET request of retrieving Auto Assignments count
* for Distribution Set.
*
* @param distributionSetId
* the ID of the set to retrieve
*
* @return a DistributionSetStatistics with status OK.
*
*/
@GetMapping(value = MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{distributionSetId}/statistics/autoassignments", produces = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<MgmtDistributionSetStatistics> getAutoAssignmentsCountForDistributionSet(@PathVariable("distributionSetId") Long distributionSetId);
/**
* Handles the GET request of retrieving Rollouts, Actions and
* Auto Assignments counts by Status for Distribution Set.
*
* @param distributionSetId
* the ID of the set to retrieve
*
* @return a DistributionSetStatistics with status OK.
*
*/
@GetMapping(value = MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{distributionSetId}/statistics", produces = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<MgmtDistributionSetStatistics> getStatisticsForDistributionSet(@PathVariable("distributionSetId") Long distributionSetId);
/**
* Invalidates a distribution set
*