-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-419] add last updated trace at to projects (#849)
* OPIK-419 insert single and batch traces * OPIK-419 null timestamp * OPIK-419 create single and batch * OPIK-419 coverage create single trace * OPIK-419 add a couple of todos to not forget * OPIK-419 refactor * OPIK-419 refactor * OPIK-419 cover batch * OPIK-419 update trace failing test * OPIK-419 update trace failing test green * OPIK-419 remove comment * OPIK-419 fix broken test * OPIK-419 projectdao not public * OPIK-419 pr comments * OPIK-419 cover new functionality in service test * OPIK-419 handle async update to last_trace_updated_at * OPIK-419 default null
- Loading branch information
Showing
11 changed files
with
338 additions
and
16 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
apps/opik-backend/src/main/java/com/comet/opik/api/events/TracesCreated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.comet.opik.api.events; | ||
|
||
import com.comet.opik.infrastructure.events.BaseEvent; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.experimental.Accessors; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
@Accessors(fluent = true) | ||
public class TracesCreated extends BaseEvent { | ||
private final @NonNull Set<UUID> projectIds; | ||
|
||
public TracesCreated(@NonNull Set<UUID> projectIds, @NonNull String workspaceId, @NonNull String userName) { | ||
super(workspaceId, userName); | ||
this.projectIds = projectIds; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/opik-backend/src/main/java/com/comet/opik/api/events/TracesUpdated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.comet.opik.api.events; | ||
|
||
import com.comet.opik.infrastructure.events.BaseEvent; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.experimental.Accessors; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
@Accessors(fluent = true) | ||
public class TracesUpdated extends BaseEvent { | ||
private final @NonNull Set<UUID> projectIds; | ||
|
||
public TracesUpdated(@NonNull Set<UUID> projectIds, @NonNull String workspaceId, @NonNull String userName) { | ||
super(workspaceId, userName); | ||
this.projectIds = projectIds; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ik-backend/src/main/java/com/comet/opik/api/resources/v1/events/ProjectEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.comet.opik.api.resources.v1.events; | ||
|
||
import com.comet.opik.api.ProjectIdLastUpdated; | ||
import com.comet.opik.api.events.TracesCreated; | ||
import com.comet.opik.api.events.TracesUpdated; | ||
import com.comet.opik.domain.ProjectService; | ||
import com.comet.opik.domain.TraceService; | ||
import com.google.common.eventbus.EventBus; | ||
import com.google.common.eventbus.Subscribe; | ||
import jakarta.inject.Inject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import reactor.core.publisher.Mono; | ||
import ru.vyarus.dropwizard.guice.module.installer.feature.eager.EagerSingleton; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
@EagerSingleton | ||
@Slf4j | ||
public class ProjectEventListener { | ||
private final ProjectService projectService; | ||
private final TraceService traceService; | ||
|
||
@Inject | ||
public ProjectEventListener(EventBus eventBus, ProjectService projectService, TraceService traceService) { | ||
this.projectService = projectService; | ||
this.traceService = traceService; | ||
eventBus.register(this); | ||
} | ||
|
||
@Subscribe | ||
public void onTracesCreated(TracesCreated event) { | ||
updateProjectsLastUpdatedTraceAt(event.workspaceId(), event.projectIds()); | ||
} | ||
|
||
@Subscribe | ||
public void onTracesUpdated(TracesUpdated event) { | ||
updateProjectsLastUpdatedTraceAt(event.workspaceId(), event.projectIds()); | ||
} | ||
|
||
private void updateProjectsLastUpdatedTraceAt(String workspaceId, Set<UUID> projectIds) { | ||
log.info("Recording last traces for projects '{}'", projectIds); | ||
|
||
traceService.getLastUpdatedTraceAt(projectIds, workspaceId) | ||
.flatMap(lastTraceByProjectId -> Mono.fromRunnable(() -> projectService.recordLastUpdatedTrace( | ||
workspaceId, | ||
lastTraceByProjectId.entrySet().stream() | ||
.map(entry -> new ProjectIdLastUpdated(entry.getKey(), entry.getValue())).toList()))) | ||
.block(); | ||
|
||
log.info("Recorded last traces for projects '{}'", projectIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...resources/liquibase/db-app-state/migrations/000006_add_projects_last_updated_trace_at.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--liquibase formatted sql | ||
--changeset idoberko2:add_projects_last_updated_trace_at | ||
|
||
ALTER TABLE projects ADD COLUMN last_updated_trace_at TIMESTAMP(6) DEFAULT NULL; | ||
|
||
--rollback ALTER TABLE projects DROP COLUMN last_updated_trace_at; |
14 changes: 14 additions & 0 deletions
14
apps/opik-backend/src/test/java/com/comet/opik/TestComparators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.comet.opik; | ||
|
||
import java.time.Instant; | ||
|
||
public class TestComparators { | ||
public static int compareMicroNanoTime(Instant i1, Instant i2) { | ||
// Calculate the difference in nanoseconds | ||
long nanoDifference = Math.abs(i1.getNano() - i2.getNano()); | ||
if (nanoDifference < 1_000) { | ||
return 0; // Consider equal if within a microsecond | ||
} | ||
return i1.compareTo(i2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.