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.
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): StateUpdates 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;
}onUpdateUserProfile(state: State, userProfile: UserProfile, operation: core.Operation): StateHandles 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): ResultTriggered 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.
The same approach scales to several categories rather than one - which leads directly to the design question below.
Design guidance
One plugin or several?
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 UserPointreturns plausible values.An error path leaves the previous result intact rather than resetting it.
Related pages
Last updated
Was this helpful?