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:
@@ -34,6 +34,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetTag;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetTagAssignmentResult;
|
||||
import org.eclipse.hawkbit.repository.model.MetaData;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.Statistic;
|
||||
import org.eclipse.hawkbit.repository.model.Tag;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -517,6 +518,45 @@ public interface DistributionSetManagement
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
|
||||
long countByTypeId(long typeId);
|
||||
|
||||
/**
|
||||
* Count all {@link org.eclipse.hawkbit.repository.model.Rollout}s by status for
|
||||
* Distribution Set.
|
||||
*
|
||||
* @param dsId
|
||||
* to look for
|
||||
*
|
||||
* @return List of Statistics for {@link org.eclipse.hawkbit.repository.model.Rollout}s status counts
|
||||
*
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
|
||||
List<Statistic> countRolloutsByStatusForDistributionSet(@NotNull Long dsId);
|
||||
|
||||
/**
|
||||
* Count all {@link org.eclipse.hawkbit.repository.model.Action}s by status for
|
||||
* Distribution Set.
|
||||
*
|
||||
* @param dsId
|
||||
* to look for
|
||||
*
|
||||
* @return List of Statistics for {@link org.eclipse.hawkbit.repository.model.Action}s status counts
|
||||
*
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
|
||||
List<Statistic> countActionsByStatusForDistributionSet(@NotNull Long dsId);
|
||||
|
||||
/**
|
||||
* Count all {@link org.eclipse.hawkbit.repository.builder.AutoAssignDistributionSetUpdate}s
|
||||
* for Distribution Set.
|
||||
*
|
||||
* @param dsId
|
||||
* to look for
|
||||
*
|
||||
* @return number of {@link org.eclipse.hawkbit.repository.builder.AutoAssignDistributionSetUpdate}s
|
||||
*
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
|
||||
Long countAutoAssignmentsForDistributionSet(@NotNull Long dsId);
|
||||
|
||||
/**
|
||||
* Sets the specified {@link DistributionSet} as invalidated.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 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.repository.model;
|
||||
|
||||
public interface Statistic {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the key of the Statistic entity.
|
||||
*/
|
||||
Object getName();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the value of the Statistic entity.
|
||||
*/
|
||||
Object getData();
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import java.util.Optional;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaStatistic;
|
||||
import org.eclipse.hawkbit.repository.model.Action;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.Rollout;
|
||||
@@ -50,6 +51,37 @@ public interface DistributionSetRepository
|
||||
@Query(value = "Select Distinct ds from JpaDistributionSet ds join ds.tags dst where dst.id = :tag and ds.deleted = 0")
|
||||
Page<JpaDistributionSet> findByTag(Pageable pageable, @Param("tag") Long tagId);
|
||||
|
||||
/**
|
||||
* Count {@link Rollout}s by Status for Distribution set.
|
||||
*
|
||||
* @param dsId
|
||||
* to be found
|
||||
* @return map for {@link Rollout}s status counts
|
||||
*/
|
||||
@Query(value = "SELECT r.status as name, COUNT(r.status) as data FROM JpaRollout r WHERE r.distributionSet.id = :dsId GROUP BY r.status")
|
||||
List<JpaStatistic> countRolloutsByStatusForDistributionSet(@Param("dsId") Long dsId);
|
||||
|
||||
|
||||
/**
|
||||
* Count {@link Action}s by Status for Distribution set.
|
||||
*
|
||||
* @param dsId
|
||||
* to be found
|
||||
* @return map for {@link Action}s status counts
|
||||
*/
|
||||
@Query(value = "SELECT a.status as name, COUNT(a.status) as data FROM JpaAction a WHERE a.distributionSet.id = :dsId GROUP BY a.status")
|
||||
List<JpaStatistic> countActionsByStatusForDistributionSet(@Param("dsId") Long dsId);
|
||||
|
||||
/**
|
||||
* Count total AutoAssignments for Distribution set.
|
||||
*
|
||||
* @param dsId
|
||||
* to be found
|
||||
* @return number of Auto Assignments for Distribution set
|
||||
*/
|
||||
@Query(value = "SELECT COUNT(f.autoAssignDistributionSet) FROM JpaTargetFilterQuery f WHERE f.autoAssignDistributionSet.id = :dsId GROUP BY f.autoAssignDistributionSet")
|
||||
Long countAutoAssignmentsForDistributionSet(@Param("dsId") Long dsId);
|
||||
|
||||
/**
|
||||
* deletes the {@link DistributionSet}s with the given IDs.
|
||||
*
|
||||
|
||||
@@ -57,6 +57,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetTagAssignmentResult;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.MetaData;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.Statistic;
|
||||
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
|
||||
import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
@@ -160,6 +161,21 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
return distributionSetRepository.countByTypeId(typeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Statistic> countRolloutsByStatusForDistributionSet(Long dsId) {
|
||||
return distributionSetRepository.countRolloutsByStatusForDistributionSet(dsId).stream().map(Statistic.class::cast).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Statistic> countActionsByStatusForDistributionSet(Long dsId) {
|
||||
return distributionSetRepository.countActionsByStatusForDistributionSet(dsId).stream().map(Statistic.class::cast).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countAutoAssignmentsForDistributionSet(Long dsId) {
|
||||
return distributionSetRepository.countAutoAssignmentsForDistributionSet(dsId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@Retryable(include = {
|
||||
@@ -664,6 +680,8 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void throwEntityNotFoundExceptionIfDsTagDoesNotExist(final Long tagId) {
|
||||
if (!distributionSetTagRepository.existsById(tagId)) {
|
||||
throw new EntityNotFoundException(DistributionSetTag.class, tagId);
|
||||
|
||||
@@ -98,7 +98,6 @@ public class JpaRollout extends AbstractJpaNamedEntity implements Rollout, Event
|
||||
@Convert("rolloutstatus")
|
||||
@NotNull
|
||||
private RolloutStatus status = RolloutStatus.CREATING;
|
||||
|
||||
@Column(name = "last_check")
|
||||
private long lastCheck;
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 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.repository.jpa.model;
|
||||
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.Statistic;
|
||||
|
||||
public interface JpaStatistic extends Statistic {
|
||||
|
||||
}
|
||||
@@ -55,7 +55,9 @@ import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.MetaData;
|
||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.NamedVersionedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.Rollout;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.Statistic;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.test.matcher.Expect;
|
||||
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
|
||||
@@ -1157,6 +1159,67 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
|
||||
.updateMetaData(ds.getId(), entityFactory.generateDsMetadata(knownKey1, knownUpdateValue)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Get the Rollouts count by status statistics for a specific Distribution Set")
|
||||
void getRolloutsCountStatisticsForDistributionSet() {
|
||||
DistributionSet ds1 = testdataFactory.createDistributionSet("DS1");
|
||||
DistributionSet ds2 = testdataFactory.createDistributionSet("DS2");
|
||||
DistributionSet ds3 = testdataFactory.createDistributionSet("DS3");
|
||||
testdataFactory.createTargets("targets", 4);
|
||||
Rollout rollout1 = testdataFactory.createRolloutByVariables("rollout1", "description",
|
||||
1, "name==targets*", ds1, "50", "5", false);
|
||||
Rollout rollout2 = testdataFactory.createRolloutByVariables("rollout2", "description",
|
||||
1, "name==targets*", ds2, "50", "5", false);
|
||||
|
||||
rolloutManagement.start(rollout2.getId());
|
||||
|
||||
assertThat(distributionSetManagement.countRolloutsByStatusForDistributionSet(ds1.getId())).hasSize(1);
|
||||
assertThat(distributionSetManagement.countRolloutsByStatusForDistributionSet(ds2.getId())).hasSize(1);
|
||||
assertThat(distributionSetManagement.countRolloutsByStatusForDistributionSet(ds3.getId())).isEmpty();
|
||||
|
||||
Optional<Rollout> rollout = rolloutManagement.get(rollout1.getId());
|
||||
rollout.ifPresent(value -> assertThat(Rollout.RolloutStatus.valueOf(String.valueOf(distributionSetManagement.countRolloutsByStatusForDistributionSet(ds1.getId()).get(0).getName()))).isEqualTo(value.getStatus()));
|
||||
|
||||
rollout = rolloutManagement.get(rollout2.getId());
|
||||
rollout.ifPresent(value -> assertThat(Rollout.RolloutStatus.valueOf(String.valueOf(distributionSetManagement.countRolloutsByStatusForDistributionSet(ds2.getId()).get(0).getName()))).isEqualTo(value.getStatus()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Get the Rollouts count by status statistics for a specific Distribution Set")
|
||||
void getActionsCountStatisticsForDistributionSet() {
|
||||
DistributionSet ds = testdataFactory.createDistributionSet("DS");
|
||||
DistributionSet ds2 = testdataFactory.createDistributionSet("DS2");
|
||||
testdataFactory.createTargets("targets", 4);
|
||||
Rollout rollout = testdataFactory.createRolloutByVariables("rollout", "description",
|
||||
1, "name==targets*", ds, "50", "5", false);
|
||||
|
||||
rolloutManagement.start(rollout.getId());
|
||||
rolloutHandler.handleAll();
|
||||
|
||||
List<Statistic> statistics = distributionSetManagement.countActionsByStatusForDistributionSet(ds.getId());
|
||||
|
||||
assertThat(statistics).hasSize(1);
|
||||
assertThat(distributionSetManagement.countActionsByStatusForDistributionSet(ds2.getId())).isEmpty();
|
||||
|
||||
statistics.forEach(statistic -> assertThat(Status.valueOf(String.valueOf(statistic.getName()))).isEqualTo(Status.RUNNING));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Get the Rollouts count by status statistics for a specific Distribution Set")
|
||||
void getAutoAssignmentsCountStatisticsForDistributionSet() {
|
||||
DistributionSet ds = testdataFactory.createDistributionSet("DS");
|
||||
DistributionSet ds2 = testdataFactory.createDistributionSet("DS2");
|
||||
testdataFactory.createTargets("targets", 4);
|
||||
targetFilterQueryManagement.create(
|
||||
entityFactory.targetFilterQuery().create().name("test filter 1").autoAssignDistributionSet(ds.getId()).query("name==targets*"));
|
||||
|
||||
targetFilterQueryManagement.create(
|
||||
entityFactory.targetFilterQuery().create().name("test filter 2").autoAssignDistributionSet(ds.getId()).query("name==targets*"));
|
||||
|
||||
assertThat(distributionSetManagement.countAutoAssignmentsForDistributionSet(ds.getId())).isEqualTo(2);
|
||||
assertThat(distributionSetManagement.countAutoAssignmentsForDistributionSet(ds2.getId())).isNull();
|
||||
}
|
||||
|
||||
// can be removed with java-11
|
||||
private <T> T getOrThrow(final Optional<T> opt) {
|
||||
return opt.orElseThrow(NoSuchElementException::new);
|
||||
|
||||
Reference in New Issue
Block a user