|
|
|
@ -18,6 +18,12 @@ package org.thingsboard.server.controller; |
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Test; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
import org.mockito.Mockito; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.boot.test.mock.mockito.SpyBean; |
|
|
|
import org.thingsboard.server.common.data.asset.Asset; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
@ -26,13 +32,45 @@ import org.thingsboard.server.common.data.queue.ProcessingStrategyType; |
|
|
|
import org.thingsboard.server.common.data.queue.Queue; |
|
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategy; |
|
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategyType; |
|
|
|
import org.thingsboard.server.common.msg.queue.RuleEngineException; |
|
|
|
import org.thingsboard.server.common.stats.StatsFactory; |
|
|
|
import org.thingsboard.server.dao.asset.AssetService; |
|
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
|
import org.thingsboard.server.dao.timeseries.TimeseriesDao; |
|
|
|
import org.thingsboard.server.gen.transport.TransportProtos; |
|
|
|
import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
|
|
|
import org.thingsboard.server.service.queue.TbRuleEngineConsumerStats; |
|
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult; |
|
|
|
import org.thingsboard.server.service.stats.DefaultRuleEngineStatisticsService; |
|
|
|
import org.thingsboard.server.service.stats.RuleEngineStatisticsService; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.mockito.ArgumentMatchers.argThat; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
|
|
import static org.thingsboard.server.dao.asset.BaseAssetService.TB_SERVICE_QUEUE; |
|
|
|
|
|
|
|
@DaoSqlTest |
|
|
|
public class BaseQueueControllerTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RuleEngineStatisticsService ruleEngineStatisticsService; |
|
|
|
@Autowired |
|
|
|
private StatsFactory statsFactory; |
|
|
|
@SpyBean |
|
|
|
private TimeseriesDao timeseriesDao; |
|
|
|
@Autowired |
|
|
|
private AssetService assetService; |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testQueueWithServiceTypeRE() throws Exception { |
|
|
|
loginSysAdmin(); |
|
|
|
@ -93,4 +131,61 @@ public class BaseQueueControllerTest extends AbstractControllerTest { |
|
|
|
.andExpect(status().isOk()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testQueueStatsTtl() throws ThingsboardException { |
|
|
|
Queue queue = new Queue(); |
|
|
|
queue.setName("Test-1"); |
|
|
|
queue.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
|
|
|
|
TbRuleEngineProcessingResult testProcessingResult = Mockito.mock(TbRuleEngineProcessingResult.class); |
|
|
|
TbProtoQueueMsg<TransportProtos.ToRuleEngineMsg> msg = new TbProtoQueueMsg<>(UUID.randomUUID(), |
|
|
|
TransportProtos.ToRuleEngineMsg.newBuilder() |
|
|
|
.setTenantIdMSB(tenantId.getId().getMostSignificantBits()) |
|
|
|
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()) |
|
|
|
.build()); |
|
|
|
when(testProcessingResult.getSuccessMap()).thenReturn(Stream.generate(() -> msg) |
|
|
|
.limit(5).collect(Collectors.toConcurrentMap(m -> UUID.randomUUID(), m -> m))); |
|
|
|
when(testProcessingResult.getFailedMap()).thenReturn(Stream.generate(() -> msg) |
|
|
|
.limit(5).collect(Collectors.toConcurrentMap(m -> UUID.randomUUID(), m -> m))); |
|
|
|
when(testProcessingResult.getPendingMap()).thenReturn(new ConcurrentHashMap<>()); |
|
|
|
RuleEngineException ruleEngineException = new RuleEngineException("Test Exception"); |
|
|
|
when(testProcessingResult.getExceptionsMap()).thenReturn(new ConcurrentHashMap<>(Map.of( |
|
|
|
tenantId, ruleEngineException |
|
|
|
))); |
|
|
|
|
|
|
|
TbRuleEngineConsumerStats testStats = new TbRuleEngineConsumerStats(queue, statsFactory); |
|
|
|
testStats.log(testProcessingResult, true); |
|
|
|
|
|
|
|
int queueStatsTtlDays = 14; |
|
|
|
int ruleEngineExceptionsTtlDays = 7; |
|
|
|
updateDefaultTenantProfileConfig(profileConfiguration -> { |
|
|
|
profileConfiguration.setQueueStatsTtlDays(queueStatsTtlDays); |
|
|
|
profileConfiguration.setRuleEngineExceptionsTtlDays(ruleEngineExceptionsTtlDays); |
|
|
|
}); |
|
|
|
ruleEngineStatisticsService.reportQueueStats(System.currentTimeMillis(), testStats); |
|
|
|
|
|
|
|
Asset serviceAsset = assetService.findAssetsByTenantIdAndType(tenantId, TB_SERVICE_QUEUE, new PageLink(100)).getData() |
|
|
|
.stream().filter(asset -> asset.getName().startsWith(queue.getName())) |
|
|
|
.findFirst().get(); |
|
|
|
|
|
|
|
ArgumentCaptor<Long> ttlCaptor = ArgumentCaptor.forClass(Long.class); |
|
|
|
verify(timeseriesDao).save(eq(tenantId), eq(serviceAsset.getId()), argThat(tsKvEntry -> { |
|
|
|
return tsKvEntry.getKey().equals(TbRuleEngineConsumerStats.SUCCESSFUL_MSGS) && |
|
|
|
tsKvEntry.getLongValue().get().equals(5L); |
|
|
|
}), ttlCaptor.capture()); |
|
|
|
verify(timeseriesDao).save(eq(tenantId), eq(serviceAsset.getId()), argThat(tsKvEntry -> { |
|
|
|
return tsKvEntry.getKey().equals(TbRuleEngineConsumerStats.FAILED_MSGS) && |
|
|
|
tsKvEntry.getLongValue().get().equals(5L); |
|
|
|
}), ttlCaptor.capture()); |
|
|
|
assertThat(ttlCaptor.getAllValues()).allSatisfy(usedTtl -> { |
|
|
|
assertThat(usedTtl).isEqualTo(TimeUnit.DAYS.toSeconds(queueStatsTtlDays)); |
|
|
|
}); |
|
|
|
|
|
|
|
verify(timeseriesDao).save(eq(tenantId), eq(serviceAsset.getId()), argThat(tsKvEntry -> { |
|
|
|
return tsKvEntry.getKey().equals(DefaultRuleEngineStatisticsService.RULE_ENGINE_EXCEPTION) && |
|
|
|
tsKvEntry.getJsonValue().get().equals(ruleEngineException.toJsonString()); |
|
|
|
}), ttlCaptor.capture()); |
|
|
|
assertThat(ttlCaptor.getValue()).isEqualTo(TimeUnit.DAYS.toSeconds(ruleEngineExceptionsTtlDays)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|