-
[OPIK-2936] [BE] [FE] Display Token & Cost Columns in Experiment Items table (#4061)
发布于
2025-11-20 13:02:28 +00:00 - [OPIK-2936] [FE][BE] Add Total tokens and Estimated cost columns to Experiment Items table
Add display and sorting capabilities for token usage and cost metrics in the experiment items comparison view.
Features:
- Display Total tokens and Estimated cost columns in experiment items table
- Sort by both metrics (ascending/descending)
- AVG/SUM statistics in column headers
- Support for multi-experiment comparison view with vertical split cells
Frontend Changes:
- Updated ExperimentItem interface to include usage and total_estimated_cost fields
- Enhanced CostCell.Compare component with generic accessor support for flexible data access
- Added columns to ExperimentItemsTab with proper formatters and statistics
Backend Changes:
- Extended SQL queries to aggregate usage and cost data from spans
- Added USAGE_TOTAL_TOKENS and TOTAL_ESTIMATED_COST to sortable fields
- Updated StatsMapper to calculate usage statistics (total, prompt, completion tokens)
- Added validation for new sortable fields in SortingFactoryDatasets
- Implemented comprehensive test coverage for sorting functionality
Testing:
- Added backend integration tests for sorting by total_estimated_cost and usage.total_tokens
- Verified multi-experiment comparison rendering
- Tested null/missing data handling
Note: Filtering capability intentionally excluded due to complexity of filtering aggregated fields.
Future work tracked in OPIK-3115.-
Export FormatCostOptions type in money.ts for improved type accessibility
-
Remove unused statisticDataFormater from the Estimated cost column in ExperimentItemsTab component.
-
Revision 2: Refactor map processing into reusable helper method
Extract duplicated map processing logic from mapExperimentItemsStats into
a parameterized helper method (addMapStats) to follow DRY principle.Addresses PR review feedback from @andrescrz about code duplication between
feedbackScoresMap and usageMap processing.Changes:
- Add addMapStats() helper method with proper Javadoc
- Simplify both feedbackScoresMap and usageMap calls to use helper
- No behavioral changes, pure refactoring
- Revision 3: Extend map processing refactoring to mapProjectStats method
Apply the same DRY principle to the mapProjectStats method, which also had
duplicated map processing logic for usage and feedbackScores.Now both mapProjectStats and mapExperimentItemsStats use the same reusable
addMapStats helper method, completely eliminating the copy-paste pattern
that was mentioned in the PR review.The helper method now takes Row and fieldName directly, making call sites
even cleaner by eliminating redundant local variables and field name repetition.Changes:
- Refactor usage and feedbackScores processing in mapProjectStats
- Refactor feedbackScoresMap and usageMap processing in mapExperimentItemsStats
- Update addMapStats to accept Row and extract map internally
- All call sites now use: addMapStats(row, FIELD_NAME, stats)
- Single @SuppressWarnings at helper method level (not at call sites)
- Revision 4: Refactor test code to use batch operations
Address @andrescrz's review feedback to use PodamFactoryUtils.manufacturePojoList
and batchCreateSpans for better test code organization.Changes:
- Replace individual trace loops with PodamFactoryUtils.manufacturePojoList().limit(3)
- Replace individual createSpan() calls with spanResourceClient.batchCreateSpans()
- More concise and readable test code
- Follows project best practices for test data generation
Affected tests:
- sortByTotalEstimatedCost__whenDescendingOrder__thenReturnSorted
- sortByUsageTotalTokens__whenAscendingOrder__thenReturnSorted
下载附件