For AI agents: the complete documentation index is available at https://docs.ovhcloud.com/de/llms.txt, the full documentation bundle is available at https://docs.ovhcloud.com/de/llms-full.txt, and this page is available as Markdown at https://docs.ovhcloud.com/de/guides/public-cloud/data-platform/tutorials-custom-event-handling.md.

Creating Event Handling Actions

Als Markdown ansehen

In this article we will go through the steps required to create an action that allows you to handle events

Objective

In this article we will go through the steps required to create an action that allows you to handle events. This can be applied to trigger actions or workflows defined in your Data Processing Engine with a body of data outside of the platform.

Event Handling Table

We usually recommend that when actions are called by an external event, the event call is archived for reference in your project. In this article we will use an example table in our data model called event_handle

  • Table Name: event_handle
  • Table Description: the table contains 4 attributes to store the content of the event.
Attribute NameTypeExample value
event_idNumber38123461
event_nameStringRebecca
event_contentStringWalked in the store
dateDate2020-05-21 11:34:23 PM
Warning

Note that the names provided in this example both for the table, its attributes as well as all the other object names below (for the action or functions) are examples. You can obviously choose to name your objects differently, just make sure that you update the subsequent code section which references the different objects.

Generally speaking this is a good best practice to set-up if you're planning on creating custom actions which handle events because you will use it to archive all triggered events. To create such a table, head to the Lakehouse Manager component, create a table with attributes following the ones outlined in the table above.

Creating a data model in the Lakehouse Manager

Event Handling Action Set-up

To create your custom action, head to the Data Processing Engine component and create a new custom action and enter the action name as Event handle. The sample Python code provided below shows how to manage the body of the event when the action is triggered outside of the platform, typically via API endpoint.

In the custom action configuration, change the function name (which specifies which Python function is the main function to run) to event_handle. Finally, copy & paste the following code sample in the custom code editor:

import logging
import pandas as pd
from datetime import datetime
from forepaas.dwh import bulk_insert
from forepaas.dwh import connect

logger = logging.getLogger(__name__)

def event_handle(event):
    logger.info(f"Start inserting event to default_dataset/event_handle")
    logger.info(f"event_type = {event.type}, number of rows = {len(event.content)}")
    if type(event.content) is not list:
        raise Exception("When playing this action you need to send rows")

    # Create dataframe
    df = pd.DataFrame(event.content)
    df["date"] = datetime.now().strftime("%Y-%m-%d")

    # Connect to destination database and insert dataframe to table
    destination = connect("dwh/default_dataset/")
    stats, error = bulk_insert(destination, "event_handle", df)

    logger.info(f"End insert of event: stats={stats}, error={error}")
Info

Note that if you're more confortable in an IDE-like interface, click on the Advanced mode on top of the page to switch to the online code editor. You will be able to update the raw source code of the action in the Python script and all its configuration in the JSON file.

Launching Actions and Workflows

Now that you know how to handle the incoming event information when an action is launched externally, let's look at how to set-up the trigger events. This will enable you to start a job of an action or workflow programmatically.

Required Parameters

There 3 parameters required to launch a job which you obviously need to set with respect to your own environment configuration. All the parameters are outlined in the code samples using {} brackets like {subdomain}.

Parameter nameValueExample
subdomainProject subdomaintest
action_idUnique reference of your action5eaf0db7f6f51d5255974217
tokenAuthentication tokenxxxxx

Project Subdomain

To retrieve your project subdomain, simply look-up your project URL. For instance if you copy paste the URL when you have the DPE component open it should look like this:

https://{project_subdomain}.eu.dataplatform.ovh.net/dpe/#/

The subdomain is the first reference in the path of your project's URL.

Action ID or Workflow ID

To retrieve the action ID of the action you want to start, you can also find it in your action's URL when editing it. To do that, click edit on the action you want to trigger, the URL should look like this:

# Action's URL
https://{project_subdomain}.eu.dataplatform.ovh.net/dpe/#/action/{action_id}

# Workflow's URL
https://{project_subdomain}.eu.dataplatform.ovh.net/dpe/#/workflow/{workflow_id}

Authentication Token

To generate an authentication token, you will first need to create a specific API & secret key from the Identity Access Manager component. To learn how to do that, check-out this dedicated article:

Creating an API / Secret Key

Code samples

cURL
Python 3+
curl --request POST \
  --url 'https://{subdomain}.eu.dataplatform.ovh.net/dpe/v3/actions/{action_id}/start?app_id=55c1423560702d6426490f38&type=cam&token={token}' \
  --header 'content-type: application/json' \
  --data '[
	{
		"event_name": "Rebecca",
		"event_id": 1,
		"event_content": "imperdiet ornare."
	},
	{
		"event_name": "Veronica",
		"event_id": 2,
		"event_content": "Nullam ut nisi a odio"
	},
	{
		"event_name": "Aurelia",
		"event_id": 3,
		"event_content": "amet"
	},
	{
		"event_name": "Yardley",
		"event_id": 4,
		"event_content": "ornare placerat, orci lacus vestibulum"
	},
	{
		"event_name": "Dakota",
		"event_id": 5,
		"event_content": "nulla. Cras eu tellus eu"
	}
]'
Info

Note that workflows and actions can be equally triggered using the same process. To use the data sent as part of the call, make sure the workflow contains a handling event action. Also, make sure to use the right URL for workflows (see section on Action or Workflow ID).

We hope this article was useful! Let us know if you have any questions or would like to suggest an improvement.

Go further

If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.

Ask questions, give your feedback and interact directly with the team building the Data Platform on the dedicated Discord channel.

If you need support with your OVHcloud services, create a request in our Help Centre.

Join our community of users.

War diese Seite hilfreich?