Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPIK-58 Add trace usage #332

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

Expand All @@ -35,6 +36,7 @@ public record Trace(
@JsonView({Trace.View.Public.class, Trace.View.Write.class}) JsonNode output,
@JsonView({Trace.View.Public.class, Trace.View.Write.class}) JsonNode metadata,
@JsonView({Trace.View.Public.class, Trace.View.Write.class}) Set<String> tags,
@JsonView({Trace.View.Public.class}) @Schema(accessMode = Schema.AccessMode.READ_ONLY) Map<String, Long> usage,
@JsonView({Trace.View.Public.class}) @Schema(accessMode = Schema.AccessMode.READ_ONLY) Instant createdAt,
@JsonView({Trace.View.Public.class}) @Schema(accessMode = Schema.AccessMode.READ_ONLY) Instant lastUpdatedAt,
@JsonView({Trace.View.Public.class}) @Schema(accessMode = Schema.AccessMode.READ_ONLY) String createdBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.UUID;

@Mapper
Expand All @@ -15,6 +16,8 @@ public interface FeedbackScoreMapper {

FeedbackScore toFeedbackScore(FeedbackScoreBatchItem feedbackScoreBatchItem);

List<FeedbackScore> toFeedbackScores(List<FeedbackScoreBatchItem> feedbackScoreBatchItems);

@Mapping(target = "id", source = "entityId")
FeedbackScoreBatchItem toFeedbackScoreBatchItem(UUID entityId, String projectName, FeedbackScore feedbackScore);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -238,17 +239,38 @@ INSERT INTO traces (

private static final String SELECT_BY_ID = """
SELECT
*
FROM
traces
WHERE id = :id
AND workspace_id = :workspace_id
ORDER BY last_updated_at DESC
LIMIT 1
t.*,
sumMap(s.usage) as usage
FROM (
SELECT
*
FROM traces
WHERE workspace_id = :workspace_id
AND id = :id
ORDER BY id DESC, last_updated_at DESC
LIMIT 1 BY id
) AS t
LEFT JOIN (
SELECT
trace_id,
usage
FROM spans
WHERE workspace_id = :workspace_id
AND trace_id = :id
ORDER BY id DESC, last_updated_at DESC
LIMIT 1 BY id
) AS s ON t.id = s.trace_id
GROUP BY
t.*
ORDER BY t.id DESC
;
""";

private static final String SELECT_BY_PROJECT_ID = """
SELECT
t.*,
sumMap(s.usage) as usage
FROM (
SELECT
*
FROM traces
Expand All @@ -263,6 +285,7 @@ AND id in (
SELECT *
FROM feedback_scores
WHERE entity_type = 'trace'
AND workspace_id = :workspace_id
AND project_id = :project_id
ORDER BY entity_id DESC, last_updated_at DESC
LIMIT 1 BY entity_id, name
Expand All @@ -274,6 +297,20 @@ AND id in (
ORDER BY id DESC, last_updated_at DESC
LIMIT 1 BY id
LIMIT :limit OFFSET :offset
) AS t
LEFT JOIN (
SELECT
trace_id,
usage
FROM spans
WHERE workspace_id = :workspace_id
AND project_id = :project_id
ORDER BY id DESC, last_updated_at DESC
LIMIT 1 BY id
) AS s ON t.id = s.trace_id
GROUP BY
t.*
ORDER BY t.id DESC
;
""";

Expand Down Expand Up @@ -306,6 +343,7 @@ AND id in (
SELECT *
FROM feedback_scores
WHERE entity_type = 'trace'
AND workspace_id = :workspace_id
AND project_id = :project_id
ORDER BY entity_id DESC, last_updated_at DESC
LIMIT 1 BY entity_id, name
Expand All @@ -314,7 +352,7 @@ AND id in (
HAVING <feedback_scores_filters>
)
<endif>
ORDER BY last_updated_at DESC
ORDER BY id DESC, last_updated_at DESC
LIMIT 1 BY id
) AS latest_rows
;
Expand Down Expand Up @@ -627,6 +665,7 @@ private Publisher<Trace> mapToDto(Result result) {
.collect(Collectors.toSet()))
.filter(it -> !it.isEmpty())
.orElse(null))
.usage(row.get("usage", Map.class))
.createdAt(row.get("created_at", Instant.class))
.lastUpdatedAt(row.get("last_updated_at", Instant.class))
.createdBy(row.get("created_by", String.class))
Expand Down
Loading