From 1a0a23dd7ee96b271f89570b17a34fee946d2967 Mon Sep 17 00:00:00 2001 From: Thiago Hora Date: Thu, 12 Dec 2024 15:38:39 +0100 Subject: [PATCH] Fix calc --- .../main/java/com/comet/opik/api/Span.java | 11 +++------ .../main/java/com/comet/opik/api/Trace.java | 11 +++------ .../com/comet/opik/utils/DurationUtils.java | 23 +++++++++++++++++++ .../resources/v1/priv/SpansResourceTest.java | 2 -- 4 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 apps/opik-backend/src/main/java/com/comet/opik/utils/DurationUtils.java diff --git a/apps/opik-backend/src/main/java/com/comet/opik/api/Span.java b/apps/opik-backend/src/main/java/com/comet/opik/api/Span.java index 294339516c..6e664cafa8 100644 --- a/apps/opik-backend/src/main/java/com/comet/opik/api/Span.java +++ b/apps/opik-backend/src/main/java/com/comet/opik/api/Span.java @@ -1,6 +1,7 @@ package com.comet.opik.api; import com.comet.opik.domain.SpanType; +import com.comet.opik.utils.DurationUtils; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonView; @@ -14,7 +15,6 @@ import lombok.Builder; import java.math.BigDecimal; -import java.time.Duration; import java.time.Instant; import java.util.List; import java.util.Map; @@ -75,13 +75,8 @@ public static class Public { @JsonProperty @JsonView({Span.View.Public.class}) - @Schema(accessMode = Schema.AccessMode.READ_ONLY) + @Schema(accessMode = Schema.AccessMode.READ_ONLY, description = "Duration in milliseconds as a decimal number to support sub-millisecond precision") public Double duration() { - if (endTime == null) { - return null; - } - - long micros = Duration.between(startTime, endTime).toNanos() / 1_000; - return micros / 1_000.0; + return DurationUtils.getDurationInSeconds(startTime, endTime); } } diff --git a/apps/opik-backend/src/main/java/com/comet/opik/api/Trace.java b/apps/opik-backend/src/main/java/com/comet/opik/api/Trace.java index c0bb1640e2..808abdecfc 100644 --- a/apps/opik-backend/src/main/java/com/comet/opik/api/Trace.java +++ b/apps/opik-backend/src/main/java/com/comet/opik/api/Trace.java @@ -1,5 +1,6 @@ package com.comet.opik.api; +import com.comet.opik.utils.DurationUtils; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonView; @@ -13,7 +14,6 @@ import lombok.Builder; import java.math.BigDecimal; -import java.time.Duration; import java.time.Instant; import java.util.List; import java.util.Map; @@ -70,13 +70,8 @@ public static class Public { @JsonProperty @JsonView({Span.View.Public.class}) - @Schema(accessMode = Schema.AccessMode.READ_ONLY) + @Schema(accessMode = Schema.AccessMode.READ_ONLY, description = "Duration in milliseconds as a decimal number to support sub-millisecond precision") public Double duration() { - if (endTime == null) { - return null; - } - - long micros = Duration.between(startTime, endTime).toNanos() / 1_000; - return micros / 1_000.0; + return DurationUtils.getDurationInSeconds(startTime, endTime); } } diff --git a/apps/opik-backend/src/main/java/com/comet/opik/utils/DurationUtils.java b/apps/opik-backend/src/main/java/com/comet/opik/utils/DurationUtils.java new file mode 100644 index 0000000000..2fd55f9fd5 --- /dev/null +++ b/apps/opik-backend/src/main/java/com/comet/opik/utils/DurationUtils.java @@ -0,0 +1,23 @@ +package com.comet.opik.utils; + +import lombok.NonNull; +import lombok.experimental.UtilityClass; + +import java.time.Duration; +import java.time.Instant; + +@UtilityClass +public class DurationUtils { + + public static final Double TIME_UNIT = 1_000.0; + + public static Double getDurationInSeconds(@NonNull Instant startTime, Instant endTime) { + if (endTime == null) { + return null; + } + + long micros = Duration.between(startTime, endTime).toNanos() / TIME_UNIT.longValue(); + return micros / TIME_UNIT; + } + +} diff --git a/apps/opik-backend/src/test/java/com/comet/opik/api/resources/v1/priv/SpansResourceTest.java b/apps/opik-backend/src/test/java/com/comet/opik/api/resources/v1/priv/SpansResourceTest.java index a02ceec357..8989c980fa 100644 --- a/apps/opik-backend/src/test/java/com/comet/opik/api/resources/v1/priv/SpansResourceTest.java +++ b/apps/opik-backend/src/test/java/com/comet/opik/api/resources/v1/priv/SpansResourceTest.java @@ -2837,8 +2837,6 @@ void getSpansByProject__whenFilterByDuration__thenReturnSpansFiltered(Operator o .value(String.valueOf(duration)) .build()); - spans.forEach(span -> log.info("Span: {}", span)); - getAndAssertPage(workspaceName, projectName, filters, spans, expectedSpans, unexpectedSpans, apiKey); }