Open atompie opened 2 years ago
Can I work on this? @atompie
@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"?
@riccardobucco Arrays fine. You can create one.
@riccardobucco Hi are you working on this issue?
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.
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.
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."
}
)
)
)
Set Up the Development Environment:
Create the Plugin File:
array_slicer_plugin.py
.Add the Plugin to Tracardi:
array_slicer_plugin.py
in the Tracardi plugin directory, typically under tracardi/process_engine/action/v1/
.__init__.py
file in the plugin directory if needed.Testing the Plugin:
data
, start
, and end
parameters as needed.Using the Plugin:
event@properties.list_of_something
) and the desired start and end indices in the configuration form of the plugin.data
.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.
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
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.