Skip to content

Api reference

components.product_compliance_manager.public.api

constraint_infos

get_coverage_rules_constraint_infos

get_coverage_rules_constraint_infos(
    coverage_rules, guarantee_constraints
)
Source code in components/product_compliance_manager/internal/business_logic/coverage_rule_constraint_infos.py
def get_coverage_rules_constraint_infos(
    coverage_rules: list[GuaranteeCatalogCoverageRule],
    guarantee_constraints: list[GuaranteeConstraint],
) -> list[GuaranteeConstraintInfo]:
    constraint_infos = []

    for constraint in guarantee_constraints:
        coverage_rule = one_or_none(
            [
                rule
                for rule in coverage_rules
                if rule.guarantee_business_id in constraint.guarantee_refs
                # FIXME: ultimately we shouldn't filter on expression_type, and use a translation function instead
                and rule.expression_type == constraint.expression_type
            ]
        )

        if not coverage_rule:
            continue

        coverage_rule_constraint_infos = _get_constraint_infos_for_coverage_rule(
            coverage_rule=coverage_rule, constraint=constraint
        )

        constraint_infos.extend(coverage_rule_constraint_infos)

    return deduplicate_guarantee_constraint_infos(constraint_infos)

constraints

search_guarantee_constraints

search_guarantee_constraints(
    product_type, source_type, source_refs, country
)

Search for guarantee constraints by product type, source type, source reference and country

Source code in components/product_compliance_manager/public/api/constraints.py
def search_guarantee_constraints(
    product_type: ConstrainedProductType,
    source_type: ConstraintSourceType,
    source_refs: list[str],
    country: ProductComplianceManagerSupportedCountry,
) -> list[PublicGuaranteeConstraint]:
    """Search for guarantee constraints by product type, source type, source reference and country"""
    repository = GuaranteeConstraintRepository()
    internal_guarantee_constraints = repository.search(
        product_type=product_type,
        source_type=source_type,
        source_refs=source_refs,
        country=country,
    )
    return [
        PublicGuaranteeConstraint.from_internal_entity(internal_guarantee_constraint)
        for internal_guarantee_constraint in internal_guarantee_constraints
    ]

violations

compute_coverage_rules_violations

compute_coverage_rules_violations(
    coverage_rules, guarantee_constraints, guarantee_catalog
)

Compute violations for a given coverage rules and guarantee constraints.

We want to link coverage rules to existing constraints, and create violations for missing coverage rules. In order to avoid false positives for guarantees that only appear in some bundle choices: - We first list all constraint bundles missing in the coverage rules - We then iterate over coverage rules having a constraint

Parameters:

Name Type Description Default
coverage_rules list[CoverageRule]

List of coverage rules.

required
guarantee_constraints list[GuaranteeConstraint]

List of guarantee constraints.

required
guarantee_catalog GuaranteeCatalog

Guarantee catalog.

required

Returns:

Name Type Description
violations list[GuaranteeViolation]

List of violations.

Source code in components/product_compliance_manager/internal/business_logic/coverage_rule_violations.py
def compute_coverage_rules_violations(
    coverage_rules: list[GuaranteeCatalogCoverageRule],
    guarantee_constraints: list[GuaranteeConstraint],
    guarantee_catalog: GuaranteeCatalog,
) -> list[GuaranteeViolation]:
    """
    Compute violations for a given coverage rules and guarantee constraints.

    We want to link coverage rules to existing constraints, and create violations for missing coverage rules.
    In order to avoid false positives for guarantees that only appear in some bundle choices:
    - We first list all constraint bundles missing in the coverage rules
    - We then iterate over coverage rules having a constraint

    Args:
        coverage_rules (list[GuaranteeCatalogCoverageRule]): List of coverage rules.
        guarantee_constraints (list[GuaranteeConstraint]): List of guarantee constraints.
        guarantee_catalog (GuaranteeCatalog): Guarantee catalog.

    Returns:
        violations (list[GuaranteeViolation]): List of violations.
    """
    violations = []

    # Group constraints by guarantee_ref (a constraint can have multiple guarantee_refs)
    constraints_by_guarantee_ref: dict[str, list[GuaranteeConstraint]] = defaultdict(
        list
    )
    for guarantee_constraint in guarantee_constraints:
        for guarantee_ref in guarantee_constraint.guarantee_refs:
            constraints_by_guarantee_ref[guarantee_ref].append(guarantee_constraint)

    coverage_rules_by_guarantee_ref = {
        rule.guarantee_business_id: rule for rule in coverage_rules
    }

    guarantee_refs_intersection = set(constraints_by_guarantee_ref.keys()) & set(
        coverage_rules_by_guarantee_ref.keys()
    )
    violations = _get_violations_for_missing_coverage_rules(
        guarantee_catalog=guarantee_catalog,
        coverage_rules_by_guarantee_ref=coverage_rules_by_guarantee_ref,
        constraints_by_guarantee_ref=constraints_by_guarantee_ref,
    )

    for guarantee_ref in guarantee_refs_intersection:
        coverage_rule = coverage_rules_by_guarantee_ref[guarantee_ref]
        constraints = constraints_by_guarantee_ref.get(guarantee_ref, [])
        # FIXME: we expect constraints to be unique by guarantee_refs x expression_type but for now it's not the case
        # when using profideo's data so we need to filter on expression_type
        # - Long term we should be using a translation function
        for constraint in (
            c for c in constraints if c.expression_type == coverage_rule.expression_type
        ):
            violations.extend(
                _get_violations_for_coverage_rule(
                    coverage_rule=coverage_rule, constraint=constraint
                )
            )

    return [
        GuaranteeViolation.from_guarantee_constraint_info(constraint_info)
        for constraint_info in deduplicate_guarantee_constraint_infos(violations)
    ]

components.product_compliance_manager.public.country_specifics

fr

enums

FrConstrainedProductType

Bases: ConstrainedProductType

health class-attribute instance-attribute
health = 'health'
FrConstraintSourceType

Bases: ConstraintSourceType

collective_agreement class-attribute instance-attribute
collective_agreement = 'collective_agreement'
poc_collective_agreement class-attribute instance-attribute
poc_collective_agreement = 'poc_collective_agreement'

components.product_compliance_manager.public.entities

guarantee_constraint

Entity for GuaranteeConstraint model.

GuaranteeConstraint dataclass

GuaranteeConstraint(
    *,
    source_type,
    source_ref,
    expression_type,
    guarantee_refs,
    eligibility_items_refs,
    parameter_constraints
)

Bases: DataClassJsonMixin

Constraints on guarantees (as defined in the guarantee catalog).

eligibility_items_refs instance-attribute
eligibility_items_refs
expression_type instance-attribute
expression_type
from_internal_entity staticmethod
from_internal_entity(internal_guarantee_constraint)

Convert an InternalGuaranteeConstraint into a GuaranteeConstraint

Source code in components/product_compliance_manager/public/entities/guarantee_constraint.py
@staticmethod
def from_internal_entity(
    internal_guarantee_constraint: InternalGuaranteeConstraint,
) -> GuaranteeConstraint:
    """Convert an InternalGuaranteeConstraint into a GuaranteeConstraint"""
    return GuaranteeConstraint(
        source_type=internal_guarantee_constraint.source_type,
        source_ref=internal_guarantee_constraint.source_ref,
        expression_type=internal_guarantee_constraint.expression_type,
        guarantee_refs=internal_guarantee_constraint.guarantee_refs,
        eligibility_items_refs=internal_guarantee_constraint.eligibility_items_refs,
        parameter_constraints=[
            GuaranteeParameterConstraint(
                parameter_type=param_constraint.parameter_type,
                value=param_constraint.value,
                constraint_type=param_constraint.constraint_type,
            )
            for param_constraint in internal_guarantee_constraint.parameter_constraints
        ],
    )
guarantee_refs instance-attribute
guarantee_refs
parameter_constraints instance-attribute
parameter_constraints
source_ref instance-attribute
source_ref
source_type instance-attribute
source_type

GuaranteeParameterConstraint dataclass

GuaranteeParameterConstraint(
    *, parameter_type, value, constraint_type
)

Bases: DataClassJsonMixin

Defines the constraint applying to a parameter of a guarantee expression.

constraint_type instance-attribute
constraint_type
parameter_type instance-attribute
parameter_type
value instance-attribute
value

guarantee_constraint_info

GuaranteeConstraintInfo dataclass

GuaranteeConstraintInfo(
    *,
    type,
    description,
    guarantee_ref,
    bundle_choice_ref,
    parameter_type,
    additional_args
)

Information about a guarantee constraint, not necessarily a violation.

additional_args instance-attribute
additional_args
bundle_choice_ref instance-attribute
bundle_choice_ref
constraint_key property
constraint_key

Builds a key that uniquely identifies a group of constraints. This can happen when multiple constraint sources target the same guarantee.

description instance-attribute
description
guarantee_ref instance-attribute
guarantee_ref
parameter_type instance-attribute
parameter_type
type instance-attribute
type

GuaranteeConstraintInfoAdditionalArgs dataclass

GuaranteeConstraintInfoAdditionalArgs(
    *,
    missing_eligibility_items=None,
    min_value=None,
    max_value=None,
    exact_value=None,
    sources=set()
)

Bases: DataClassJsonMixinWithNoneExclusion

Additional arguments for a guarantee violation, provides metadata for the frontend display.

exact_value class-attribute instance-attribute
exact_value = None
max_value class-attribute instance-attribute
max_value = None
merge
merge(other)

Merge two GuaranteeConstraintInfoAdditionalArgs instances.

For sets (missing_eligibility_items, sources), combines all values. For min_value, keeps the maximum value. For max_value, keeps the minimum value. For exact_value, uses the value from other if present.

Source code in components/product_compliance_manager/public/entities/guarantee_constraint_info.py
def merge(
    self, other: "GuaranteeConstraintInfoAdditionalArgs"
) -> "GuaranteeConstraintInfoAdditionalArgs":
    """
    Merge two GuaranteeConstraintInfoAdditionalArgs instances.

    For sets (missing_eligibility_items, sources), combines all values.
    For min_value, keeps the maximum value.
    For max_value, keeps the minimum value.
    For exact_value, uses the value from other if present.
    """
    merged_sources = self.sources | other.sources
    merged_missing_eligibility_items = (self.missing_eligibility_items or set()) | (
        other.missing_eligibility_items or set()
    )

    merged_min_value = self.min_value
    if other.min_value is not None:
        if merged_min_value is None or other.min_value > merged_min_value:
            merged_min_value = other.min_value

    merged_max_value = self.max_value
    if other.max_value is not None:
        if merged_max_value is None or other.max_value < merged_max_value:
            merged_max_value = other.max_value

    merged_exact_value = (
        other.exact_value if other.exact_value is not None else self.exact_value
    )

    return GuaranteeConstraintInfoAdditionalArgs(
        sources=merged_sources,
        missing_eligibility_items=merged_missing_eligibility_items
        if merged_missing_eligibility_items
        else None,
        min_value=merged_min_value,
        max_value=merged_max_value,
        exact_value=merged_exact_value,
    )
min_value class-attribute instance-attribute
min_value = None
missing_eligibility_items class-attribute instance-attribute
missing_eligibility_items = None
sources class-attribute instance-attribute
sources = field(default_factory=set)

GuaranteeConstraintInfoType

Bases: AlanBaseEnum

Enum representing types of guarantee violations within the compliance manager.

constraint_exact_match class-attribute instance-attribute
constraint_exact_match = 'constraint_exact_match'
constraint_max_value class-attribute instance-attribute
constraint_max_value = 'constraint_max_value'
constraint_min_value class-attribute instance-attribute
constraint_min_value = 'constraint_min_value'

guarantee_violation

GuaranteeViolation dataclass

GuaranteeViolation(
    *,
    type,
    description,
    guarantee_ref,
    bundle_choice_ref,
    parameter_type,
    additional_args
)

Bases: GuaranteeConstraintInfo

A guarantee constraint violation

from_guarantee_constraint_info staticmethod
from_guarantee_constraint_info(guarantee_constraint_info)

Convert a GuaranteeConstraintInfo into a GuaranteeViolation.

Source code in components/product_compliance_manager/public/entities/guarantee_violation.py
@staticmethod
def from_guarantee_constraint_info(
    guarantee_constraint_info: GuaranteeConstraintInfo,
) -> GuaranteeViolation:
    """
    Convert a GuaranteeConstraintInfo into a GuaranteeViolation.
    """
    return GuaranteeViolation(
        type=guarantee_constraint_info.type,
        description=guarantee_constraint_info.description,
        guarantee_ref=guarantee_constraint_info.guarantee_ref,
        bundle_choice_ref=guarantee_constraint_info.bundle_choice_ref,
        parameter_type=guarantee_constraint_info.parameter_type,
        additional_args=guarantee_constraint_info.additional_args,
    )

GuaranteeViolationType

Bases: AlanBaseEnum

Enum representing types of guarantee violations within the compliance manager.

missing_constraint_mandatory_bundle class-attribute instance-attribute
missing_constraint_mandatory_bundle = (
    "missing_constraint_mandatory_bundle"
)

components.product_compliance_manager.public.enums

product_compliance_manager_supported_country

ProductComplianceManagerSupportedCountry

Bases: AlanBaseEnum

Enum representing possible countries supported by the product compliance manager.

fr class-attribute instance-attribute
fr = 'fr'