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

[FEATURE-BRANCH] feat: add Webhooks #5467

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from

Conversation

jfcalvo
Copy link
Member

@jfcalvo jfcalvo commented Sep 9, 2024

Description

This is the feature branch for Webhooks implementation.

Closes #1836

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested

  • Manually tested locally and in HF spaces.

Checklist

  • I added relevant documentation
  • I followed the style guidelines of this project
  • I did a self-review of my code
  • I made corresponding changes to the documentation
  • I confirm My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/)

# Description

This PR adds the base implementation for Webhooks with the following
changes:
* Adding a new API to create/update/delete Webhooks.
* Add a new endpoint to execute a testing a ping event for a specific
Webhook.
* No background jobs integration. This is gonna be done in a different
PR.

**Type of change**

- New feature (non-breaking change which adds functionality)

**How Has This Been Tested**

- [x] Manually tested locally and in HF spaces.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)

---------

Co-authored-by: Paco Aranda <[email protected]>
Copy link

codecov bot commented Sep 9, 2024

Codecov Report

Attention: Patch coverage is 99.07621% with 4 lines in your changes missing coverage. Please review.

Project coverage is 91.71%. Comparing base (e43c9bf) to head (f6ab425).

Files with missing lines Patch % Lines
...lla-server/src/argilla_server/jobs/webhook_jobs.py 86.66% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5467      +/-   ##
===========================================
+ Coverage    91.22%   91.71%   +0.49%     
===========================================
  Files          145      159      +14     
  Lines         6030     6448     +418     
===========================================
+ Hits          5501     5914     +413     
- Misses         529      534       +5     
Flag Coverage Δ
argilla-server 91.71% <99.07%> (+0.49%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

jfcalvo and others added 7 commits September 11, 2024 13:45
# Description

This PR adds the following changes:
* Add first real Webhooks notifications for responses using some
background jobs for them.
* Add a new RQ `high` queue where we will enqueue the Webhooks
notification jobs.
* Add webhook events:
  * `response.created`
  * `response.updated`
  * `response.deleted`
  * `response.upserted`
  * `dataset.created`
  * `dataset.updated`
  * `dataset.deleted`
  * `dataset.published`

**Type of change**

- New feature (non-breaking change which adds functionality)

**How Has This Been Tested**

- [x] Manually tested locally and in HF spaces.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)

---------

Co-authored-by: Damián Pumar <[email protected]>
Co-authored-by: Paco Aranda <[email protected]>
# Description

This is a PR exploring the possibility of expanding associated resources
on webhook event messages.

The following changes has been added:
* Add webhook event schemas for the following resources:
  * `Response`
  * `Record`
  * `Dataset`
  * `Workspace`
  * `User`
* Webhook events associated to deletion of resources are sending now a
simple `{"id": "id-of-the-deleted-resource"}` to avoid problems with
missing resources deleted on cascade.
* The model associations are loaded using `awaitable_attrs` to avoid
replacing the original resource instance (and avoid a database refresh
that could cause losing the state of the resource).

This is an example of a `response.created` event with the changes:

```json
{
  "type": "response.created",
  "version": 1,
  "timestamp": "2024-09-12T11:52:15.083612Z",
  "data": {
    "id": "053a845a-02fd-4951-9884-2317a4a2be19",
    "values": {
      "int_score": {
        "value": 3
      }
    },
    "status": "submitted",
    "record": {
      "id": "60eadd2c-76ed-4216-870e-9d361ac0a894",
      "status": "pending",
      "fields": {
        "text": "a combination of one or more elementary reaction steps which start with the appropriate reactants and end with the appropriate product(s)\na description of the path, or sequence of steps, by which a reaction occurs\na description of the path that a reaction takes\na detailed description of how a chemical reaction occurs\na detailed description of the way a reaction occurs and is based on the known experimental data about the reaction\na detailed (theoretical) description of how we think the chemical reaction proceeds\na series of elementary reactions or elementary steps that lead from reactants to products\na set of steps at the molecular level\na step by step description of the separate steps that occur during a chemical reaction\na stepwise description of the reaction path\nmechanism. A list of all elementary reactions that occur in the course of an overall chemical reaction.\nIn chemistry, a reaction mechanism is the step by step sequence of elementary reactions by which overall chemical change occurs."
      },
      "metadata": {
        "dump": "CC-MAIN-2013-20",
        "url": "http://www.metaglossary.com/meanings/3337605/",
        "language": "en",
        "language_score": 0.9414262175559998,
        "token_count": 195,
        "score": 3.671875
      },
      "external_id": "<urn:uuid:7ae3a60b-341f-4a49-8f9d-b5b4588f2f8d>",
      "dataset": {
        "id": "408239b6-d100-4eff-b5d9-afcc8e99b9f1",
        "name": "fineweb-edu-min-submitted-big",
        "guidelines": null,
        "allow_extra_metadata": false,
        "status": "ready",
        "distribution": {
          "strategy": "overlap",
          "min_submitted": 1
        },
        "workspace": {
          "id": "350bc020-2cd2-4a67-8b23-37a15c4d8139",
          "name": "argilla",
          "inserted_at": "2024-09-05T11:39:20.377192",
          "updated_at": "2024-09-05T11:39:20.377192"
        },
        "questions": [
          {
            "id": "f6453e3a-5ed2-4853-a8c4-913d03bc1dfb",
            "name": "int_score",
            "title": "Rate the quality of the text",
            "description": null,
            "required": true,
            "settings": {
              "type": "rating",
              "options": [
                {
                  "value": 0
                },
                {
                  "value": 1
                },
                {
                  "value": 2
                },
                {
                  "value": 3
                },
                {
                  "value": 4
                },
                {
                  "value": 5
                }
              ]
            },
            "inserted_at": "2024-09-06T10:21:25.482006",
            "updated_at": "2024-09-06T10:21:25.482006"
          },
          {
            "id": "4d0b53e6-9b96-42da-a863-2c17d483be01",
            "name": "comments",
            "title": "Comments:",
            "description": null,
            "required": false,
            "settings": {
              "type": "text",
              "use_markdown": false
            },
            "inserted_at": "2024-09-06T10:21:25.493565",
            "updated_at": "2024-09-06T10:21:25.493565"
          }
        ],
        "fields": [
          {
            "id": "02ef122a-d989-4422-89ad-e29096a1acfd",
            "name": "text",
            "title": "text",
            "required": true,
            "settings": {
              "type": "text",
              "use_markdown": false
            },
            "inserted_at": "2024-09-06T10:21:25.468310",
            "updated_at": "2024-09-06T10:21:25.468310"
          }
        ],
        "metadata_properties": [
          {
            "id": "c79d1808-7551-4fb8-b673-b5bb0bec0532",
            "name": "dump",
            "title": "dump",
            "settings": {
              "type": "terms",
              "values": null
            },
            "visible_for_annotators": true,
            "inserted_at": "2024-09-06T10:21:25.506303",
            "updated_at": "2024-09-06T10:21:25.506303"
          },
          {
            "id": "1195fc9b-0974-498f-8cff-6feddfd6e9a1",
            "name": "url",
            "title": "url",
            "settings": {
              "type": "terms",
              "values": null
            },
            "visible_for_annotators": true,
            "inserted_at": "2024-09-06T10:21:25.516085",
            "updated_at": "2024-09-06T10:21:25.516085"
          },
          {
            "id": "28cc925c-fed6-48bf-a1df-ef43e884b33b",
            "name": "language",
            "title": "language",
            "settings": {
              "type": "terms",
              "values": null
            },
            "visible_for_annotators": true,
            "inserted_at": "2024-09-06T10:21:25.523871",
            "updated_at": "2024-09-06T10:21:25.523871"
          },
          {
            "id": "9333a2c9-efaf-4b0e-88e7-b1ed24dfcbfa",
            "name": "language_score",
            "title": "language_score",
            "settings": {
              "min": null,
              "max": null,
              "type": "float"
            },
            "visible_for_annotators": true,
            "inserted_at": "2024-09-06T10:21:25.532447",
            "updated_at": "2024-09-06T10:21:25.532447"
          },
          {
            "id": "9d56b399-3c53-4e98-9d4a-ebfb360ec293",
            "name": "token_count",
            "title": "token_count",
            "settings": {
              "min": null,
              "max": null,
              "type": "integer"
            },
            "visible_for_annotators": true,
            "inserted_at": "2024-09-06T10:21:25.539531",
            "updated_at": "2024-09-06T10:21:25.539531"
          },
          {
            "id": "6c701f7b-de47-4224-b9b0-3629133282ed",
            "name": "score",
            "title": "score",
            "settings": {
              "min": null,
              "max": null,
              "type": "float"
            },
            "visible_for_annotators": true,
            "inserted_at": "2024-09-06T10:21:25.546505",
            "updated_at": "2024-09-06T10:21:25.546505"
          }
        ],
        "vectors_settings": [],
        "last_activity_at": "2024-09-12T11:52:15.030317",
        "inserted_at": "2024-09-06T10:21:25.443578",
        "updated_at": "2024-09-12T11:52:04.923388"
      },
      "inserted_at": "2024-09-06T10:21:27.790000",
      "updated_at": "2024-09-06T10:21:27.790000"
    },
    "user": {
      "id": "df114042-958d-42c6-9f03-ab49bd451c6c",
      "first_name": "",
      "last_name": null,
      "username": "argilla",
      "role": "owner",
      "inserted_at": "2024-09-05T11:39:20.376463",
      "updated_at": "2024-09-05T11:39:20.376463"
    },
    "inserted_at": "2024-09-12T11:52:15.029023",
    "updated_at": "2024-09-12T11:52:15.029023"
  }
}
```

There are still missing changes and solutions to explore:
- [ ] Review some associations that we can still add to the events.
- [ ] Review if some missing endpoints are necessary.
- [ ] Should we truncate record field values that are too large?
- [ ] For example truncate image field values. Something like
`afdsmk432mkfseiwer...(truncated)`
- [ ] Update CHANGELOG if necessary.

**Type of change**

- New feature (non-breaking change which adds functionality)

**How Has This Been Tested**

- [ ] Manually test the new events on HF spaces.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
# Description
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->

This PR adds record-related webhook events: `created`, `updated`,
`deleted` and `completed`

**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->

- Improvement (change adding some improvement to an existing
functionality)

**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)

---------

Co-authored-by: José Francisco Calvo <[email protected]>
# Description
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->

This PR exposes the new webhooks API through the low-level `client.api`
component. Once the backend API and SDK flows are validated, we can work
on exposing webhooks as client resource level

**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Refactor (change restructuring the codebase without changing
functionality)
- Improvement (change adding some improvement to an existing
functionality)
- Documentation update

**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
# Description

As discussed this PR is moving `webhooks` folder outside `api` folder.
Now that we have specific schemas for webhooks it does not have much
sense to have this code inside the `api` folder.

Refs #1836 

**Type of change**

- Refactor (change restructuring the codebase without changing
functionality)

**How Has This Been Tested**

- [x] Running test suite.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
# Description

As discussed we want to stop using `response.upserted` webhook event and
instead use `response.created` or `response.updated` when a response is
upserted.

Refs #1836 

**Type of change**

- New feature (non-breaking change which adds functionality)

**How Has This Been Tested**

- [x] Running test suite.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
@jfcalvo jfcalvo added this to the v2.3.0 milestone Sep 17, 2024
frascuchon and others added 13 commits September 18, 2024 11:00
…5511)

# Description
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->

Since URLs including IP addresses are allowed values when
creating/updating webhooks, this PR only adds tests checking these
scenarios.

**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->


**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)

---------

Co-authored-by: José Francisco Calvo <[email protected]>
# Description

Added some changes so we can generate Webhook events before deleting
resources and notify those events only after the resource has been
successfully deleted.

Affected events:
* Delete a dataset.
* Delete a response.
* Delete a record.
* Delete many records.

Refs #1836 

**Type of change**

- Improvement (change adding some improvement to an existing
functionality)

**How Has This Been Tested**

- [x] Manually test that deleted resource events are correctly working.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
…alue instead of event timestamp (#5521)

# Description

Using the same timestamp value in the payload and in the
`webhook-timestamp` (this one used for verification) was causing an
"message too old" error when verifying the webhook message.

Replacing it to be calculated in the moment of the webhook request
should solve these associated problems.

**Type of change**

- Fix


**How Has This Been Tested**

- [x] Test suite passing.
- [x] Tested manually with a listener verifying the webhook message. 

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
# Description

This PR adds some missing tests checking that webhook events are
enqueued on background jobs when actions occurs inside Argilla app.

Refs #1836 

**Type of change**

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Refactor (change restructuring the codebase without changing
functionality)
- Improvement (change adding some improvement to an existing
functionality)
- Documentation update

**How Has This Been Tested**

 - [x] All tests should be passing.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
# Description

This PR increases the timeout for webhook requests from `5` seconds to
`20` seconds so the webhook listeners have more time to generate a
response.

Refs #1836 

**Type of change**

- Improvement (change adding some improvement to an existing
functionality)

**How Has This Been Tested**

- [x] Test suite should be passing.

**Checklist**

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
@frascuchon frascuchon modified the milestones: v2.3.0, v2.4.0 Sep 30, 2024
frascuchon and others added 3 commits October 2, 2024 16:44
# Description
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->

This PR adds the `argilla.webhooks` module to work with webhooks from
the SDK. With the changes on this PR, users can easily create webhook
listeners using the python SDK. For example, listening changes can be
defined as follows:

```python
import argilla as rg


@rg.webhook_listener(events="response.updated")
async def on_response_updated(response: rg.UserResponse, **kwargs):
   ... # do your work 

@rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.published"])
async def on_dataset_event(
    type: str,
    timestamp: datetime,
    dataset: rg.Dataset,
    **kwargs,
):
    print(f"Event type {type} at {timestamp}")
    print(dataset.settings)

```

You can find a fully basic example using webhooks
[here](https:/argilla-io/argilla/tree/feat/argilla/working-with-webhooks/examples/webhooks/basic-webhooks)

This is still a draft PR and the final feature may change.


Refs: #4658

**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->

- New feature (non-breaking change which adds functionality)

**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- I added relevant documentation
- I followed the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)

---------

Co-authored-by: David Berenstein <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sara Han <[email protected]>
Co-authored-by: José Francisco Calvo <[email protected]>
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.

Webhooks
2 participants