For the complete documentation index, see llms.txt. This page is also available as Markdown.

Build a computed field

Write the plugin behind a computed field: the methods, the state, and the tests.

Your goal

Implement the Computed Field Function that produces a computed field's value - correctly enough that it survives an initial loading where event order is not guaranteed.

Expected outcome

A COMPUTED_FIELD_FUNCTION plugin whose state stays small, whose result is stable, and which behaves identically however the events arrive.

Before you start

  • Read Computed fields for what a computed field is, its lifecycle, and how it is declared in the schema.

  • Read Plugin architecture for the plugin, version, and instance model.

Computed fields are created as plugins, which requires collaboration with the mediarithmics Professional Services team: defining the logic, writing the plugin specification, developing the plugin, and configuring the computed field. Discuss your needs with your Account Manager.

Create the plugin with the type COMPUTED_FIELD_FUNCTION, following the standard creation and deployment procedure described in Build a custom feed plugin, then generate a version from your build.

The methods to implement

You are required to implement these functions from the Plugin SDK: the onUpdate methods that maintain the state, and buildResult, which turns a state into a result.

onUpdateActivity(state: State, userActivity: UserActivity): State

Updates the state with a new user activity, and removes outdated activities.

Inputs

state - the current state object. userActivity - the new activity data.

Output

The updated state object.

onUpdateActivity(state, userActivity) {
    // Logic to update state
    return updatedState;
}

The activities and profiles the plugin receives are raw data. You cannot rely on the structure defined in your schema to understand their shape - inspect a real activity in the user lookup page instead.

onUpdateUserProfile(state: State, userProfile: UserProfile, operation: core.Operation): State

Handles updates to a user profile.

Inputs

state, userProfile, and operation - an enum of UPDATE or DELETE.

Output

The updated state object.

onUpdateUserProfile(state, userProfile, operation) {
    // Logic to handle UserProfile updates
    return updatedState;
}
onUpdateComputedFields(state: State | null): Result

Triggered by the result computation of another computed field.

onUpdateComputedFields(state) {
    return state;
}

Computes the result from the current state. The result is stored for a defined duration, and querying the field returns the stored value during that period.

The hard constraints

Worked example

The use case: calculate the sales over 12 months for items in the IT category, per user.

1

Declare the state

The state stores the order amounts per day, for IT items, over the last 12 months:

Keying by day is what makes the lookback window enforceable: dropping days older than 12 months is a cheap operation on this shape.

2

Declare the result

The result sums the values stored in the state.

3

Declare the activity shape you read

To find your real activity structure, look at an activity returned in the user lookup page in Navigator. See Explore a user profile.

4

Implement the class

The same approach scales to several categories rather than one - which leads directly to the design question below.

Design guidance

One plugin or several?

Situation
Decision

Two scores need the same data in the state

One plugin.

Two scores need different data - the state would be polymorphic

Two plugins.

Maximise the number of scores calculated by one Computed Field Function. The same state then serves several scores, and you avoid storing the same state several times.

Other rules that come from experience:

  • On error, always return the same state, or null. Returning anything else deletes the state, and therefore resets the result.

  • Design the state to store relevant information efficiently. It is capped at 1 MB.

  • Clean the state against the lookback window, for both profiles and activities: when updating, remove data that is no longer useful to compute the result.

  • When editing a live computed field, you must relaunch an initial loading through the API.

Test before deploying

Create tests covering at least:

  • Adding new activities and updating profiles, and verifying the state is updated correctly - including that the order in which you send profile updates and activities has no impact.

  • That activities no longer relevant to the result are removed from the state.

  • That the correct values are returned in the result.

Verify

  • Tests pass with events sent in several different orders.

  • The state stays well below 1 MB on your heaviest test user.

  • After the initial loading, SELECT { your_field } FROM UserPoint returns plausible values.

  • An error path leaves the previous result intact rather than resetting it.

Last updated

Was this helpful?