Skip to content

Commit

Permalink
[OPIK-441] Split feedback score name endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagohora committed Nov 22, 2024
1 parent 0469227 commit 18ae3cd
Show file tree
Hide file tree
Showing 20 changed files with 503 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
import java.time.Instant;

@Builder(toBuilder = true)
public record DataPoint(Instant time, Number value) {}
public record DataPoint(Instant time, Number value) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public record ProjectMetricRequest(
@NonNull MetricType metricType,
@NonNull TimeInterval interval,
Instant intervalStart,
Instant intervalEnd) {}
Instant intervalEnd) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Builder;

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

Expand All @@ -27,5 +26,6 @@ public record ProjectMetricResponse(
@Builder(toBuilder = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record Results(String name, List<DataPoint> data) {}
public record Results(String name, List<DataPoint> data) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.comet.opik.api.queryparams;

import jakarta.ws.rs.ext.ParamConverter;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class UUIDListParamConverter implements ParamConverter<List<UUID>> {

@Override
public List<UUID> fromString(String value) {
if (value == null || value.trim().isEmpty()) {
return List.of(); // Return an empty list if no value is provided
}
return Arrays.stream(value.split(","))
.map(String::trim)
.map(UUID::fromString)
.collect(Collectors.toList());
}

@Override
public String toString(List<UUID> value) {
return value.stream()
.map(UUID::toString)
.collect(Collectors.joining(","));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.comet.opik.api.queryparams;

import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;

import java.lang.reflect.Type;
import java.util.List;

@Provider
public class UUIDListParamConverterProvider implements ParamConverterProvider {

@Override
@SuppressWarnings("unchecked")
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType,
java.lang.annotation.Annotation[] annotations) {
if (rawType.equals(List.class) && genericType.getTypeName().contains("UUID")) {
return (ParamConverter<T>) new UUIDListParamConverter();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import com.comet.opik.api.ExperimentItemsDelete;
import com.comet.opik.api.ExperimentSearchCriteria;
import com.comet.opik.api.ExperimentsDelete;
import com.comet.opik.api.FeedbackDefinition;
import com.comet.opik.api.FeedbackScoreNames;
import com.comet.opik.domain.ExperimentItemService;
import com.comet.opik.domain.ExperimentService;
import com.comet.opik.domain.FeedbackScoreService;
import com.comet.opik.domain.IdGenerator;
import com.comet.opik.domain.Streamer;
import com.comet.opik.infrastructure.auth.RequestContext;
Expand Down Expand Up @@ -48,6 +51,7 @@
import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.server.ChunkedOutput;

import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
Expand All @@ -66,6 +70,7 @@ public class ExperimentsResource {

private final @NonNull ExperimentService experimentService;
private final @NonNull ExperimentItemService experimentItemService;
private final @NonNull FeedbackScoreService feedbackScoreService;
private final @NonNull Provider<RequestContext> requestContext;
private final @NonNull IdGenerator idGenerator;
private final @NonNull Streamer streamer;
Expand Down Expand Up @@ -241,4 +246,28 @@ public Response deleteExperimentItems(
log.info("Deleted experiment items, count '{}'", request.ids().size());
return Response.noContent().build();
}

@GET
@Path("/feedback-scores/names")
@Operation(operationId = "findFeedbackScoreNames", summary = "Find Feedback Score names", description = "Find Feedback Score names", responses = {
@ApiResponse(responseCode = "200", description = "Feedback Scores resource", content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class))))
})
@JsonView({FeedbackDefinition.View.Public.class})
public Response findFeedbackScoreNames(@QueryParam("experiment_ids") List<UUID> experimentIds) {

String workspaceId = requestContext.get().getWorkspaceId();

log.info("Find feedback score names by experiment_ids '{}', on workspaceId '{}'",
experimentIds, workspaceId);
FeedbackScoreNames feedbackScoreNames = feedbackScoreService
.getExperimentsFeedbackScoreNames(experimentIds)
.map(names -> names.stream().map(FeedbackScoreNames.ScoreName::new).toList())
.map(FeedbackScoreNames::new)
.contextWrite(ctx -> setRequestContext(ctx, requestContext))
.block();
log.info("Found feedback score names '{}' by experiment_ids '{}', on workspaceId '{}'",
feedbackScoreNames.scores().size(), experimentIds, workspaceId);

return Response.ok(feedbackScoreNames).build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.codahale.metrics.annotation.Timed;
import com.comet.opik.api.DeleteFeedbackScore;
import com.comet.opik.api.FeedbackDefinition;
import com.comet.opik.api.FeedbackScore;
import com.comet.opik.api.FeedbackScoreBatch;
import com.comet.opik.api.FeedbackScoreNames;
import com.comet.opik.api.Span;
import com.comet.opik.api.SpanBatch;
import com.comet.opik.api.SpanSearchCriteria;
Expand All @@ -19,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
Expand All @@ -29,6 +32,7 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
Expand Down Expand Up @@ -258,4 +262,33 @@ public Response scoreBatchOfSpans(
return Response.noContent().build();
}

@GET
@Path("/feedback-scores/names")
@Operation(operationId = "findFeedbackScoreNames", summary = "Find Feedback Score names", description = "Find Feedback Score names", responses = {
@ApiResponse(responseCode = "200", description = "Feedback Scores resource", content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class))))
})
@JsonView({FeedbackDefinition.View.Public.class})
public Response findFeedbackScoreNames(@QueryParam("project_id") UUID projectId,
@QueryParam("type") SpanType type) {

if (projectId == null) {
throw new BadRequestException("project_id must be provided");
}

String workspaceId = requestContext.get().getWorkspaceId();

log.info("Find feedback score names by project_id '{}', on workspaceId '{}'",
projectId, workspaceId);
FeedbackScoreNames feedbackScoreNames = feedbackScoreService
.getSpanFeedbackScoreNames(projectId, type)
.map(names -> names.stream().map(FeedbackScoreNames.ScoreName::new).toList())
.map(FeedbackScoreNames::new)
.contextWrite(ctx -> setRequestContext(ctx, requestContext))
.block();
log.info("Found feedback score names '{}' by project_id '{}', on workspaceId '{}'",
feedbackScoreNames.scores().size(), projectId, workspaceId);

return Response.ok(feedbackScoreNames).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.codahale.metrics.annotation.Timed;
import com.comet.opik.api.DeleteFeedbackScore;
import com.comet.opik.api.FeedbackDefinition;
import com.comet.opik.api.FeedbackScore;
import com.comet.opik.api.FeedbackScoreBatch;
import com.comet.opik.api.FeedbackScoreNames;
import com.comet.opik.api.Trace;
import com.comet.opik.api.Trace.TracePage;
import com.comet.opik.api.TraceBatch;
Expand All @@ -20,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
Expand All @@ -29,6 +32,7 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
Expand Down Expand Up @@ -290,4 +294,32 @@ public Response scoreBatchOfTraces(
return Response.noContent().build();
}

@GET
@Path("/feedback-scores/names")
@Operation(operationId = "findFeedbackScoreNames", summary = "Find Feedback Score names", description = "Find Feedback Score names", responses = {
@ApiResponse(responseCode = "200", description = "Feedback Scores resource", content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class))))
})
@JsonView({FeedbackDefinition.View.Public.class})
public Response findFeedbackScoreNames(@QueryParam("project_id") UUID projectId) {

if (projectId == null) {
throw new BadRequestException("project_id must be provided");
}

String workspaceId = requestContext.get().getWorkspaceId();

log.info("Find feedback score names by project_id '{}', on workspaceId '{}'",
projectId, workspaceId);
FeedbackScoreNames feedbackScoreNames = feedbackScoreService
.getTraceFeedbackScoreNames(projectId)
.map(names -> names.stream().map(FeedbackScoreNames.ScoreName::new).toList())
.map(FeedbackScoreNames::new)
.contextWrite(ctx -> setRequestContext(ctx, requestContext))
.block();
log.info("Found feedback score names '{}' by project_id '{}', on workspaceId '{}'",
feedbackScoreNames.scores().size(), projectId, workspaceId);

return Response.ok(feedbackScoreNames).build();
}

}
Loading

0 comments on commit 18ae3cd

Please sign in to comment.