Alternative less expensive stats service. (#549)
* Alternative less expensive stats service. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix NEP. Address is optional. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * revert null check, Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Specified return object. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Add tenant count overall. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -19,14 +19,13 @@ import org.eclipse.hawkbit.mgmt.json.model.systemmanagement.MgmtSystemStatistics
|
||||
import org.eclipse.hawkbit.mgmt.json.model.systemmanagement.MgmtSystemTenantServiceUsage;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtSystemManagementRestApi;
|
||||
import org.eclipse.hawkbit.repository.SystemManagement;
|
||||
import org.eclipse.hawkbit.repository.report.model.SystemUsageReport;
|
||||
import org.eclipse.hawkbit.repository.report.model.SystemUsageReportWithTenants;
|
||||
import org.eclipse.hawkbit.repository.report.model.TenantUsage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -68,7 +67,7 @@ public class MgmtSystemManagementResource implements MgmtSystemManagementRestApi
|
||||
*/
|
||||
@Override
|
||||
public ResponseEntity<MgmtSystemStatisticsRest> getSystemUsageStats() {
|
||||
final SystemUsageReport report = systemManagement.getSystemUsageStatistics();
|
||||
final SystemUsageReportWithTenants report = systemManagement.getSystemUsageStatisticsWithTenants();
|
||||
|
||||
final MgmtSystemStatisticsRest result = new MgmtSystemStatisticsRest()
|
||||
.setOverallActions(report.getOverallActions()).setOverallArtifacts(report.getOverallArtifacts())
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.repository.model.TenantMetaData;
|
||||
import org.eclipse.hawkbit.repository.report.model.SystemUsageReport;
|
||||
import org.eclipse.hawkbit.repository.report.model.SystemUsageReportWithTenants;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -70,6 +71,14 @@ public interface SystemManagement {
|
||||
* @return SystemUsageReport of the current system
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN)
|
||||
SystemUsageReportWithTenants getSystemUsageStatisticsWithTenants();
|
||||
|
||||
/**
|
||||
* Calculated overall system usage statistics
|
||||
*
|
||||
* @return SystemUsageReport of the current system
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN)
|
||||
SystemUsageReport getSystemUsageStatistics();
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,10 +8,6 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.report.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bean for holding the system usage stats.
|
||||
*
|
||||
@@ -21,8 +17,7 @@ public class SystemUsageReport {
|
||||
private final long overallArtifacts;
|
||||
private final long overallArtifactVolumeInBytes;
|
||||
private final long overallActions;
|
||||
|
||||
private final List<TenantUsage> tenants = new ArrayList<>();
|
||||
private final long overallTenants;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -35,60 +30,35 @@ public class SystemUsageReport {
|
||||
* of the system
|
||||
* @param overallArtifactVolumeInBytes
|
||||
* of the system
|
||||
* @param overallTenants
|
||||
* of the system
|
||||
*/
|
||||
public SystemUsageReport(final long overallTargets, final long overallArtifacts, final long overallActions,
|
||||
final long overallArtifactVolumeInBytes) {
|
||||
super();
|
||||
final long overallArtifactVolumeInBytes, final long overallTenants) {
|
||||
this.overallTargets = overallTargets;
|
||||
this.overallArtifacts = overallArtifacts;
|
||||
this.overallActions = overallActions;
|
||||
|
||||
this.overallArtifactVolumeInBytes = overallArtifactVolumeInBytes;
|
||||
this.overallTenants = overallTenants;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return overallTargets in the system
|
||||
*/
|
||||
public long getOverallTargets() {
|
||||
return overallTargets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return overallArtifacts in the system
|
||||
*/
|
||||
public long getOverallArtifacts() {
|
||||
return overallArtifacts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return overallArtifactVolumeInBytes of the system
|
||||
*/
|
||||
public long getOverallArtifactVolumeInBytes() {
|
||||
return overallArtifactVolumeInBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param tenantUsage
|
||||
* of one tenant
|
||||
* @return updated bean
|
||||
*/
|
||||
public SystemUsageReport addTenantData(final TenantUsage tenantUsage) {
|
||||
tenants.add(tenantUsage);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return actions of system
|
||||
*/
|
||||
public long getOverallActions() {
|
||||
return overallActions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return tenant data
|
||||
*/
|
||||
public List<TenantUsage> getTenants() {
|
||||
return Collections.unmodifiableList(tenants);
|
||||
public long getOverallTenants() {
|
||||
return overallTenants;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations 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.report.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bean for holding the system usage stats including tenant specific data.
|
||||
*
|
||||
*/
|
||||
public class SystemUsageReportWithTenants extends SystemUsageReport {
|
||||
|
||||
private final List<TenantUsage> tenants = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param overallTargets
|
||||
* of the system
|
||||
* @param overallArtifacts
|
||||
* of the system
|
||||
* @param overallActions
|
||||
* of the system
|
||||
* @param overallArtifactVolumeInBytes
|
||||
* of the system
|
||||
*/
|
||||
public SystemUsageReportWithTenants(final long overallTargets, final long overallArtifacts,
|
||||
final long overallActions, final long overallArtifactVolumeInBytes, final long overallTenants) {
|
||||
super(overallTargets, overallArtifacts, overallActions, overallArtifactVolumeInBytes, overallTenants);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param tenantUsage
|
||||
* of one tenant
|
||||
* @return updated bean
|
||||
*/
|
||||
public SystemUsageReportWithTenants addTenantData(final TenantUsage tenantUsage) {
|
||||
tenants.add(tenantUsage);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return tenant data
|
||||
*/
|
||||
public List<TenantUsage> getTenants() {
|
||||
return Collections.unmodifiableList(tenants);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.repository.model.TenantMetaData;
|
||||
import org.eclipse.hawkbit.repository.report.model.SystemUsageReport;
|
||||
import org.eclipse.hawkbit.repository.report.model.SystemUsageReportWithTenants;
|
||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
||||
@@ -145,15 +146,20 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
final Long actions = (Long) entityManager.createNativeQuery("SELECT COUNT(id) FROM sp_action")
|
||||
.getSingleResult();
|
||||
|
||||
final SystemUsageReport result = new SystemUsageReport(targets, artifacts, actions,
|
||||
sumOfArtifacts.setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
|
||||
return new SystemUsageReportWithTenants(targets, artifacts, actions,
|
||||
sumOfArtifacts.setScale(0, BigDecimal.ROUND_HALF_UP).longValue(), tenantMetaDataRepository.count());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SystemUsageReportWithTenants getSystemUsageStatisticsWithTenants() {
|
||||
final SystemUsageReportWithTenants result = (SystemUsageReportWithTenants) getSystemUsageStatistics();
|
||||
|
||||
usageStatsPerTenant(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void usageStatsPerTenant(final SystemUsageReport report) {
|
||||
private void usageStatsPerTenant(final SystemUsageReportWithTenants report) {
|
||||
final List<String> tenants = findTenants(new PageRequest(0, MAX_TENANTS_QUERY)).getContent();
|
||||
|
||||
tenants.forEach(tenant -> tenantAware.runAsTenant(tenant, () -> {
|
||||
|
||||
@@ -51,7 +51,7 @@ public class SystemManagementTest extends AbstractJpaIntegrationTest {
|
||||
assertThat(systemManagement.getSystemUsageStatistics().getOverallArtifactVolumeInBytes()).isEqualTo(1234 * 2);
|
||||
|
||||
// per tenant data
|
||||
final List<TenantUsage> tenants = systemManagement.getSystemUsageStatistics().getTenants();
|
||||
final List<TenantUsage> tenants = systemManagement.getSystemUsageStatisticsWithTenants().getTenants();
|
||||
assertThat(tenants).hasSize(3);
|
||||
assertThat(tenants).containsOnly(new TenantUsage("default"),
|
||||
new TenantUsage("tenant0").setArtifacts(1).setOverallArtifactVolumeInBytes(1234),
|
||||
@@ -69,10 +69,11 @@ public class SystemManagementTest extends AbstractJpaIntegrationTest {
|
||||
assertThat(systemManagement.getSystemUsageStatistics().getOverallActions()).isEqualTo(0);
|
||||
|
||||
// per tenant data
|
||||
final List<TenantUsage> tenants = systemManagement.getSystemUsageStatistics().getTenants();
|
||||
final List<TenantUsage> tenants = systemManagement.getSystemUsageStatisticsWithTenants().getTenants();
|
||||
assertThat(tenants).hasSize(3);
|
||||
assertThat(tenants).containsOnly(new TenantUsage("default"), new TenantUsage("tenant0").setTargets(100),
|
||||
new TenantUsage("tenant1").setTargets(100));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,7 +86,7 @@ public class SystemManagementTest extends AbstractJpaIntegrationTest {
|
||||
assertThat(systemManagement.getSystemUsageStatistics().getOverallActions()).isEqualTo(400);
|
||||
|
||||
// per tenant data
|
||||
final List<TenantUsage> tenants = systemManagement.getSystemUsageStatistics().getTenants();
|
||||
final List<TenantUsage> tenants = systemManagement.getSystemUsageStatisticsWithTenants().getTenants();
|
||||
assertThat(tenants).hasSize(3);
|
||||
assertThat(tenants).containsOnly(new TenantUsage("default"),
|
||||
new TenantUsage("tenant0").setTargets(100).setActions(200),
|
||||
|
||||
Reference in New Issue
Block a user