|
|
|
@ -580,6 +580,7 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes |
|
|
|
@Test |
|
|
|
public void testScriptCalculatedFieldWhenUsedLatestTsInScript() throws Exception { |
|
|
|
Device testDevice = createDevice("Test device", "1234567890"); |
|
|
|
|
|
|
|
long ts = System.currentTimeMillis() - 300000L; |
|
|
|
postTelemetry(testDevice.getId(), String.format("{\"ts\": %s, \"values\": {\"temperature\":30}}", ts)); |
|
|
|
|
|
|
|
@ -614,6 +615,90 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testSimpleCalculatedFieldWhenUseLatestTsIsTrueAndDefaultArguments() throws Exception { |
|
|
|
Device testDevice = createDevice("Test device", "1234567890"); |
|
|
|
|
|
|
|
CalculatedField calculatedField = new CalculatedField(); |
|
|
|
calculatedField.setEntityId(testDevice.getId()); |
|
|
|
calculatedField.setType(CalculatedFieldType.SIMPLE); |
|
|
|
calculatedField.setName("a + b + c"); |
|
|
|
calculatedField.setDebugSettings(DebugSettings.all()); |
|
|
|
calculatedField.setConfigurationVersion(1); |
|
|
|
|
|
|
|
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration(); |
|
|
|
|
|
|
|
Argument argument1 = new Argument(); |
|
|
|
ReferencedEntityKey refEntityKey1 = new ReferencedEntityKey("a", ArgumentType.TS_LATEST, null); |
|
|
|
argument1.setRefEntityKey(refEntityKey1); |
|
|
|
argument1.setDefaultValue("100"); |
|
|
|
Argument argument2 = new Argument(); |
|
|
|
ReferencedEntityKey refEntityKey2 = new ReferencedEntityKey("b", ArgumentType.TS_LATEST, null); |
|
|
|
argument2.setRefEntityKey(refEntityKey2); |
|
|
|
argument2.setDefaultValue("200"); |
|
|
|
Argument argument3 = new Argument(); |
|
|
|
ReferencedEntityKey refEntityKey3 = new ReferencedEntityKey("c", ArgumentType.TS_LATEST, null); |
|
|
|
argument3.setRefEntityKey(refEntityKey3); |
|
|
|
argument3.setDefaultValue("300"); |
|
|
|
config.setArguments(Map.of("a", argument1, "b", argument2, "c", argument3)); |
|
|
|
config.setExpression("a + b + c"); |
|
|
|
|
|
|
|
TimeSeriesOutput output = new TimeSeriesOutput(); |
|
|
|
output.setName("d"); |
|
|
|
output.setDecimalsByDefault(0); |
|
|
|
config.setOutput(output); |
|
|
|
|
|
|
|
config.setUseLatestTs(true); |
|
|
|
|
|
|
|
calculatedField.setConfiguration(config); |
|
|
|
|
|
|
|
CalculatedField savedCalculatedField = doPost("/api/calculatedField", calculatedField, CalculatedField.class); |
|
|
|
|
|
|
|
await().alias("create CF -> perform initial calculation with default arguments").atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
|
.pollInterval(POLL_INTERVAL, TimeUnit.SECONDS) |
|
|
|
.untilAsserted(() -> { |
|
|
|
ObjectNode d = getLatestTelemetry(testDevice.getId(), "d"); |
|
|
|
assertThat(d).isNotNull(); |
|
|
|
assertThat(d.get("d").get(0).get("value").asText()).isEqualTo("600"); |
|
|
|
}); |
|
|
|
|
|
|
|
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"a\":10}")); |
|
|
|
|
|
|
|
await().alias("update telemetry -> save result with ts of 'a' argument").atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
|
.pollInterval(POLL_INTERVAL, TimeUnit.SECONDS) |
|
|
|
.untilAsserted(() -> { |
|
|
|
ObjectNode keys = getLatestTelemetry(testDevice.getId(), "d", "a"); |
|
|
|
assertThat(keys).isNotNull(); |
|
|
|
String aTs = keys.get("a").get(0).get("ts").asText(); |
|
|
|
assertThat(keys.get("d").get(0).get("ts").asText()).isEqualTo(aTs); |
|
|
|
assertThat(keys.get("d").get(0).get("value").asText()).isEqualTo("510"); |
|
|
|
}); |
|
|
|
|
|
|
|
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"b\":20}")); |
|
|
|
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"c\":30}")); |
|
|
|
|
|
|
|
await().alias("update telemetry -> save result with latest ts of updated arguments").atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
|
.pollInterval(POLL_INTERVAL, TimeUnit.SECONDS) |
|
|
|
.untilAsserted(() -> { |
|
|
|
ObjectNode keys = getLatestTelemetry(testDevice.getId(), "d"); |
|
|
|
assertThat(keys).isNotNull(); |
|
|
|
assertThat(keys.get("d").get(0).get("value").asText()).isEqualTo("60"); |
|
|
|
}); |
|
|
|
|
|
|
|
String latestTs = getLatestTelemetry(testDevice.getId(), "d").get("d").get(0).get("ts").asText(); |
|
|
|
|
|
|
|
doDelete("/api/plugins/telemetry/DEVICE/" + testDevice.getId() + "/timeseries/delete?keys=b&deleteAllDataForKeys=true").andExpect(status().isOk()); |
|
|
|
|
|
|
|
await().alias("delete telemetry -> save result with previous latest ts and default argument").atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
|
.pollInterval(POLL_INTERVAL, TimeUnit.SECONDS) |
|
|
|
.untilAsserted(() -> { |
|
|
|
ObjectNode keys = getLatestTelemetry(testDevice.getId(), "d"); |
|
|
|
assertThat(keys).isNotNull(); |
|
|
|
assertThat(keys.get("d").get(0).get("ts").asText()).isEqualTo(latestTs); |
|
|
|
assertThat(keys.get("d").get(0).get("value").asText()).isEqualTo("240"); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testSimpleCalculatedFieldWhenCtxBecameUninitialized() throws Exception { |
|
|
|
Device testDevice = createDevice("Test device", "1234567890"); |
|
|
|
|