Skip to content

Commit

Permalink
metric id tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eduwercamacaro committed Feb 24, 2025
1 parent 51f8440 commit 30056a4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,38 @@ public class MetricIdModel extends MetadataId<MetricId, Metric, MetricModel> {
private WfSpecIdModel wfSpecId;
private ThreadSpecReferenceModel threadSpecReference;
private MetricType metricType;
private MetricId.ReferenceCase referenceCase;

public MetricIdModel() {}

public MetricIdModel(MeasurableObject object, MetricType type) {
this.object = object;
this.metricType = type;
this.referenceCase = MetricId.ReferenceCase.OBJECT;
}

public MetricIdModel(NodeReferenceModel nodeReference, MetricType type) {
this.nodeReference = nodeReference;
this.metricType = type;
this.referenceCase = MetricId.ReferenceCase.NODE;
}

public MetricIdModel(WfSpecIdModel wfSpecId, MetricType type) {
this.wfSpecId = wfSpecId;
this.metricType = type;
this.referenceCase = MetricId.ReferenceCase.WF_SPEC_ID;
}

public MetricIdModel(ThreadSpecReferenceModel threadSpecReference, MetricType type) {
this.threadSpecReference = threadSpecReference;
this.metricType = type;
this.referenceCase = MetricId.ReferenceCase.REFERENCE_NOT_SET;
}

@Override
public void initFrom(Message proto, ExecutionContext context) throws LHSerdeError {
MetricId p = (MetricId) proto;
this.referenceCase = p.getReferenceCase();
this.object = p.hasObject() ? p.getObject() : null;
this.nodeReference =
p.hasNode() ? LHSerializable.fromProto(p.getNode(), NodeReferenceModel.class, context) : null;
Expand Down Expand Up @@ -86,15 +92,31 @@ public Class<MetricId> getProtoBaseClass() {

@Override
public String toString() {
return LHUtil.getCompositeId(measurable.toString(), metricType.toString());
return switch (referenceCase) {
case OBJECT -> LHUtil.getCompositeId(String.valueOf(referenceCase.getNumber()), String.valueOf(object.getNumber()), metricType.toString());
case WF_SPEC_ID ->LHUtil.getCompositeId(String.valueOf(referenceCase.getNumber()), wfSpecId.toString(), metricType.toString());
case THREAD_SPEC -> LHUtil.getCompositeId(String.valueOf(referenceCase.getNumber()), threadSpecReference.toString(), metricType.toString());
case NODE -> LHUtil.getCompositeId(String.valueOf(referenceCase.getNumber()), nodeReference.toString(), metricType.toString());
default -> throw new IllegalStateException("Unexpected value: " + referenceCase);
};
}

@Override
public void initFromString(String storeKey) {
MeasurableObject measurable = MeasurableObject.valueOf(storeKey.split("/")[0]);
MetricType type = MetricType.valueOf(storeKey.split("/")[1]);
this.measurable = measurable;
this.metricType = type;
String[] parts = storeKey.split("/");
this.referenceCase = MetricId.ReferenceCase.forNumber(Integer.parseInt(parts[0]));
switch (referenceCase) {
case OBJECT -> {
this.object = MeasurableObject.forNumber(Integer.parseInt(parts[1]));
this.metricType = MetricType.valueOf(parts[2]);
}
case WF_SPEC_ID -> {
this.wfSpecId = new WfSpecIdModel();
this.wfSpecId.initFromString(storeKey.substring(parts[0].length() + 1, storeKey.indexOf(parts[parts.length - 1]) - 1));
this.metricType = MetricType.valueOf(parts[parts.length - 1]);
}
case null, default -> throw new IllegalStateException("Unexpected value: " + referenceCase);
}
}

@Override
Expand All @@ -104,6 +126,6 @@ public GetableClassEnum getType() {

@Override
public Optional<String> getPartitionKey() {
return Optional.of(LHUtil.getCompositeId(measurable.toString(), metricType.toString()));
return Optional.of(this.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.protobuf.Message;
import io.littlehorse.common.LHSerializable;
import io.littlehorse.common.util.LHUtil;
import io.littlehorse.sdk.common.exception.LHSerdeError;
import io.littlehorse.sdk.common.proto.ThreadSpecReference;
import io.littlehorse.server.streams.topology.core.ExecutionContext;
Expand Down Expand Up @@ -44,4 +45,16 @@ public void initFrom(Message proto, ExecutionContext context) throws LHSerdeErro
public Class<ThreadSpecReference> getProtoBaseClass() {
return ThreadSpecReference.class;
}

@Override
public String toString() {
if(threadNumber != null ) {
return LHUtil.getCompositeId(wfSpecId.toString(), threadNumber.toString());
} else {
return LHUtil.getCompositeId(wfSpecId.toString());
}
}

public void initFromKeyString(String keyString) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public class PutMetricRequestModel extends MetadataSubCommand<PutMetricRequest>
@Override
public void initFrom(Message proto, ExecutionContext context) throws LHSerdeError {
PutMetricRequest p = (PutMetricRequest) proto;
this.measurable = p.getMeasurable();
// this.measurable = p.getMeasurable();
this.metricType = p.getType();
this.windowLength = Duration.ofSeconds(p.getWindowLength().getSeconds());
}

@Override
public PutMetricRequest.Builder toProto() {
return PutMetricRequest.newBuilder()
.setMeasurable(measurable)
// .setMeasurable(measurable)
.setType(metricType)
.setWindowLength(com.google.protobuf.Duration.newBuilder()
.setSeconds(windowLength.getSeconds())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.littlehorse.common.model.getable.objectId;


import io.littlehorse.sdk.common.proto.MetricType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class MetricIdTest {

@Test
public void shouldSerializeToStringForWfSpecMetrics() {
WfSpecIdModel wfSpecId = new WfSpecIdModel("test-wf", 0, 1);
MetricIdModel expected = new MetricIdModel(wfSpecId, MetricType.AVG);
MetricIdModel deserialized = new MetricIdModel();
deserialized.initFromString(expected.toString());
Assertions.assertEquals(expected, deserialized);
}

@Test
public void shouldSerializeToStringForMeasurableObject() {
WfSpecIdModel wfSpecId = new WfSpecIdModel("test-wf", 0, 1);
MetricIdModel expected = new MetricIdModel(wfSpecId, MetricType.AVG);
MetricIdModel deserialized = new MetricIdModel();
deserialized.initFromString(expected.toString());
Assertions.assertEquals(expected, deserialized);
}


}

0 comments on commit 30056a4

Please sign in to comment.