Skip to content

Commit

Permalink
Added TestExport to avoid creating unstructured json, fixes #860
Browse files Browse the repository at this point in the history
  • Loading branch information
stalep committed Jan 30, 2024
1 parent 6abbb69 commit 45d0d74
Show file tree
Hide file tree
Showing 25 changed files with 826 additions and 461 deletions.
382 changes: 378 additions & 4 deletions docs/site/content/en/openapi/openapi.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

public class ChangeDetection {
@JsonProperty( required = true )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.util.List;
import java.util.Set;

public class Variable {
Expand All @@ -23,19 +23,16 @@ public class Variable {
public int order;
@NotNull
@JsonProperty(required = true)
public JsonNode labels;
public List<String> labels;
@JsonInclude(JsonInclude.Include.NON_NULL)
public String calculation;
@Schema(
required = true,
implementation = ChangeDetection[].class
)
@Schema(required = true, implementation = ChangeDetection[].class)
public Set<ChangeDetection> changeDetection;

public Variable() {
}

public Variable(Integer id, int testId, String name, String group, int order, JsonNode labels, String calculation,
public Variable(Integer id, int testId, String name, String group, int order, List<String> labels, String calculation,
Set<ChangeDetection> changeDetection) {
this.id = id;
this.testId = testId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.hyperfoil.tools.horreum.api.data;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.util.Collection;

@JsonIdentityInfo( property = "id", generator = ObjectIdGenerators.PropertyGenerator.class)
@Schema(description = "An Experiment Profile defines the labels and filters for the dataset and baseline")
public class ExperimentProfile {
@JsonProperty(required = true )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class Label extends ProtectedType {
public Label() {
}

public Label(String name, int schemaId) {
this.name = name;
this.schemaId = schemaId;
}

public static class Value implements Serializable {
public int datasetId;
public int labelId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.hyperfoil.tools.horreum.api.data;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
Expand Down Expand Up @@ -63,6 +65,23 @@ public Test() {
this.access = Access.PUBLIC;
}

public Test(Test t) {
id = t.id;
name = t.name;
folder = t.folder;
description = t.description;
tokens = t.tokens;
timelineLabels = t.timelineLabels;
timelineFunction = t.timelineFunction;
fingerprintLabels = t.fingerprintLabels;
fingerprintFilter = t.fingerprintFilter;
compareUrl = t.compareUrl;
transformers = t.transformers;
notificationsEnabled = t.notificationsEnabled;
access = t.access;
owner = t.owner;
}

@Override
public String toString() {
return "Test{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.hyperfoil.tools.horreum.api.data;

import io.hyperfoil.tools.horreum.api.alerting.MissingDataRule;
import io.hyperfoil.tools.horreum.api.alerting.Variable;
import io.hyperfoil.tools.horreum.api.alerting.Watch;

import java.util.List;

public class TestExport extends Test {

public List<Variable> variables;
public List<MissingDataRule> missingDataRules;
public List<ExperimentProfile> experiments;
public List<Action> actions;
public Watch subscriptions;

public TestExport() {
super();
}
public TestExport(Test t) {
super(t);
}

//need to propagate the changes to newTest into the existing properties
public void update(Test newTest) {
id = newTest.id;
}

public void updateRefs() {
//need to make sure the correct variables are used by experiments
if(variables != null && !variables.isEmpty())
variables.forEach(variable -> variable.testId = id);

if(experiments != null && !experiments.isEmpty()) {
for(ExperimentProfile experiment : experiments) {
experiment.testId = id;
}
}
if(actions != null && !actions.isEmpty()) {
for(Action action : actions) {
action.testId = id;
}
}
if(subscriptions != null) {
subscriptions.testId = id;
}
if(missingDataRules != null && !missingDataRules.isEmpty()) {
for(MissingDataRule rule : missingDataRules) {
rule.testId = id;
}
}
}
public void updateExperimentsVariableId(int oldVarId, int newVarId) {
if(experiments != null && !experiments.isEmpty()) {
for(ExperimentProfile experiment : experiments) {
if(experiment.comparisons != null && !experiment.comparisons.isEmpty()) {
experiment.comparisons.forEach(c -> {
if(c.variableId == oldVarId)
c.variableId = newVarId;
});
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import io.hyperfoil.tools.horreum.api.SortDirection;
import io.hyperfoil.tools.horreum.api.data.Access;
import io.hyperfoil.tools.horreum.api.data.TestExport;
import io.hyperfoil.tools.horreum.api.data.Fingerprints;
import io.hyperfoil.tools.horreum.api.data.ExportedLabelValues;
import io.hyperfoil.tools.horreum.api.data.ProtectedType;
Expand Down Expand Up @@ -228,18 +229,18 @@ List<ExportedLabelValues> listLabelValues(@PathParam("id") int testId,

@GET
@Path("{id}/export")
@APIResponseSchema(value = String.class,
@APIResponseSchema(value = TestExport.class,
responseDescription = "A Test defintion formatted as json",
responseCode = "200")
String export(@PathParam("id") int testId);
TestExport export(@PathParam("id") int testId);

@POST
@Path("import")
@APIResponse(responseCode = "204", description = "Import a new test")
@RequestBody(content = @Content( mediaType = MediaType.APPLICATION_JSON,
schema = @Schema( type = SchemaType.STRING, implementation = String.class)) )
schema = @Schema( type = SchemaType.STRING, implementation = TestExport.class)) )
@Operation(description="Import a previously exported Test")
void importTest( String testConfig);
void importTest(TestExport testExport);

class TestListing {
@Schema(description = "Array of Test Summaries")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.hyperfoil.tools.horreum.entity;

import java.util.Collection;
import java.util.List;

import io.hyperfoil.tools.horreum.hibernate.JsonBinaryType;
import jakarta.persistence.CollectionTable;
Expand Down Expand Up @@ -67,7 +68,7 @@ public class ExperimentProfileDAO extends PanacheEntityBase {
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name="experiment_comparisons", joinColumns=@JoinColumn(name="profile_id"))
@OrderBy("variable_id, model")
public Collection<ExperimentComparisonDAO> comparisons;
public List<ExperimentComparisonDAO> comparisons;

/* These labels are not used in Horreum but are added to the result event */
@Column(name = "extra_labels", columnDefinition = "jsonb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public static ExperimentProfile from(ExperimentProfileDAO ep) {

public static ExperimentComparison fromExperimentComparison(ExperimentComparisonDAO ec) {
ExperimentComparison dto = new ExperimentComparison();
dto.variableId = ec.getVariableId();
dto.variableName = ec.variable.name;
if(ec.variable != null) {
dto.variableId = ec.variable.id;
dto.variableName = ec.variable.name;
}
dto.config = ec.config;
dto.model = ec.model;

Expand Down Expand Up @@ -56,7 +58,6 @@ public static ExperimentComparisonDAO toExperimentComparison(ExperimentCompariso
ec.model = dto.model;
if(dto.variableId != null && dto.variableId > 0)
ec.setVariableId(dto.variableId);

return ec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public class LabelMapper {
public static Label from(LabelDAO l) {
if(l == null)
return null;
Label dto = new Label();
dto.id = l.id;
dto.name = l.name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
package io.hyperfoil.tools.horreum.mapper;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.hyperfoil.tools.horreum.entity.alerting.VariableDAO;
import io.hyperfoil.tools.horreum.api.alerting.Variable;
import io.hyperfoil.tools.horreum.entity.data.LabelDAO;

import java.util.ArrayList;
import java.util.stream.Collectors;

public class VariableMapper {

public static Variable from(VariableDAO variable) {
return new Variable(variable.id, variable.testId, variable.name, variable.group,
variable.order, variable.labels, variable.calculation,
variable.changeDetection.stream().map(ChangeDetectionMapper::from).collect(Collectors.toSet())
);
public static Variable from(VariableDAO dao) {
Variable v = new Variable();
v.id = dao.id;
v.testId = dao.testId;
v.name = dao.name;
v.group = dao.group;
v.order = dao.order;
v.calculation = dao.calculation;
v.changeDetection =
dao.changeDetection.stream().map(ChangeDetectionMapper::from).collect(Collectors.toSet());
//label
if(dao.labels.isArray()) {
v.labels = new ArrayList<>();
dao.labels.spliterator().forEachRemaining(n -> {
LabelDAO l = LabelDAO.find("name", n.asText()).firstResult();
if(l != null)
v.labels.add( LabelMapper.from(l).name);
});
}
return v;
}

public static VariableDAO to(Variable dto) {
Expand All @@ -21,7 +40,12 @@ public static VariableDAO to(Variable dto) {
v.name = dto.name;
v.group = dto.group;
v.order = dto.order;
v.labels = dto.labels;
if(dto.labels != null && !dto.labels.isEmpty()) {
ArrayNode n = JsonNodeFactory.instance.arrayNode();
for (String l : dto.labels)
n.add(l);
v.labels = n;
}
v.calculation = dto.calculation;
if(dto.changeDetection != null)
v.changeDetection = dto.changeDetection.stream().map(ChangeDetectionMapper::to).collect(Collectors.toSet());
Expand Down
Loading

0 comments on commit 45d0d74

Please sign in to comment.