Skip to content

Commit

Permalink
Convert ArrayNode elements from db query to expected Number data type…
Browse files Browse the repository at this point in the history
…s allowing Javascript to operate. The expected types are present for Array.reduce function to succeed.
  • Loading branch information
whitingjr committed Sep 20, 2023
1 parent 274e0e5 commit 093b89f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.hyperfoil.tools.horreum.svc;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.node.ArrayNode;
import io.hyperfoil.tools.horreum.bus.MessageBusChannels;
import io.hyperfoil.tools.horreum.hibernate.JsonBinaryType;
import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -433,7 +435,7 @@ void calculateLabels(int testId, int datasetId, int queryLabelId, boolean isReca
LabelDAO.Value.delete("datasetId = ?1 AND labelId = ?2", datasetId, queryLabelId);
}

Util.evaluateMany(extracted, row -> (String) row[2], row -> (JsonNode) row[3],
Util.evaluateMany(extracted, row -> (String) row[2], row -> (row[3] instanceof ArrayNode ? split((ArrayNode) row[3]) : (JsonNode) row[3]),
(row, result) -> createLabel(datasetId, (int) row[0], Util.convertToJson(result)),
row -> createLabel(datasetId, (int) row[0], (JsonNode) row[3]),
(row, e, jsCode) -> logMessage(datasetId, PersistentLogDAO.ERROR,
Expand All @@ -442,6 +444,26 @@ void calculateLabels(int testId, int datasetId, int queryLabelId, boolean isReca
messageBus.publish(MessageBusChannels.DATASET_UPDATED_LABELS, testId, new DataSet.LabelsUpdatedEvent(testId, datasetId, isRecalculation));
}

private ArrayNode split(ArrayNode arrayNode){
JsonNode node = arrayNode.get(0);
if (node == null)
return arrayNode;

String element0 = node.asText().replace("[", "").replace("]","");
String[] values = element0.split(",");
if (values.length > 1){
arrayNode.removeAll();
Arrays.asList(values).forEach(v -> {
try {
arrayNode.add(Integer.parseInt(v));
} catch (NumberFormatException nfe) {
arrayNode.add(v);
}
});
}
return arrayNode;
}

@WithRoles(extras = Roles.HORREUM_SYSTEM)
@Transactional(Transactional.TxType.REQUIRES_NEW)
protected void findFailingExtractor(int datasetId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.quarkus.test.oidc.server.OidcWiremockTestResource;
import org.junit.jupiter.api.TestInfo;

@QuarkusTest
@QuarkusTestResource(PostgresResource.class)
Expand Down Expand Up @@ -82,7 +83,7 @@ public void testDataSetQuerySchemaArray() {
private String testDataSetQuery(String jsonPath, boolean array, String schemaUri) {
AtomicReference<String> result = new AtomicReference<>();
withExampleSchemas(schemas -> result.set(withExampleDataset(createTest(createExampleTest("dummy")),
createABData(), ds -> {
createABCData(), ds -> {
QueryResult queryResult = datasetService.queryData(ds.id, jsonPath, array, schemaUri);
assertTrue(queryResult.valid);
return queryResult.value;
Expand All @@ -95,13 +96,22 @@ public void testDatasetLabelsSingle() {
withExampleSchemas((schemas) -> {
int labelA = addLabel(schemas[0], "value", null, new Extractor("value", "$.value", false));
int labelB = addLabel(schemas[1], "value", "v => v + 1", new Extractor("value", "$.value", false));
List<Label.Value> values = withLabelValues(createABData());
List<Label.Value> values = withLabelValues(createABCData());
assertEquals(2, values.size());
assertEquals(24, values.stream().filter(v -> v.labelId == labelA).map(v -> v.value.numberValue()).findFirst().orElse(null));
assertEquals(43, values.stream().filter(v -> v.labelId == labelB).map(v -> v.value.numberValue()).findFirst().orElse(null));
}, "urn:A", "urn:B");
}

@org.junit.jupiter.api.Test
public void testDatasetLabelSingleWithReduceFunction(TestInfo info) throws InterruptedException {
withExampleSchemas((schemas) -> {
int labelC = addLabel(schemas[2], "value", "value => { return value.reduce((a,b) => a+b) / value.length; }", new Extractor("value", "$.samples", true));
List<Label.Value> values = withLabelValues(createABCData());
assertEquals(6, values.stream().filter(v -> v.labelId == labelC).map(v -> v.value.numberValue()).findFirst().orElse(null));
}, "urn:A", "urn:B", "urn:C");
}

private ArrayNode createXYData() {
ArrayNode data = JsonNodeFactory.instance.arrayNode();
ObjectNode a = JsonNodeFactory.instance.objectNode();
Expand Down Expand Up @@ -182,7 +192,7 @@ public void testDatasetLabelChanged() {
int labelC = addLabel(schemas[1], "C", null, new Extractor("value", "$.value", false));
Test test = createTest(createExampleTest("dummy"));
BlockingQueue<DataSet.LabelsUpdatedEvent> updateQueue = eventConsumerQueue(DataSet.LabelsUpdatedEvent.class, MessageBusChannels.DATASET_UPDATED_LABELS, e -> checkTestId(e.datasetId, test.id));
withExampleDataset(test, createABData(), ds -> {
withExampleDataset(test, createABCData(), ds -> {
waitForUpdate(updateQueue, ds);
List<LabelDAO.Value> values = LabelDAO.Value.<LabelDAO.Value>find("datasetId", ds.id).list();
assertEquals(3, values.size());
Expand Down Expand Up @@ -293,7 +303,7 @@ public void testDatasetView() {
int labelB = addLabel(schemas[1], "b", null, valuePath);
// view update should happen in the same transaction as labels update so we can use the event
BlockingQueue<DataSet.LabelsUpdatedEvent> updateQueue = eventConsumerQueue(DataSet.LabelsUpdatedEvent.class, MessageBusChannels.DATASET_UPDATED_LABELS, e -> checkTestId(e.datasetId, test.id));
withExampleDataset(test, createABData(), ds -> {
withExampleDataset(test, createABCData(), ds -> {
waitForUpdate(updateQueue, ds);
JsonNode datasets = fetchDatasetsByTest(test.id);
assertEquals(1, datasets.get("total").asInt());
Expand Down Expand Up @@ -349,15 +359,18 @@ private void withExampleSchemas(Consumer<Schema[]> testLogic, String... schemas)
}
}

private ArrayNode createABData() {
private ArrayNode createABCData() {
ArrayNode data = JsonNodeFactory.instance.arrayNode();
ObjectNode a = JsonNodeFactory.instance.objectNode();
ObjectNode b = JsonNodeFactory.instance.objectNode();
ObjectNode c = JsonNodeFactory.instance.objectNode();
a.put("$schema", "urn:A");
a.put("value", 24);
b.put("$schema", "urn:B");
b.put("value", 42);
data.add(a).add(b);
c.put("$schema", "urn:C");
c.put("samples", "[2,4,6,8,10]");
data.add(a).add(b).add(c);
return data;
}

Expand Down

0 comments on commit 093b89f

Please sign in to comment.