Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using dedicated event to store working time #7958

Merged
merged 42 commits into from
Jun 3, 2024
Merged

Conversation

bsekachev
Copy link
Member

@bsekachev bsekachev commented May 28, 2024

Motivation and context

  • Parsing JSON payloads to get working_time in general leads to low performance in Clickhouse requests. This patch will not fix it right now, but with this patch, after a period of time we may switch to new quick approach to calculate working time.
  • There will not be a lot of send:working_time events, we may store this scope of events for a longer time (e.g. 5 years instead of one by default).
  • Finally storing working time in such events like click:element or send:exception, or debug:info seems not logical.
  • Also, the history showed, that as result in different bugs, these events may sometime lose information about job_id, task_id, etc.

Resolved #7884

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • New Features

    • Introduced automatic working time tracking per job, enhancing time management and reporting.
  • Bug Fixes

    • Improved event processing to ensure accurate timestamp updates and payload handling.
  • Refactor

    • Streamlined internal logic for handling client events and working time calculations.
  • Security

    • Added proper permission handling to prevent unauthorized access.

Copy link
Contributor

coderabbitai bot commented May 28, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce a new mechanism for handling and generating working time events in the CVAT application. This involves adding new methods and constants, refactoring existing code for better event handling, and importing necessary modules. The updates enhance the functionality to process client events more effectively and generate corresponding working time events.

Changes

File(s) Change Summary
cvat/apps/events/serializers.py Added imports, constants, and methods for working time event generation. Refactored logic for handling events.
cvat/apps/events/views.py Imported PermissionDenied from rest_framework.exceptions.
cvat/apps/events/handlers.py Added imports and a new function handle_client_events_push for processing client events and generating working time events.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant Server
    participant Serializer
    participant Handler

    Client->>Server: Push client events
    Server->>Handler: handle_client_events_push(request, data)
    Handler->>Serializer: Deserialize events
    Serializer->>Handler: Return deserialized events
    Handler->>Handler: Process events and generate working time events
    Handler->>Server: Return response
    Server->>Client: Response with status
Loading

Poem

In the code where changes bloom,
We’ve added logic to consume,
Events that track the working time,
With new methods, oh so prime.
Refactored flows, imports aligned,
Our code now runs, so refined.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 5359a4e and f36cf4e.
Files selected for processing (1)
  • cvat/apps/events/serializers.py (5 hunks)
Additional Context Used
Ruff (1)
cvat/apps/events/serializers.py (1)

122-122: Do not use bare except

Additional comments not posted (4)
cvat/apps/events/serializers.py (4)

8-8: Import of suppress from contextlib is appropriate for handling exceptions where specific errors are expected and can be safely ignored.


38-38: The constant _WORKING_TIME_SCOPE is well-defined and follows naming conventions for constants in Python.


40-70: The method _generate_wt_event is well-structured and handles the creation of working time events. It uses the suppress context manager to handle cases where a Job might not exist, which is a good use of Python's capabilities to handle exceptions cleanly.


Line range hint 91-148: The modifications in the to_internal_value method to handle working time per job and generate events accordingly are significant. It's important to ensure that these changes are thoroughly tested, especially since they involve time calculations and JSON operations which are error-prone.

Would you like assistance in writing comprehensive unit tests for these changes?

Verification successful

The test functions in cvat/apps/events/tests/test_events.py cover various scenarios related to event handling, including instant events, compressed events, and events with different gap durations. These tests indirectly verify the functionality of the to_internal_value method and the new working time handling logic.

  • test_instant
  • test_compressed
  • test_instants_with_short_gap
  • test_instants_with_long_gap
  • test_compressed_with_short_gap
  • test_compressed_with_long_gap
  • test_compressed_contained
  • test_compressed_overlapping
  • test_instant_inside_compressed
  • test_previous_instant_short_gap
  • test_previous_instant_long_gap
  • test_previous_compressed_short_gap
  • test_previous_compressed_long_gap

These tests ensure that the working time calculations and event processing logic are functioning correctly.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the new working time handling logic integrates well with existing event processing.

# Test: Search for unit tests related to `to_internal_value` method. Expect: Tests covering the new logic.
rg --type python $'def test_to_internal_value'

Length of output: 78



Script:

#!/bin/bash
# Description: Verify that the new working time handling logic integrates well with existing event processing.

# Test: Search for unit tests related to `to_internal_value` method. Expect: Tests covering the new logic.
rg --type py 'def test_to_internal_value'

Length of output: 43



Script:

#!/bin/bash
# Description: Verify that the new working time handling logic integrates well with existing event processing.

# Test: Search for references to `ClientEventsSerializer` in test files to identify any relevant tests.
rg --type py 'ClientEventsSerializer' --glob '*test*.py'

Length of output: 808



Script:

#!/bin/bash
# Description: Verify that the new working time handling logic integrates well with existing event processing.

# Test: Search for test functions within the `test_events.py` file and inspect their content for coverage of the `to_internal_value` method.
rg --type py 'def ' cvat/apps/events/tests/test_events.py

Length of output: 886



Script:

#!/bin/bash
# Description: Extract the content of test functions within the `test_events.py` file to verify coverage of the `to_internal_value` method.

# Extract the content of the test functions.
rg --type py -A 10 'def test_' cvat/apps/events/tests/test_events.py

Length of output: 4890

@bsekachev bsekachev changed the title [WIP] Using dedicated event to store working time Using dedicated event to store working time May 29, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f36cf4e and 22e0e1d.
Files selected for processing (1)
  • cvat/apps/events/serializers.py (4 hunks)
Additional Context Used
Ruff (1)
cvat/apps/events/serializers.py (1)

122-122: Do not use bare except

Additional comments not posted (3)
cvat/apps/events/serializers.py (3)

8-8: Ensure the suppress import is used appropriately.

This import is correctly used in the context of suppressing exceptions when a job does not exist in the database.


38-38: Define the scope constant clearly.

The _WORKING_TIME_SCOPE constant is well-defined and clearly indicates its purpose, which is to tag events related to working time.


40-70: Review the method _generate_wt_event for correctness and efficiency.

This method correctly calculates the working time and creates an event. The use of suppress to handle the potential Job.DoesNotExist exception is appropriate. However, consider adding more detailed logging for debugging purposes.

cvat/apps/events/serializers.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 22e0e1d and ae9ac55.
Files selected for processing (1)
  • cvat/apps/events/serializers.py (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • cvat/apps/events/serializers.py

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between ae9ac55 and 2b8bc1c.
Files selected for processing (2)
  • cvat/apps/events/serializers.py (4 hunks)
  • cvat/apps/events/views.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • cvat/apps/events/serializers.py
Additional Context Used
Ruff (1)
cvat/apps/events/views.py (1)

8-8: rest_framework.exceptions.PermissionDenied imported but unused

@@ -5,6 +5,7 @@
from django.conf import settings
from rest_framework import status, viewsets
from rest_framework.response import Response
from rest_framework.exceptions import PermissionDenied
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused import.

- from rest_framework.exceptions import PermissionDenied

This import statement for PermissionDenied is not used anywhere in the file. It should be removed to keep the code clean and efficient unless there are plans to use it in the near future.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
from rest_framework.exceptions import PermissionDenied

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 2b8bc1c and 4d84047.
Files selected for processing (3)
  • cvat/apps/events/handlers.py (3 hunks)
  • cvat/apps/events/serializers.py (2 hunks)
  • cvat/apps/events/views.py (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • cvat/apps/events/views.py
Additional Context Used
Ruff (1)
cvat/apps/events/handlers.py (1)

42-42: .serializers.ClientEventsSerializer imported but unused

Additional comments not posted (4)
cvat/apps/events/serializers.py (3)

45-48: Ensure correct handling of time correction for previous events.


52-54: Proper exception handling for JSON payload parsing.


57-64: Ensure that the updated event data includes all necessary fields and that the payload is correctly serialized.

cvat/apps/events/handlers.py (1)

602-692: Review the logic in handle_client_events_push to ensure it correctly calculates and records working time events. This function is central to the PR's objectives and should be thoroughly tested.

Verification successful

The handle_client_events_push function is called within the create method of the EventsViewSet class. The serializer.validated_data is passed to this function, which includes the client events data.

The logic in handle_client_events_push appears to be correctly structured to calculate and record working time events based on the provided data. The function processes the events, calculates the working time, and generates the appropriate events to be recorded.

  • cvat/apps/events/views.py
    • EventsViewSet.create method: Calls handle_client_events_push with request and serializer.validated_data.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct calculation of working time events.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type python $'handle_client_events_push'

Length of output: 75



Script:

#!/bin/bash
# Description: Verify the correct calculation of working time events.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg 'handle_client_events_push'

Length of output: 275



Script:

#!/bin/bash
# Description: Extract relevant lines from cvat/apps/events/views.py to verify the usage of handle_client_events_push.

# Extract lines where handle_client_events_push is imported and used.
rg -A 5 -B 5 'handle_client_events_push' cvat/apps/events/views.py

Length of output: 930



Script:

#!/bin/bash
# Description: Extract the definition of the method in EventsViewSet where handle_client_events_push is called.

# Extract the method definition from cvat/apps/events/views.py.
rg -A 20 -B 5 'handle_client_events_push' cvat/apps/events/views.py

Length of output: 2754

@@ -36,6 +39,7 @@
from cvat.apps.organizations.models import Membership, Organization, Invitation
from cvat.apps.organizations.serializers import OrganizationReadSerializer, MembershipReadSerializer, InvitationReadSerializer

from .serializers import ClientEventsSerializer, EventSerializer
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused import for ClientEventsSerializer.

- from .serializers import ClientEventsSerializer, EventSerializer
+ from .serializers import EventSerializer

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
from .serializers import ClientEventsSerializer, EventSerializer
from .serializers import EventSerializer

@bsekachev bsekachev requested a review from mdacoca as a code owner May 29, 2024 10:45
@bsekachev bsekachev requested review from SpecLad and removed request for Marishka17 May 29, 2024 10:51
@codecov-commenter
Copy link

codecov-commenter commented May 29, 2024

Codecov Report

Attention: Patch coverage is 88.15789% with 9 lines in your changes are missing coverage. Please review.

Project coverage is 83.42%. Comparing base (88aabbf) to head (d6080ee).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #7958   +/-   ##
========================================
  Coverage    83.42%   83.42%           
========================================
  Files          383      383           
  Lines        40238    40267   +29     
  Branches      3772     3772           
========================================
+ Hits         33567    33592   +25     
- Misses        6671     6675    +4     
Components Coverage Δ
cvat-ui 79.16% <100.00%> (ø)
cvat-server 87.31% <87.83%> (-0.01%) ⬇️

cvat/apps/events/handlers.py Outdated Show resolved Hide resolved
cvat/apps/events/handlers.py Outdated Show resolved Hide resolved
cvat/apps/events/handlers.py Outdated Show resolved Hide resolved
cvat/apps/events/handlers.py Show resolved Hide resolved
site/content/en/docs/administration/advanced/analytics.md Outdated Show resolved Hide resolved
site/content/en/docs/administration/advanced/analytics.md Outdated Show resolved Hide resolved
site/content/en/docs/administration/advanced/analytics.md Outdated Show resolved Hide resolved
site/content/en/docs/administration/advanced/analytics.md Outdated Show resolved Hide resolved
site/content/en/docs/administration/advanced/analytics.md Outdated Show resolved Hide resolved
@bsekachev bsekachev requested a review from SpecLad June 3, 2024 13:05
@SpecLad SpecLad merged commit 7f4be9c into develop Jun 3, 2024
32 checks passed
@SpecLad SpecLad deleted the bs/dedicated_event_for_wt branch June 3, 2024 13:24
@cvat-bot cvat-bot bot mentioned this pull request Jun 5, 2024
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.

Lack of documenation on working_time metrics of Analytics
3 participants