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

/api/events endpoint can be used to receive events not only from client #8799

Merged
merged 10 commits into from
Dec 18, 2024
3 changes: 3 additions & 0 deletions cvat/apps/events/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from cvat.apps.iam.permissions import OpenPolicyAgentPermission, StrEnum
from cvat.utils.http import make_requests_session


class EventsPermission(OpenPolicyAgentPermission):
class Scopes(StrEnum):
SEND_EVENTS = 'send:events'
DUMP_EVENTS = 'dump:events'
SEND_EXTERNAL_EVENTS = 'send-external:events'
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update the rego rules to reflect this change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I planned it to be only accessible by admins, and rego rules do not need to be changed for it.
But due to comments above I will try to use the existing endpoint somehow, and just drop this scope

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed it


@classmethod
def create(cls, request, view, obj, iam_context):
Expand Down Expand Up @@ -49,6 +51,7 @@ def get_scopes(request, view, obj):
Scopes = __class__.Scopes
return [{
('create', 'POST'): Scopes.SEND_EVENTS,
('external', 'POST'): Scopes.SEND_EXTERNAL_EVENTS,
('list', 'GET'): Scopes.DUMP_EVENTS,
}[(view.action, request.method)]]

Expand Down
26 changes: 25 additions & 1 deletion cvat/apps/events/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Copyright (C) 2023-2024 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import json

from django.conf import settings
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import (OpenApiParameter, OpenApiResponse,
extend_schema)
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response

Expand All @@ -15,8 +17,10 @@
from cvat.apps.events.serializers import ClientEventsSerializer
from cvat.apps.iam.filters import ORGANIZATION_OPEN_API_PARAMETERS

from .event import record_server_event
from .export import export
from .handlers import handle_client_events_push
from .handlers import handle_client_events_push, request_id
from .serializers import EventSerializer


class EventsViewSet(viewsets.ViewSet):
Expand Down Expand Up @@ -44,6 +48,26 @@ def create(self, request):

return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

@extend_schema(summary='Log external events',
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need several end-points to send events (add a row into Clickhouse)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reused existing endpoint

description='Sends logs to the Clickhouse if it is connected',
responses={'201': EventSerializer()})
@action(
detail=False,
url_path='external',
methods=['POST'],
serializer_class=EventSerializer,
)
def external(self, request):
serializer = EventSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = dict(
serializer.validated_data,
payload=json.loads(serializer.validated_data['payload']),
)
record_server_event(request_id=request_id(), **data)

return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

@extend_schema(summary='Get an event log',
methods=['GET'],
description='The log is returned in the CSV format.',
Expand Down
26 changes: 26 additions & 0 deletions cvat/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,32 @@ paths:
schema:
$ref: '#/components/schemas/ClientEvents'
description: ''
/api/events/external:
post:
operationId: events_create_external
description: Sends logs to the Clickhouse if it is connected
summary: Log external events
tags:
- events
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/EventRequest'
required: true
security:
- sessionAuth: []
csrfAuth: []
tokenAuth: []
- signatureAuth: []
- basicAuth: []
responses:
'201':
content:
application/vnd.cvat+json:
schema:
$ref: '#/components/schemas/Event'
description: ''
/api/guides:
post:
operationId: guides_create
Expand Down
Loading