Skip to content

Add aggregate field crackingTime to AgentAssignment#2329

Merged
s3inlc merged 12 commits into
masterfrom
2297-enhancement-add-crackingtime-on-agentassignment
Jul 15, 2026
Merged

Add aggregate field crackingTime to AgentAssignment#2329
s3inlc merged 12 commits into
masterfrom
2297-enhancement-add-crackingtime-on-agentassignment

Conversation

@novasam23

Copy link
Copy Markdown
Contributor

Fixes #2297

… existing code that did something similar and write tests for checking the correct calculation of total time value
@novasam23 novasam23 linked an issue Jul 14, 2026 that may be closed by this pull request
@novasam23

Copy link
Copy Markdown
Contributor Author

A few things to add:

  • For the AgentAssignment I suggest to call the aggregate crackingTime instead of timeSpent in order to be consistent with how it's called on the Agent model

  • A similar calculation is also done in TaskUtils called getTimeSpentOnTask. However, since this involves potentially multiple agents which results in parallel time spans, the calculation is more complex and hence done entirely in the API layer. A future enhancement might be, to move this to the DB for performance reasons, but I think that would require making the DB access classes capable of DB window functions. Definitely something for the future, if at all.

  • As for the existing aggregate on the Agent, currently crackingTime is implemented in the AgentAPI. One benefit of having that in a utilities class (similar to getTimeSpentOnTask) would be to better test the "business logic" of correctly calculating the time values via PHP tests. Another benefit would be reducing the occurrences of duplicate code, since we now have almost identical calculations for the Agent and AgentAssignment.

  • In short, I'm proposing to move this calculation to TaskUtils and consolidate existing occurrences accordingly.

  • With window functions in the DB access layer, this could be further consolidated into a single generic time span (i.e. solveTime minus dispatchTime) aggregating function, to be used even by the Task entity.

Tests

  • Talking about tests, I did implement a Pytest, but chunk creation and manipulation is very restricted via API, hence it's difficult to create meaningful test cases that cover all sorts of chunk processing constellations.

  • The more relevant test I think is the PHP test, that creates different chunks with different timing constellations, assigned to multiple agents, etc.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a reusable “aggregate cracking time” computation and exposes it as an aggregate field on AgentAssignment (and refactors Agent’s existing aggregate to use the shared implementation), aligning with the enhancement request to aggregate time spent on a specific task by an agent.

Changes:

  • Introduces TaskUtils::getAggregateCrackingTime(agentId, taskId?) to compute summed cracking time from chunk timestamps.
  • Adds an assignment.crackingTime aggregate fieldset to AgentAssignmentAPI.
  • Adds/updates PHPUnit and API tests to validate the new aggregation behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/inc/utils/TaskUtils.php Adds shared aggregate cracking-time helper (agent-wide or agent+task).
src/inc/apiv2/model/AgentAssignmentAPI.php Exposes cracking-time aggregation on AgentAssignment via aggregate fieldsets.
src/inc/apiv2/model/AgentAPI.php Refactors Agent crackingTime aggregation to call the shared TaskUtils helper.
ci/phpunit/inc/utils/TaskUtilsTest.php Adds PHPUnit coverage for the new aggregate cracking-time helper.
ci/apiv2/test_agentassignment.py Adds API-level test for AgentAssignment crackingTime aggregation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/inc/utils/TaskUtils.php Outdated
Comment thread src/inc/apiv2/model/AgentAssignmentAPI.php Outdated
Comment thread src/inc/apiv2/model/AgentAssignmentAPI.php
Comment thread ci/phpunit/inc/utils/TaskUtilsTest.php Outdated
Comment thread ci/phpunit/inc/utils/TaskUtilsTest.php Outdated
Comment on lines +282 to +289
// Calculate reference value via an interval merge algorithm
$totalEnd = $referenceSum = 0;
foreach ($timeSpans as [$currentStart, $currentEnd]) { // Expects list to be sorted by time
$referenceSum = $referenceSum + ($currentEnd - $currentStart) // Add current time span to running total
- max(0, $totalEnd - $currentStart) // Correct for potential overlapping when current start before previous end
+ max(0, $totalEnd - $currentEnd); // Correct for potential overcorrection when current end before previous end
$totalEnd = max($totalEnd, $currentEnd); // Extend window if current end exceeds previous end
}
Comment thread ci/apiv2/test_agentassignment.py Outdated
Comment thread ci/apiv2/test_agentassignment.py Outdated
Comment thread ci/phpunit/inc/utils/TaskUtilsTest.php Outdated
@novasam23 novasam23 marked this pull request as draft July 14, 2026 08:19
novasam23 and others added 8 commits July 14, 2026 10:30
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@s3inlc

s3inlc commented Jul 14, 2026

Copy link
Copy Markdown
Member

I agree with your consolidation, but I think it would be better to move it to AgentUtils and not TaskUtils, as it's agent dependent (and Task only in certain cases). Especially, as this way of do the diff of the sums only works when calculating for one agent, but not if for example we calculate the time which has been spent on task (wall clock time).

@novasam23

Copy link
Copy Markdown
Contributor Author

@s3inlc I moved the code for the cracking time aggregation to the AgentUtils as you suggested, I agree that this class is a better fit.

I also refactored the PHP unit test code accordingly to correspond with this change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread src/inc/apiv2/model/AgentAssignmentAPI.php
Comment thread ci/phpunit/inc/utils/AgentUtilsTest.php Outdated
Comment thread ci/apiv2/test_agentassignment.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@novasam23 novasam23 requested review from jessevz and s3inlc July 15, 2026 12:35
@novasam23 novasam23 marked this pull request as ready for review July 15, 2026 12:35
@s3inlc s3inlc merged commit 6b11e7e into master Jul 15, 2026
14 checks passed
@s3inlc s3inlc deleted the 2297-enhancement-add-crackingtime-on-agentassignment branch July 15, 2026 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENHANCEMENT]: add aggregation on AgentAssignment

3 participants