# Setup

{% hint style="info" %}
Computed fields are created as of plugin, requiring collaboration with mediarithmics Professional Services (PS) team.

The implementation involves defining the logic, writing plugin specifications, developing the plugin, and configuring the computed fields.

Please discuss your needs with your account manager.
{% endhint %}

1. [Create your computed field function (plugin)](#step-1-create-your-computed-field-function-plugin)
2. [Create your computed field within the platform](#step-2-create-your-computed-field-within-the-platform)
3. [Declare your computed field in your schema](#step-3-declare-your-computed-field-in-your-schema)
4. [Wait until the end of the initial loading](#step-4-wait-until-the-end-of-the-initial-loading)
5. [Use your computed field](#step-5-use-your-computed-field)

## Step 1: Create your computed field function (plugin)

Check the [following documentation to learn more about Plugin development](/advanced-usages/plugins/creation-deployment.md). Bellow are some precisions about specific Computed fields Function implementation.

### Develop the plugin with the SDK and create a build from it

{% hint style="info" %}
You are required to implement 4 key functions from the Plugin SDK.
{% endhint %}

#### OnUpdate functions

These 3 functions are triggered during the initialization of the Computed Field Function and whenever a new activity, profile update, or computed field modification occurs, updating the `State` accordingly.

[You can find the list of all the methods to be developed here](/schema/computed-fields/development.md#list-of-methods).

{% hint style="warning" %}
Although `onUpdateComputedField` has not been released yet, it is still required to be implemented in your plugin. In the meantime, simply return the `State` directly to avoid any errors.
{% endhint %}

#### BuildResult

This function returns the result of the computed field function for a specific `State`. The `Result` will be stored for a defined duration, and querying the field will return the stored value during that period.

[You can find all the details about this method here](/schema/computed-fields/development.md#buildresult).

### Create a plugin and a version

Follow the standard procedure to [create a plugin](/advanced-usages/plugins.md) with the type `COMPUTED_FIELD_FUNCTION`. Once the plugin is created, you can [generate a new version](/advanced-usages/plugins.md#versions) from the build.

## Step 2: Create your computed field within the platform

1. In the **Navigator > Settings > Datamart**, navigate to **Computed Fields**.
2. Add a **new computed field**.
3. Select the **plugin** and **version** of the new instance you want to use.
4. Complete the necessary fields:

<table><thead><tr><th width="305">Fields</th><th>Description</th></tr></thead><tbody><tr><td>Name</td><td>Name of the computed field</td></tr><tr><td>Description</td><td>(Optional) Describe the computed field</td></tr><tr><td>Technical name</td><td>Name of the computed field used in the schema</td></tr><tr><td>Compute period (in days)</td><td>Maximal duration before the periodic update of the <code>Result</code>  in days.</td></tr><tr><td>Events filter</td><td>GraphQL selector query to filter the activity which will trigger the <code>State</code> updates</td></tr></tbody></table>

This will set up the computed field linked to your computedfield function for use in the schema.

## Step 3: Declare your computed field in your schema

After creating the computed field, you need to connect it to your schema:

1. Use the directive `@ComputedField(technical_name = "...")` at the appropriate level of your schema.
2. Ensure the **technical name** matches the one used when creating the computed field.

This will integrate the computed field into your schema and make it available for use in queries and operations.

```graphql
// Example in your schema 
type UserPoint {
  id: ID!
  accounts: [UserAccount]
  …
  rfm_score: RfmScore @ComputedField(technical_name = “RfmScore”) @TreeIndex(index:"USER_INDEX")
}

type RfmScore {
  …
}
```

A **schema validation** will be triggered when you click **Save**. It will ensure that your Datamart has access to all the declared computed fields and verify their integration with the schema.

{% hint style="warning" %}
The computed field must be in the UserPoint object.
{% endhint %}

## Step 4: Wait until the end of the initial loading&#x20;

Once your schema is validated, our service will run a job to initialize your computed field and set the state to match the current status of the timeline. For each UserPoint in your Datamart, we will process their timeline and execute the **OnUpdateXXX()** function.

To monitor the progress:

1. Go to the **Computing Console** > **Computed Fields**.
2. Check if your computed field is ready or if the initial loading is still in progress.

Please note, this process may take some time to complete (up to 72h) as it computes the timelines for all UserPoint.

{% hint style="danger" %}
Avoid using your computed field until the initial loading is complete. While the query will not return an error, the result may be inaccurate during this process.
{% endhint %}

## Step 5: Use your computed field

Once the initial loading is complete, you can start using your computed field just like any other standard field in mediarithmics features.

For example, in **query tools** or any **OTQL** query:

```sql
SELECT { rfm_score } FROM UserPoint
```

If your computed field is indexed, you can also perform queries like:

```sql
SELECT { id } FROM UserPoint WHERE rfm_score = "PASSIVE"
```

This allows you to incorporate the computed field into your data analysis and queries once it's fully initialized.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.mediarithmics.io/schema/computed-fields/setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
