diff --git a/apps/opik-backend/src/main/java/com/comet/opik/domain/SpanDAO.java b/apps/opik-backend/src/main/java/com/comet/opik/domain/SpanDAO.java index 2cea7a059f..5fea2d7f0f 100644 --- a/apps/opik-backend/src/main/java/com/comet/opik/domain/SpanDAO.java +++ b/apps/opik-backend/src/main/java/com/comet/opik/domain/SpanDAO.java @@ -479,7 +479,10 @@ LEFT JOIN ( private static final String SELECT_BY_ID = """ SELECT *, - duration_millis + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) AS duration_millis FROM spans WHERE id = :id @@ -513,7 +516,10 @@ LEFT JOIN ( last_updated_at, created_by, last_updated_by, - duration_millis + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) AS duration_millis FROM spans WHERE project_id = :project_id AND workspace_id = :workspace_id @@ -636,7 +642,10 @@ AND id in ( workspace_id, project_id, id, - duration_millis, + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) AS duration_millis, if(length(input) > 0, 1, 0) as input_count, if(length(output) > 0, 1, 0) as output_count, if(length(metadata) > 0, 1, 0) as metadata_count, diff --git a/apps/opik-backend/src/main/java/com/comet/opik/domain/TraceDAO.java b/apps/opik-backend/src/main/java/com/comet/opik/domain/TraceDAO.java index 66b69bad40..b525679585 100644 --- a/apps/opik-backend/src/main/java/com/comet/opik/domain/TraceDAO.java +++ b/apps/opik-backend/src/main/java/com/comet/opik/domain/TraceDAO.java @@ -273,7 +273,10 @@ INSERT INTO traces ( FROM ( SELECT *, - duration_millis + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) AS duration_millis FROM traces WHERE workspace_id = :workspace_id AND id = :id @@ -321,7 +324,10 @@ LEFT JOIN ( last_updated_at, created_by, last_updated_by, - duration_millis + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) AS duration_millis FROM traces WHERE project_id = :project_id AND workspace_id = :workspace_id @@ -620,7 +626,10 @@ LEFT JOIN ( workspace_id, project_id, id, - duration_millis, + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) AS duration_millis, if(length(input) > 0, 1, 0) as input_count, if(length(output) > 0, 1, 0) as output_count, if(length(metadata) > 0, 1, 0) as metadata_count, diff --git a/apps/opik-backend/src/main/java/com/comet/opik/domain/filter/FilterQueryBuilder.java b/apps/opik-backend/src/main/java/com/comet/opik/domain/filter/FilterQueryBuilder.java index 8edad0657d..b2a3d66aff 100644 --- a/apps/opik-backend/src/main/java/com/comet/opik/domain/filter/FilterQueryBuilder.java +++ b/apps/opik-backend/src/main/java/com/comet/opik/domain/filter/FilterQueryBuilder.java @@ -42,7 +42,12 @@ public class FilterQueryBuilder { private static final String USAGE_PROMPT_TOKENS_ANALYTICS_DB = "usage['prompt_tokens']"; private static final String USAGE_TOTAL_TOKENS_ANALYTICS_DB = "usage['total_tokens']"; private static final String VALUE_ANALYTICS_DB = "value"; - private static final String DURATION_ANALYTICS_DB = "duration_millis"; + private static final String DURATION_ANALYTICS_DB = """ + if(end_time IS NOT NULL AND start_time IS NOT NULL + AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), + (dateDiff('microsecond', start_time, end_time) / 1000.0), + NULL) + """; private static final Map> ANALYTICS_DB_OPERATOR_MAP = new EnumMap<>(Map.of( Operator.CONTAINS, new EnumMap<>(Map.of( diff --git a/apps/opik-backend/src/main/resources/liquibase/db-app-analytics/migrations/000009_add_duration_columns.sql b/apps/opik-backend/src/main/resources/liquibase/db-app-analytics/migrations/000009_add_duration_columns.sql deleted file mode 100644 index bae646985f..0000000000 --- a/apps/opik-backend/src/main/resources/liquibase/db-app-analytics/migrations/000009_add_duration_columns.sql +++ /dev/null @@ -1,19 +0,0 @@ ---liquibase formatted sql ---changeset idoberko2:add_duration_columns - -ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.spans - ADD COLUMN IF NOT EXISTS duration_millis Nullable(Float64) MATERIALIZED - if(end_time IS NOT NULL AND start_time IS NOT NULL - AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), - (dateDiff('microsecond', start_time, end_time) / 1000.0), - NULL); - -ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.traces - ADD COLUMN IF NOT EXISTS duration_millis Nullable(Float64) MATERIALIZED - if(end_time IS NOT NULL AND start_time IS NOT NULL - AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)), - (dateDiff('microsecond', start_time, end_time) / 1000.0), - NULL); - ---rollback ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.spans DROP COLUMN IF EXISTS duration_millis; ---rollback ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.traces DROP COLUMN IF EXISTS duration_millis; \ No newline at end of file