Fixed download progress event.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
kaizimmerm
2016-07-07 14:46:29 +02:00
parent a9204fe5eb
commit 8fb2bd4322
15 changed files with 73 additions and 127 deletions

View File

@@ -65,11 +65,14 @@ public interface ControllerManagement {
* the ID of the {@link ActionStatus}
* @param progressPercent
* the progress in percentage which must be between 0-100
* @param shippedBytes
* since last event
* @param shippedBytesSinceLast
* since the last report
* @param shippedBytesOverall
* for the {@link ActionStatus}
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
void downloadProgressPercent(long statusId, int progressPercent, long shippedBytes);
void downloadProgressPercent(long statusId, int progressPercent, long shippedBytesSinceLast,
long shippedBytesOverall);
/**
* Simple addition of a new {@link ActionStatus} entry to the {@link Action}
@@ -77,9 +80,11 @@ public interface ControllerManagement {
*
* @param statusMessage
* to add to the action
*
* @return create {@link ActionStatus} entity
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
void addInformationalActionStatus(@NotNull ActionStatus statusMessage);
ActionStatus addInformationalActionStatus(@NotNull ActionStatus statusMessage);
/**
* Adds an {@link ActionStatus} entry for an update {@link Action} including

View File

@@ -29,12 +29,4 @@ public interface TenantStatsManagement {
+ SpringEvalExpressions.IS_SYSTEM_CODE)
TenantUsage getStatsOfTenant();
/**
* Resets {@link TenantUsage#getOverallArtifactTrafficInBytes()} to zero.
*
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_TENANT_CONFIGURATION + SpringEvalExpressions.HAS_AUTH_OR
+ SpringEvalExpressions.IS_SYSTEM_CODE)
void resetTrafficStatsOfTenant();
}

View File

@@ -19,7 +19,6 @@ public class TenantUsage {
private long artifacts;
private long actions;
private long overallArtifactVolumeInBytes;
private long overallArtifactTrafficInBytes;
/**
* Constructor.
@@ -106,28 +105,12 @@ public class TenantUsage {
return this;
}
/**
* @return the overallArtifactTrafficInBytes
*/
public long getOverallArtifactTrafficInBytes() {
return overallArtifactTrafficInBytes;
}
/**
* @param overallArtifactTrafficInBytes
* the overallArtifactTrafficInBytes to set
*/
public void setOverallArtifactTrafficInBytes(final long overallArtifactTrafficInBytes) {
this.overallArtifactTrafficInBytes = overallArtifactTrafficInBytes;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (actions ^ (actions >>> 32));
result = prime * result + (int) (artifacts ^ (artifacts >>> 32));
result = prime * result + (int) (overallArtifactTrafficInBytes ^ (overallArtifactTrafficInBytes >>> 32));
result = prime * result + (int) (overallArtifactVolumeInBytes ^ (overallArtifactVolumeInBytes >>> 32));
result = prime * result + (int) (targets ^ (targets >>> 32));
result = prime * result + ((tenantName == null) ? 0 : tenantName.hashCode());
@@ -142,7 +125,7 @@ public class TenantUsage {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!(obj instanceof TenantUsage)) {
return false;
}
final TenantUsage other = (TenantUsage) obj;
@@ -152,9 +135,6 @@ public class TenantUsage {
if (artifacts != other.artifacts) {
return false;
}
if (overallArtifactTrafficInBytes != other.overallArtifactTrafficInBytes) {
return false;
}
if (overallArtifactVolumeInBytes != other.overallArtifactVolumeInBytes) {
return false;
}
@@ -174,8 +154,7 @@ public class TenantUsage {
@Override
public String toString() {
return "TenantUsage [tenantName=" + tenantName + ", targets=" + targets + ", artifacts=" + artifacts
+ ", actions=" + actions + ", overallArtifactVolumeInBytes=" + overallArtifactVolumeInBytes
+ ", overallArtifactTrafficInBytes=" + overallArtifactTrafficInBytes + "]";
+ ", actions=" + actions + ", overallArtifactVolumeInBytes=" + overallArtifactVolumeInBytes + "]";
}
}

View File

@@ -451,8 +451,8 @@ public class JpaControllerManagement implements ControllerManagement {
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void addInformationalActionStatus(final ActionStatus statusMessage) {
actionStatusRepository.save((JpaActionStatus) statusMessage);
public ActionStatus addInformationalActionStatus(final ActionStatus statusMessage) {
return actionStatusRepository.save((JpaActionStatus) statusMessage);
}
@Override
@@ -469,8 +469,9 @@ public class JpaControllerManagement implements ControllerManagement {
}
@Override
public void downloadProgressPercent(final long statusId, final int progressPercent, final long shippedBytes) {
cacheWriteNotify.downloadProgressPercent(statusId, progressPercent, shippedBytes);
public void downloadProgressPercent(final long statusId, final int progressPercent,
final long shippedBytesSinceLast, final long shippedBytesOverall) {
cacheWriteNotify.downloadProgressPercent(statusId, progressPercent, shippedBytesSinceLast, shippedBytesOverall);
}
}

View File

@@ -8,12 +8,8 @@
*/
package org.eclipse.hawkbit.repository.jpa;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.hawkbit.eventbus.event.DownloadProgressEvent;
import org.eclipse.hawkbit.repository.TenantStatsManagement;
import org.eclipse.hawkbit.repository.report.model.TenantUsage;
import org.eclipse.hawkbit.tenancy.TenantAware;
@@ -23,8 +19,6 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import com.google.common.eventbus.Subscribe;
/**
* Management service for statistics of a single tenant.
*
@@ -44,8 +38,6 @@ public class JpaTenantStatsManagement implements TenantStatsManagement {
@Autowired
private TenantAware tenantAware;
private final Map<String, AtomicLong> traffic = new ConcurrentHashMap<>();
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
public TenantUsage getStatsOfTenant() {
@@ -64,26 +56,9 @@ public class JpaTenantStatsManagement implements TenantStatsManagement {
}
result.setActions(actionRepository.count());
if (traffic.containsKey(tenant)) {
result.setOverallArtifactTrafficInBytes(traffic.get(tenant).get());
}
return result;
}
@Override
public void resetTrafficStatsOfTenant() {
traffic.remove(tenantAware.getCurrentTenant());
}
@Subscribe
public void listen(final DownloadProgressEvent event) {
if (traffic.containsKey(event.getTenant())) {
traffic.get(event.getTenant()).addAndGet(event.getShippedBytes());
} else {
traffic.put(event.getTenant(), new AtomicLong(event.getShippedBytes()));
}
}
}

View File

@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.repository.jpa.cache;
import org.eclipse.hawkbit.eventbus.event.DownloadProgressEvent;
import org.eclipse.hawkbit.repository.eventbus.event.RolloutGroupCreatedEvent;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.ActionStatus;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.tenancy.TenantAware;
@@ -54,12 +53,15 @@ public class CacheWriteNotify {
* the ID of the {@link ActionStatus}
* @param progressPercent
* the progress in percentage which must be between 0-100
* @param shippedBytes
* @param shippedBytesSinceLast
* since last event
* @param shippedBytesOverall
* for the download request
*/
public void downloadProgressPercent(final long statusId, final int progressPercent, final long shippedBytes) {
public void downloadProgressPercent(final long statusId, final int progressPercent,
final long shippedBytesSinceLast, final long shippedBytesOverall) {
final Cache cache = cacheManager.getCache(Action.class.getName());
final Cache cache = cacheManager.getCache(ActionStatus.class.getName());
final String cacheKey = CacheKeys.entitySpecificCacheKey(String.valueOf(statusId),
CacheKeys.DOWNLOAD_PROGRESS_PERCENT);
if (progressPercent < DOWNLOAD_PROGRESS_MAX) {
@@ -71,8 +73,8 @@ public class CacheWriteNotify {
cache.evict(cacheKey);
}
eventBus.post(
new DownloadProgressEvent(tenantAware.getCurrentTenant(), statusId, progressPercent, shippedBytes));
eventBus.post(new DownloadProgressEvent(tenantAware.getCurrentTenant(), statusId, progressPercent,
shippedBytesSinceLast, shippedBytesOverall));
}
/**

View File

@@ -14,7 +14,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.eclipse.hawkbit.eventbus.event.DownloadProgressEvent;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.ActionStatus;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.junit.Before;
import org.junit.Test;
@@ -61,12 +61,12 @@ public class CacheWriteNotifyTest {
final long knownStatusId = 1;
final int knownPercentage = 23;
when(cacheManagerMock.getCache(Action.class.getName())).thenReturn(cacheMock);
when(cacheManagerMock.getCache(ActionStatus.class.getName())).thenReturn(cacheMock);
when(tenantAwareMock.getCurrentTenant()).thenReturn("default");
underTest.downloadProgressPercent(knownStatusId, knownPercentage, 100L);
underTest.downloadProgressPercent(knownStatusId, knownPercentage, 100L, 500L);
verify(cacheManagerMock).getCache(eq(Action.class.getName()));
verify(cacheManagerMock).getCache(eq(ActionStatus.class.getName()));
verify(cacheMock).put(knownStatusId + "." + CacheKeys.DOWNLOAD_PROGRESS_PERCENT, knownPercentage);
verify(eventBusMock).post(any(DownloadProgressEvent.class));
}