Indicators#

Introduction#

This section will discuss the importance of Indicators in OpenSPP. Indicators are a powerful tool that creates abstraction and simplifies targeting, allowing for the calculation of the number of children under 18 years old, the number of elderly women, or whether a household is headed by a single woman.

The Indicators play a crucial role in determining the eligibility of registrants for social protection programs, as they're computed based on the information available in the social registry. By using Indicators, errors and inaccuracies can be avoided.

Technical considerations#

Indicators are fields added to the group or individual table. In Odoo those are computed fields. To reduce the performance impact of those, those fields are recomputed. in an asynchronous job, only when the data they depend on changes. This means that the value of the field won't be available immediately after the data it depends on is changed. It will be available after the next time the job is run, usually within a second.

The indicator fields are stored as fields in the database, but they're not editable. They're computed based on the data in the registry.

Note

Indicators computation, if not properly written can use a lot of resources. While Odoo developers often write a for loop in the compute method to fetch some data, this is not a good practice. It is recommended to fetch the data for all the records in one query and then set the value to each record.

Creating an indicator field#

Through the UI#

Go to Registry → Configuration → Custom Fields and click Create.

Indicator creation interface in OpenSPP

TODO

Through the code#

Create a model that inherits from res.partner. In this example, we will count the number of children in a group.

To learn more about search domain, see the Odoo documentation on search domains. Search domain are like a simplified SQL query. They're used to filter the data that is used to compute the indicator.

To simplify the count of members in a group, we created a helper method compute_count_and_set_indicator that takes the name of the indicator field, the domain to filter the members of the group, and the domain to filter the members of the group that are used to compute the indicator.

G2PMembershipGroup.compute_count_and_set_indicator(field_name, kinds, domain, presence_only=False)[source]

This method computes the count matching a domain, then sets the indicator on the field name.

Parameters
  • field_name (str) -- The name of the field.

  • kinds (list) -- The kinds of roles in the group

  • domain (list) -- The domain to filter group members.

  • presence_only (bool) -- A boolean value to define if we return a boolean instead of the count

Returns

The count of the specified field, then sets the indicator on the field name.

Return type

int, bool

import datetime
from odoo import fields, models
from dateutil.relativedelta import relativedelta

CHILDREN_AGE_LIMIT = 18

class G2PGroup(models.Model):
    _inherit = "res.partner"

    z_ind_grp_num_children = fields.Integer(
        "Number of children",
        compute="_compute_ind_grp_num_children",
        help="Number of children",
        store=True,
        allow_filter=True,
    )

    def _compute_ind_grp_num_children(self):
        now = datetime.datetime.now()
        children = now - relativedelta(years=CHILDREN_AGE_LIMIT)
        domain = [("birthdate", ">=", children)]
        self.compute_count_and_set_indicator("z_ind_grp_num_children", None, domain)