Tracardi / tracardi

TRACARDI is a new HOME for your customer data. TRACARDI is an Composable API-first solution for any company that need inexpensive CDP to intergrate with.
https://www.tracardi.com
Other
527 stars 96 forks source link

Plugin that slices array #742

Open atompie opened 2 years ago

atompie commented 2 years ago

Is your feature request related to a problem? Please describe. Plugin that slice a referenced array in plugin config. This plugin is very simple in terms of python. The challenge is to add it as plugin. This is also very simple if you read this http://docs.tracardi.com/plugins/tutorial/part1/ and http://docs.tracardi.com/plugins/tutorial/part2

Describe the solution you'd like User defines in plugin configuration what array needs to be sliced and defines a start and and end of the slice.

Example of configuration

{
   "data": "event@properties.list_of_something",
   "start": 2,
   "end": 10
}

Input: Workflow payload

Output: an array slice

Example:

Data pointed with event@properties.list_of_something is: [1,2,3,"a",b","c"] start is 2, end is 4 the result is the items form array between 2nd and 4th position: [2,3,"a"]

Additional context You will need to setup the dev environment. Here is the tutorial how to do it. Also to understand how the plugins work please read http://docs.tracardi.com/plugins/tutorial/part1/ and http://docs.tracardi.com/plugins/tutorial/part2/ . The next part is o its way so you can extend your knowledge about Tracardi plugins.

riccardobucco commented 2 years ago

Can I work on this? @atompie

riccardobucco commented 2 years ago

@atompie Where should it be saved? I mean, in which subfolder of tracardi/process_engine/action/v1? There is no "arrays" subfolder. Maybe inside "operations"?

atompie commented 2 years ago

@riccardobucco Arrays fine. You can create one.

atompie commented 2 years ago

@riccardobucco Hi are you working on this issue?

atompie commented 2 months ago

This plugin is still not done. Here is a help how to write the plugin from our AI. Your task will be to verify it and add it to tracardi.

Here’s how you can create and integrate a Tracardi plugin to slice an array based on a specified configuration. I'll walk you through the solution and the steps required to integrate it into Tracardi as a plugin.

Solution Overview

The plugin will take an array, a starting index, and an ending index as input parameters. It will then return the slice of the array according to these indices. The plugin needs to be properly registered in the Tracardi system following the Tracardi plugin guidelines.

Plugin Code

Below is the Python code for your slicing plugin:

from tracardi.service.plugin.domain.result import Result
from tracardi.service.plugin.domain.config import PluginConfig
from tracardi.service.plugin.runner import ActionRunner
from tracardi.service.plugin.domain.register import Plugin, Spec, Meta, Documentation, Form, FormField, FormComponent

class ArraySlicerConfig(PluginConfig):
    data: str
    start: int
    end: int

def validate(config: dict) -> ArraySlicerConfig:
    return ArraySlicerConfig(**config)

class ArraySlicerPlugin(ActionRunner):

    config: ArraySlicerConfig

    async def set_up(self, init):
        self.config = validate(init)

    async def run(self, payload):
        # Access the array using the data reference
        try:
            if not isinstance(self.config.data, list):
                raise ValueError(f"Expected list, but got {type(array).__name__}")

            # Perform slicing
            start = self.config.start
            end = self.config.end
            sliced_array = self.config.data[start:end]

            return Result(port="payload", value={"result":sliced_array})
        except Exception as e:
            return Result(port="error", value={"error": str(e)})

# Plugin registration
def register() -> Plugin:
    return Plugin(
        start=False,
        spec=Spec(
            module='tracardi.process_engine.action.v1.array_slicer.plugin',
            className='ArraySlicerPlugin',
            inputs=['payload'],
            outputs=['payload', 'error'],
            init={
                "data": "event@properties.list_of_something",
                "start": 0,
                "end": 1
            },
            form=Form(
                components=[
                    FormField(
                        id="data",
                        name="Data Path",
                        description="Path to the array in the event or profile.",
                        component=FormComponent(type="dotPath", props={"label": "Data path"})
                    ),
                    FormField(
                        id="start",
                        name="Start Index",
                        description="Starting index of the slice.",
                        component=FormComponent(type="number", props={"label": "Start Index"})
                    ),
                    FormField(
                        id="end",
                        name="End Index",
                        description="Ending index of the slice.",
                        component=FormComponent(type="number", props={"label": "End Index"})
                    ),
                ]
            )
        ),
        metadata=Meta(
            name="Array Slicer",
            desc="Slices an array based on start and end indices.",
            icon="array",
            group=["Data processing"],
            documentation=Documentation(
                inputs={"payload": "Input payload containing the array."},
                outputs={
                    "payload": "Sliced array.",
                    "error": "Error if the operation failed."
                }
            )
        )
    )

Steps to Add the Plugin to Tracardi

  1. Set Up the Development Environment:

    • Follow the development environment setup in the Tracardi documentation to ensure your environment is correctly configured for plugin development.
  2. Create the Plugin File:

    • Place the code above into a Python file, e.g., array_slicer_plugin.py.
  3. Add the Plugin to Tracardi:

    • Place the array_slicer_plugin.py in the Tracardi plugin directory, typically under tracardi/process_engine/action/v1/.
    • Register the plugin by updating the __init__.py file in the plugin directory if needed.
  4. Testing the Plugin:

    • Restart Tracardi services to load the new plugin.
    • Add the plugin to a workflow in the Tracardi Workflow Editor. You should now see the "Array Slicer" plugin available.
    • Configure the plugin within the workflow using the data, start, and end parameters as needed.
  5. Using the Plugin:

    • In a workflow, provide the reference to the array (e.g., event@properties.list_of_something) and the desired start and end indices in the configuration form of the plugin.
    • The output will be the sliced array, which you can use in subsequent workflow steps.

Testing the Plugin

This plugin will integrate seamlessly into Tracardi's workflow system and allow you to slice arrays dynamically based on configuration inputs. Make sure to follow Tracardi’s plugin development guidelines for naming conventions and placement within the plugin directories.