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

enhancing analytics #8616

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Added

- New events (create|update|delete):(membership|webhook) and (create|delete):invitation
(<https://github.com/cvat-ai/cvat/pull/8616>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Changed

- Payload for events (create|update|delete):(shapes|tags|tracks) does not include frame and attributes anymore
(<https://github.com/cvat-ai/cvat/pull/8616>)
3 changes: 3 additions & 0 deletions cvat/apps/events/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class EventScopes:
"task": ["create", "update", "delete"],
"job": ["create", "update", "delete"],
"organization": ["create", "update", "delete"],
"membership": ["create", "update", "delete"],
"invitation": ["create", "delete"],
"user": ["create", "update", "delete"],
"cloudstorage": ["create", "update", "delete"],
"issue": ["create", "update", "delete"],
Expand All @@ -28,6 +30,7 @@ class EventScopes:
"label": ["create", "update", "delete"],
"dataset": ["export", "import"],
"function": ["call"],
"webhook": ["create", "update", "delete"],
}

@classmethod
Expand Down
19 changes: 12 additions & 7 deletions cvat/apps/events/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

from .cache import get_cache
from .event import event_scope, record_server_event
from ..webhooks.models import Webhook
from ..webhooks.serializers import WebhookReadSerializer


def project_id(instance):
Expand Down Expand Up @@ -245,6 +247,8 @@ def get_serializer(instance):
serializer = MembershipReadSerializer(instance=instance, context=context)
if isinstance(instance, Invitation):
serializer = InvitationReadSerializer(instance=instance, context=context)
if isinstance(instance, Webhook):
serializer = WebhookReadSerializer(instance=instance, context=context)

return serializer

Expand All @@ -254,6 +258,7 @@ def get_serializer_without_url(instance):
serializer.fields.pop("url", None)
return serializer


def handle_create(scope, instance, **kwargs):
oid = organization_id(instance)
oslug = organization_slug(instance)
Expand Down Expand Up @@ -288,6 +293,7 @@ def handle_create(scope, instance, **kwargs):
payload=payload,
)


def handle_update(scope, instance, old_instance, **kwargs):
oid = organization_id(instance)
oslug = organization_slug(instance)
Expand Down Expand Up @@ -322,12 +328,14 @@ def handle_update(scope, instance, old_instance, **kwargs):
payload={"old_value": change["old_value"]},
)


def handle_delete(scope, instance, store_in_deletion_cache=False, **kwargs):
deletion_cache = get_cache()
instance_id = getattr(instance, "id", None)
if store_in_deletion_cache:
deletion_cache.set(
instance.__class__,
instance.id,
instance_id,
{
"oid": organization_id(instance),
"oslug": organization_slug(instance),
Expand All @@ -338,7 +346,7 @@ def handle_delete(scope, instance, store_in_deletion_cache=False, **kwargs):
)
return

instance_meta_info = deletion_cache.pop(instance.__class__, instance.id)
instance_meta_info = deletion_cache.pop(instance.__class__, instance_id)
if instance_meta_info:
oid = instance_meta_info["oid"]
oslug = instance_meta_info["oslug"]
Expand All @@ -360,7 +368,7 @@ def handle_delete(scope, instance, store_in_deletion_cache=False, **kwargs):
scope=scope,
request_id=request_id(),
on_commit=True,
obj_id=getattr(instance, 'id', None),
obj_id=instance_id,
obj_name=_get_object_name(instance),
org_id=oid,
org_slug=oslug,
Expand All @@ -372,15 +380,12 @@ def handle_delete(scope, instance, store_in_deletion_cache=False, **kwargs):
user_email=uemail,
)


def handle_annotations_change(instance, annotations, action, **kwargs):
def filter_data(data):
filtered_data = {
"id": data["id"],
"frame": data["frame"],
"attributes": data["attributes"],
}
if label_id := data.get("label_id"):
filtered_data["label_id"] = label_id

return filtered_data

Expand Down
35 changes: 24 additions & 11 deletions cvat/apps/events/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@
#
# SPDX-License-Identifier: MIT

from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save, post_delete
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_delete, post_save, pre_save
from django.dispatch import receiver

from cvat.apps.engine.models import (
TimestampedModel,
Project,
Task,
Job,
User,
CloudStorage,
Issue,
Comment,
Issue,
Job,
Label,
Project,
Task,
TimestampedModel,
User,
)
from cvat.apps.organizations.models import Organization
from cvat.apps.organizations.models import Invitation, Membership, Organization
from cvat.apps.webhooks.models import Webhook

from .handlers import handle_update, handle_create, handle_delete
from .event import EventScopeChoice, event_scope
from .handlers import handle_create, handle_delete, handle_update


@receiver(pre_save, sender=Webhook, dispatch_uid="webhook:update_receiver")
@receiver(pre_save, sender=Membership, dispatch_uid="membership:update_receiver")
@receiver(pre_save, sender=Organization, dispatch_uid="organization:update_receiver")
@receiver(pre_save, sender=Project, dispatch_uid="project:update_receiver")
@receiver(pre_save, sender=Task, dispatch_uid="task:update_receiver")
Expand All @@ -34,7 +38,8 @@
def resource_update(sender, *, instance, update_fields, **kwargs):
if (
isinstance(instance, TimestampedModel)
and update_fields and list(update_fields) == ["updated_date"]
and update_fields
and list(update_fields) == ["updated_date"]
):
# This is an optimization for the common case where only the date is bumped
# (see `TimestampedModel.touch`). Since the actual update of the field will
Expand All @@ -57,6 +62,10 @@ def resource_update(sender, *, instance, update_fields, **kwargs):

handle_update(scope=scope, instance=instance, old_instance=old_instance, **kwargs)


@receiver(post_save, sender=Webhook, dispatch_uid="webhook:create_receiver")
@receiver(post_save, sender=Membership, dispatch_uid="membership:create_receiver")
@receiver(post_save, sender=Invitation, dispatch_uid="invitation:create_receiver")
@receiver(post_save, sender=Organization, dispatch_uid="organization:create_receiver")
@receiver(post_save, sender=Project, dispatch_uid="project:create_receiver")
@receiver(post_save, sender=Task, dispatch_uid="task:create_receiver")
Expand All @@ -78,6 +87,10 @@ def resource_create(sender, instance, created, **kwargs):

handle_create(scope=scope, instance=instance, **kwargs)


@receiver(post_delete, sender=Webhook, dispatch_uid="webhook:delete_receiver")
@receiver(post_delete, sender=Membership, dispatch_uid="membership:delete_receiver")
@receiver(post_delete, sender=Invitation, dispatch_uid="invitation:delete_receiver")
@receiver(post_delete, sender=Organization, dispatch_uid="organization:delete_receiver")
@receiver(post_delete, sender=Project, dispatch_uid="project:delete_receiver")
@receiver(post_delete, sender=Task, dispatch_uid="task:delete_receiver")
Expand Down
1 change: 1 addition & 0 deletions dev/format_python_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ for paths in \
"cvat/apps/engine/model_utils.py" \
"cvat/apps/dataset_manager/tests/test_annotation.py" \
"cvat/apps/dataset_manager/tests/utils.py" \
"cvat/apps/events/signals.py" \
; do
${BLACK} -- ${paths}
${ISORT} -- ${paths}
Expand Down
6 changes: 6 additions & 0 deletions site/content/en/docs/administration/advanced/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ Server events:

- `call:function`

- `create:membership`, `update:membership`, `delete:membership`

- `create:webhook`, `update:webhook`, `delete:webhook`

- `create:invitation`, `delete:invitation`

Client events:

- `load:cvat`
Expand Down
Loading