From 6d9c6da756c745836e6a61e61b2bc9e9270e39e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Francisco=20Calvo?= Date: Mon, 16 Sep 2024 14:26:18 +0200 Subject: [PATCH 1/3] chore: move webhooks folder outside api folder --- .../src/argilla_server/api/handlers/v1/webhooks.py | 2 +- .../src/argilla_server/api/schemas/v1/webhooks.py | 2 +- argilla-server/src/argilla_server/bulk/records_bulk.py | 4 ++-- argilla-server/src/argilla_server/contexts/datasets.py | 8 ++++---- .../src/argilla_server/contexts/distribution.py | 4 ++-- argilla-server/src/argilla_server/jobs/webhook_jobs.py | 2 +- .../src/argilla_server/{api => }/webhooks/__init__.py | 0 .../src/argilla_server/{api => }/webhooks/v1/__init__.py | 0 .../src/argilla_server/{api => }/webhooks/v1/commons.py | 0 .../src/argilla_server/{api => }/webhooks/v1/datasets.py | 4 ++-- .../src/argilla_server/{api => }/webhooks/v1/enums.py | 0 .../src/argilla_server/{api => }/webhooks/v1/ping.py | 4 ++-- .../src/argilla_server/{api => }/webhooks/v1/records.py | 4 ++-- .../src/argilla_server/{api => }/webhooks/v1/responses.py | 4 ++-- .../src/argilla_server/{api => }/webhooks/v1/schemas.py | 0 argilla-server/tests/factories.py | 2 +- .../unit/api/handlers/v1/webhooks/test_create_webhook.py | 2 +- .../unit/api/handlers/v1/webhooks/test_delete_webhook.py | 2 +- .../unit/api/handlers/v1/webhooks/test_list_webhooks.py | 2 +- .../unit/api/handlers/v1/webhooks/test_update_webhook.py | 2 +- argilla-server/tests/unit/{api => }/webhooks/__init__.py | 0 .../tests/unit/{api => }/webhooks/v1/__init__.py | 0 .../unit/{api => }/webhooks/v1/test_notify_ping_event.py | 4 ++-- 23 files changed, 26 insertions(+), 26 deletions(-) rename argilla-server/src/argilla_server/{api => }/webhooks/__init__.py (100%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/__init__.py (100%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/commons.py (100%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/datasets.py (94%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/enums.py (100%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/ping.py (89%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/records.py (93%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/responses.py (94%) rename argilla-server/src/argilla_server/{api => }/webhooks/v1/schemas.py (100%) rename argilla-server/tests/unit/{api => }/webhooks/__init__.py (100%) rename argilla-server/tests/unit/{api => }/webhooks/v1/__init__.py (100%) rename argilla-server/tests/unit/{api => }/webhooks/v1/test_notify_ping_event.py (92%) diff --git a/argilla-server/src/argilla_server/api/handlers/v1/webhooks.py b/argilla-server/src/argilla_server/api/handlers/v1/webhooks.py index 49c99d1963..54513dbc04 100644 --- a/argilla-server/src/argilla_server/api/handlers/v1/webhooks.py +++ b/argilla-server/src/argilla_server/api/handlers/v1/webhooks.py @@ -18,7 +18,7 @@ from argilla_server.database import get_async_db from argilla_server.api.policies.v1 import WebhookPolicy, authorize -from argilla_server.api.webhooks.v1.ping import notify_ping_event +from argilla_server.webhooks.v1.ping import notify_ping_event from argilla_server.security import auth from argilla_server.models import User from argilla_server.api.schemas.v1.webhooks import ( diff --git a/argilla-server/src/argilla_server/api/schemas/v1/webhooks.py b/argilla-server/src/argilla_server/api/schemas/v1/webhooks.py index ac75525b22..a093b7f26a 100644 --- a/argilla-server/src/argilla_server/api/schemas/v1/webhooks.py +++ b/argilla-server/src/argilla_server/api/schemas/v1/webhooks.py @@ -16,7 +16,7 @@ from typing import List, Optional from uuid import UUID -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.enums import WebhookEvent from argilla_server.api.schemas.v1.commons import UpdateSchema from argilla_server.pydantic_v1 import BaseModel, Field, HttpUrl diff --git a/argilla-server/src/argilla_server/bulk/records_bulk.py b/argilla-server/src/argilla_server/bulk/records_bulk.py index e3e1155e72..6edbed7265 100644 --- a/argilla-server/src/argilla_server/bulk/records_bulk.py +++ b/argilla-server/src/argilla_server/bulk/records_bulk.py @@ -29,8 +29,8 @@ ) from argilla_server.api.schemas.v1.responses import UserResponseCreate from argilla_server.api.schemas.v1.suggestions import SuggestionCreate -from argilla_server.api.webhooks.v1.enums import RecordEvent -from argilla_server.api.webhooks.v1.records import notify_record_event as notify_record_event_v1 +from argilla_server.webhooks.v1.enums import RecordEvent +from argilla_server.webhooks.v1.records import notify_record_event as notify_record_event_v1 from argilla_server.contexts import distribution from argilla_server.contexts.accounts import fetch_users_by_ids_as_dict from argilla_server.contexts.records import ( diff --git a/argilla-server/src/argilla_server/contexts/datasets.py b/argilla-server/src/argilla_server/contexts/datasets.py index 690acc8c48..b49dd72477 100644 --- a/argilla-server/src/argilla_server/contexts/datasets.py +++ b/argilla-server/src/argilla_server/contexts/datasets.py @@ -60,10 +60,10 @@ VectorSettingsCreate, ) from argilla_server.api.schemas.v1.vectors import Vector as VectorSchema -from argilla_server.api.webhooks.v1.enums import DatasetEvent, ResponseEvent, RecordEvent -from argilla_server.api.webhooks.v1.records import notify_record_event as notify_record_event_v1 -from argilla_server.api.webhooks.v1.responses import notify_response_event as notify_response_event_v1 -from argilla_server.api.webhooks.v1.datasets import notify_dataset_event as notify_dataset_event_v1 +from argilla_server.webhooks.v1.enums import DatasetEvent, ResponseEvent, RecordEvent +from argilla_server.webhooks.v1.records import notify_record_event as notify_record_event_v1 +from argilla_server.webhooks.v1.responses import notify_response_event as notify_response_event_v1 +from argilla_server.webhooks.v1.datasets import notify_dataset_event as notify_dataset_event_v1 from argilla_server.contexts import accounts, distribution from argilla_server.database import get_async_db from argilla_server.enums import DatasetStatus, UserRole, RecordStatus diff --git a/argilla-server/src/argilla_server/contexts/distribution.py b/argilla-server/src/argilla_server/contexts/distribution.py index 410c375178..0073fe8160 100644 --- a/argilla-server/src/argilla_server/contexts/distribution.py +++ b/argilla-server/src/argilla_server/contexts/distribution.py @@ -21,8 +21,8 @@ from sqlalchemy.orm import selectinload from sqlalchemy.ext.asyncio import AsyncSession -from argilla_server.api.webhooks.v1.enums import RecordEvent -from argilla_server.api.webhooks.v1.records import notify_record_event as notify_record_event_v1 +from argilla_server.webhooks.v1.enums import RecordEvent +from argilla_server.webhooks.v1.records import notify_record_event as notify_record_event_v1 from argilla_server.enums import DatasetDistributionStrategy, RecordStatus from argilla_server.models import Record from argilla_server.search_engine.base import SearchEngine diff --git a/argilla-server/src/argilla_server/jobs/webhook_jobs.py b/argilla-server/src/argilla_server/jobs/webhook_jobs.py index a8c054cad4..6bc24a417d 100644 --- a/argilla-server/src/argilla_server/jobs/webhook_jobs.py +++ b/argilla-server/src/argilla_server/jobs/webhook_jobs.py @@ -24,7 +24,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from fastapi.encoders import jsonable_encoder -from argilla_server.api.webhooks.v1.commons import notify_event +from argilla_server.webhooks.v1.commons import notify_event from argilla_server.database import AsyncSessionLocal from argilla_server.jobs.queues import HIGH_QUEUE from argilla_server.contexts import webhooks diff --git a/argilla-server/src/argilla_server/api/webhooks/__init__.py b/argilla-server/src/argilla_server/webhooks/__init__.py similarity index 100% rename from argilla-server/src/argilla_server/api/webhooks/__init__.py rename to argilla-server/src/argilla_server/webhooks/__init__.py diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/__init__.py b/argilla-server/src/argilla_server/webhooks/v1/__init__.py similarity index 100% rename from argilla-server/src/argilla_server/api/webhooks/v1/__init__.py rename to argilla-server/src/argilla_server/webhooks/v1/__init__.py diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/commons.py b/argilla-server/src/argilla_server/webhooks/v1/commons.py similarity index 100% rename from argilla-server/src/argilla_server/api/webhooks/v1/commons.py rename to argilla-server/src/argilla_server/webhooks/v1/commons.py diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/datasets.py b/argilla-server/src/argilla_server/webhooks/v1/datasets.py similarity index 94% rename from argilla-server/src/argilla_server/api/webhooks/v1/datasets.py rename to argilla-server/src/argilla_server/webhooks/v1/datasets.py index 168e9f7a7f..7df7c257b4 100644 --- a/argilla-server/src/argilla_server/api/webhooks/v1/datasets.py +++ b/argilla-server/src/argilla_server/webhooks/v1/datasets.py @@ -22,8 +22,8 @@ from argilla_server.models import Dataset from argilla_server.jobs.webhook_jobs import enqueue_notify_events -from argilla_server.api.webhooks.v1.schemas import DatasetEventSchema -from argilla_server.api.webhooks.v1.enums import DatasetEvent +from argilla_server.webhooks.v1.schemas import DatasetEventSchema +from argilla_server.webhooks.v1.enums import DatasetEvent async def notify_dataset_event(db: AsyncSession, dataset_event: DatasetEvent, dataset: Dataset) -> List[Job]: diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/enums.py b/argilla-server/src/argilla_server/webhooks/v1/enums.py similarity index 100% rename from argilla-server/src/argilla_server/api/webhooks/v1/enums.py rename to argilla-server/src/argilla_server/webhooks/v1/enums.py diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/ping.py b/argilla-server/src/argilla_server/webhooks/v1/ping.py similarity index 89% rename from argilla-server/src/argilla_server/api/webhooks/v1/ping.py rename to argilla-server/src/argilla_server/webhooks/v1/ping.py index f4b2a0bf50..caea2bf287 100644 --- a/argilla-server/src/argilla_server/api/webhooks/v1/ping.py +++ b/argilla-server/src/argilla_server/webhooks/v1/ping.py @@ -18,8 +18,8 @@ from argilla_server.models import Webhook from argilla_server.contexts import info -from argilla_server.api.webhooks.v1.commons import notify_event -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.commons import notify_event +from argilla_server.webhooks.v1.enums import WebhookEvent def notify_ping_event(webhook: Webhook) -> httpx.Response: diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/records.py b/argilla-server/src/argilla_server/webhooks/v1/records.py similarity index 93% rename from argilla-server/src/argilla_server/api/webhooks/v1/records.py rename to argilla-server/src/argilla_server/webhooks/v1/records.py index 34b4db4d18..b60d95d884 100644 --- a/argilla-server/src/argilla_server/api/webhooks/v1/records.py +++ b/argilla-server/src/argilla_server/webhooks/v1/records.py @@ -20,8 +20,8 @@ from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import selectinload -from argilla_server.api.webhooks.v1.enums import RecordEvent -from argilla_server.api.webhooks.v1.schemas import RecordEventSchema +from argilla_server.webhooks.v1.enums import RecordEvent +from argilla_server.webhooks.v1.schemas import RecordEventSchema from argilla_server.jobs.webhook_jobs import enqueue_notify_events from argilla_server.models import Record, Dataset diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/responses.py b/argilla-server/src/argilla_server/webhooks/v1/responses.py similarity index 94% rename from argilla-server/src/argilla_server/api/webhooks/v1/responses.py rename to argilla-server/src/argilla_server/webhooks/v1/responses.py index 97f451da11..a4048094c0 100644 --- a/argilla-server/src/argilla_server/api/webhooks/v1/responses.py +++ b/argilla-server/src/argilla_server/webhooks/v1/responses.py @@ -23,8 +23,8 @@ from argilla_server.models import Response, Record, Dataset from argilla_server.jobs.webhook_jobs import enqueue_notify_events -from argilla_server.api.webhooks.v1.schemas import ResponseEventSchema -from argilla_server.api.webhooks.v1.enums import ResponseEvent +from argilla_server.webhooks.v1.schemas import ResponseEventSchema +from argilla_server.webhooks.v1.enums import ResponseEvent async def notify_response_event(db: AsyncSession, response_event: ResponseEvent, response: Response) -> List[Job]: diff --git a/argilla-server/src/argilla_server/api/webhooks/v1/schemas.py b/argilla-server/src/argilla_server/webhooks/v1/schemas.py similarity index 100% rename from argilla-server/src/argilla_server/api/webhooks/v1/schemas.py rename to argilla-server/src/argilla_server/webhooks/v1/schemas.py diff --git a/argilla-server/tests/factories.py b/argilla-server/tests/factories.py index 1d189d1ce4..2d2666dbce 100644 --- a/argilla-server/tests/factories.py +++ b/argilla-server/tests/factories.py @@ -21,7 +21,7 @@ from sqlalchemy.ext.asyncio import async_object_session from argilla_server.enums import DatasetDistributionStrategy, FieldType, MetadataPropertyType, OptionsOrder -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.enums import WebhookEvent from argilla_server.models import ( Dataset, Field, diff --git a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_create_webhook.py b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_create_webhook.py index 4d168e8847..5b076b2d85 100644 --- a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_create_webhook.py +++ b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_create_webhook.py @@ -19,7 +19,7 @@ from sqlalchemy import func, select from sqlalchemy.ext.asyncio import AsyncSession -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.enums import WebhookEvent from argilla_server.models import Webhook from argilla_server.constants import API_KEY_HEADER_NAME diff --git a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_delete_webhook.py b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_delete_webhook.py index 995583c155..f48b12dcb6 100644 --- a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_delete_webhook.py +++ b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_delete_webhook.py @@ -19,7 +19,7 @@ from sqlalchemy import func, select from sqlalchemy.ext.asyncio import AsyncSession -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.enums import WebhookEvent from argilla_server.models import Webhook from argilla_server.constants import API_KEY_HEADER_NAME diff --git a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_list_webhooks.py b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_list_webhooks.py index e7c0dc1216..5738e23f5a 100644 --- a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_list_webhooks.py +++ b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_list_webhooks.py @@ -16,7 +16,7 @@ from httpx import AsyncClient -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.enums import WebhookEvent from argilla_server.constants import API_KEY_HEADER_NAME from tests.factories import AdminFactory, AnnotatorFactory, WebhookFactory diff --git a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_update_webhook.py b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_update_webhook.py index e4df0142c4..c615cce9c9 100644 --- a/argilla-server/tests/unit/api/handlers/v1/webhooks/test_update_webhook.py +++ b/argilla-server/tests/unit/api/handlers/v1/webhooks/test_update_webhook.py @@ -18,7 +18,7 @@ from httpx import AsyncClient from typing import Any -from argilla_server.api.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.enums import WebhookEvent from argilla_server.constants import API_KEY_HEADER_NAME from tests.factories import AdminFactory, AnnotatorFactory, WebhookFactory diff --git a/argilla-server/tests/unit/api/webhooks/__init__.py b/argilla-server/tests/unit/webhooks/__init__.py similarity index 100% rename from argilla-server/tests/unit/api/webhooks/__init__.py rename to argilla-server/tests/unit/webhooks/__init__.py diff --git a/argilla-server/tests/unit/api/webhooks/v1/__init__.py b/argilla-server/tests/unit/webhooks/v1/__init__.py similarity index 100% rename from argilla-server/tests/unit/api/webhooks/v1/__init__.py rename to argilla-server/tests/unit/webhooks/v1/__init__.py diff --git a/argilla-server/tests/unit/api/webhooks/v1/test_notify_ping_event.py b/argilla-server/tests/unit/webhooks/v1/test_notify_ping_event.py similarity index 92% rename from argilla-server/tests/unit/api/webhooks/v1/test_notify_ping_event.py rename to argilla-server/tests/unit/webhooks/v1/test_notify_ping_event.py index 12c58cc208..1fc7f4ae26 100644 --- a/argilla-server/tests/unit/api/webhooks/v1/test_notify_ping_event.py +++ b/argilla-server/tests/unit/webhooks/v1/test_notify_ping_event.py @@ -20,8 +20,8 @@ from httpx import Response from standardwebhooks.webhooks import Webhook -from argilla_server.api.webhooks.v1.enums import WebhookEvent -from argilla_server.api.webhooks.v1.ping import notify_ping_event +from argilla_server.webhooks.v1.enums import WebhookEvent +from argilla_server.webhooks.v1.ping import notify_ping_event from argilla_server.contexts import info from tests.factories import WebhookFactory From 92372d03f9068265843fbe3ed1a0a9ee961f4934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Francisco=20Calvo?= Date: Mon, 16 Sep 2024 14:43:18 +0200 Subject: [PATCH 2/3] feat: remove response.upserted event and use response.created or response.updated instead --- argilla-server/src/argilla_server/contexts/datasets.py | 6 +++++- argilla-server/src/argilla_server/webhooks/v1/enums.py | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/argilla-server/src/argilla_server/contexts/datasets.py b/argilla-server/src/argilla_server/contexts/datasets.py index b49dd72477..3208ad9c7b 100644 --- a/argilla-server/src/argilla_server/contexts/datasets.py +++ b/argilla-server/src/argilla_server/contexts/datasets.py @@ -939,7 +939,11 @@ async def upsert_response( await db.commit() await distribution.update_record_status(search_engine, record.id) - await notify_response_event_v1(db, ResponseEvent.upserted, response) + + if response.inserted_at == response.updated_at: + await notify_response_event_v1(db, ResponseEvent.created, response) + else: + await notify_response_event_v1(db, ResponseEvent.updated, response) return response diff --git a/argilla-server/src/argilla_server/webhooks/v1/enums.py b/argilla-server/src/argilla_server/webhooks/v1/enums.py index b5c396be91..39c9cb64a0 100644 --- a/argilla-server/src/argilla_server/webhooks/v1/enums.py +++ b/argilla-server/src/argilla_server/webhooks/v1/enums.py @@ -24,7 +24,6 @@ class WebhookEvent(str, Enum): response_created = "response.created" response_updated = "response.updated" response_deleted = "response.deleted" - response_upserted = "response.upserted" record_created = "record.created" record_updated = "record.updated" @@ -49,7 +48,6 @@ class ResponseEvent(str, Enum): created = WebhookEvent.response_created.value updated = WebhookEvent.response_updated.value deleted = WebhookEvent.response_deleted.value - upserted = WebhookEvent.response_upserted.value def __str__(self): return str(self.value) From 0028345ec791e9f60c9bfa1a6567df2390a5f3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Francisco=20Calvo?= Date: Mon, 16 Sep 2024 15:01:23 +0200 Subject: [PATCH 3/3] chore: reorder webhooks events --- .../src/argilla_server/webhooks/v1/enums.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/argilla-server/src/argilla_server/webhooks/v1/enums.py b/argilla-server/src/argilla_server/webhooks/v1/enums.py index 39c9cb64a0..fce902e476 100644 --- a/argilla-server/src/argilla_server/webhooks/v1/enums.py +++ b/argilla-server/src/argilla_server/webhooks/v1/enums.py @@ -21,15 +21,15 @@ class WebhookEvent(str, Enum): dataset_deleted = "dataset.deleted" dataset_published = "dataset.published" - response_created = "response.created" - response_updated = "response.updated" - response_deleted = "response.deleted" - record_created = "record.created" record_updated = "record.updated" record_deleted = "record.deleted" record_completed = "record.completed" + response_created = "response.created" + response_updated = "response.updated" + response_deleted = "response.deleted" + def __str__(self): return str(self.value) @@ -44,15 +44,6 @@ def __str__(self): return str(self.value) -class ResponseEvent(str, Enum): - created = WebhookEvent.response_created.value - updated = WebhookEvent.response_updated.value - deleted = WebhookEvent.response_deleted.value - - def __str__(self): - return str(self.value) - - class RecordEvent(str, Enum): created = WebhookEvent.record_created.value updated = WebhookEvent.record_updated.value @@ -61,3 +52,12 @@ class RecordEvent(str, Enum): def __str__(self): return str(self.value) + + +class ResponseEvent(str, Enum): + created = WebhookEvent.response_created.value + updated = WebhookEvent.response_updated.value + deleted = WebhookEvent.response_deleted.value + + def __str__(self): + return str(self.value)