Skip to content

api_v1

django_spire.metric.domain.statistic.api_v1

router = Router() module-attribute

StatisticValueIn

Bases: BaseModel

reference = Field(..., min_length=1) class-attribute instance-attribute

sub_domain_key instance-attribute

value = Decimal(1) class-attribute instance-attribute

StatisticValueOut

Bases: BaseModel

statistic_key instance-attribute

reference instance-attribute

sub_domain_key instance-attribute

timestamp instance-attribute

value instance-attribute

record_value

Source code in django_spire/metric/domain/statistic/api_v1.py
@router.post('{statistic_key}/record')
def record_value(
    request: HttpRequest, statistic_key: str, payload: StatisticValueIn
) -> StatisticValueOut:
    statistic = _get_statistic(statistic_key, active_required=True)
    sub_domain = _get_sub_domain(statistic, payload.sub_domain_key, active_required=True)
    statistic_value = statistic.services.processor.add_value(
        reference=payload.reference, sub_domain=sub_domain, value=payload.value
    )
    return StatisticValueOut(
        statistic_key=statistic.key,
        reference=statistic_value.reference,
        sub_domain_key=statistic_value.sub_domain.key,
        timestamp=statistic_value.timestamp,
        value=statistic_value.value,
    )

total_for_interval

Source code in django_spire/metric/domain/statistic/api_v1.py
@router.get('{statistic_key}/total')
def total_for_interval(
    request: HttpRequest,
    statistic_key: str,
    value_date: date_type | None = None,
    sub_domain_key: uuid.UUID | None = None,
) -> dict:
    statistic = _get_statistic(statistic_key)
    sub_domain = _get_sub_domain(statistic, sub_domain_key)
    start_date, end_date = statistic.services.transformation.interval_bounds(value_date)
    total = statistic.services.transformation.total_for_interval(value_date, sub_domain=sub_domain)
    return {
        'date': value_date.isoformat() if value_date else None,
        'start_date': start_date.isoformat(),
        'end_date': end_date.isoformat(),
        'total': str(total),
    }

interval_summary

Source code in django_spire/metric/domain/statistic/api_v1.py
@router.get('{statistic_key}/summary')
def interval_summary(
    request: HttpRequest,
    statistic_key: str,
    start_date: date_type,
    end_date: date_type,
    sub_domain_key: uuid.UUID | None = None,
) -> dict:
    statistic = _get_statistic(statistic_key)
    sub_domain = _get_sub_domain(statistic, sub_domain_key)
    summary = statistic.services.transformation.interval_summary(
        start_date, end_date, sub_domain=sub_domain
    )
    return {
        'start_date': start_date.isoformat(),
        'end_date': end_date.isoformat(),
        'totals': {k.isoformat(): str(v) for k, v in summary.items()},
    }

values_for_interval

Source code in django_spire/metric/domain/statistic/api_v1.py
@router.get('{statistic_key}/values')
def values_for_interval(
    request: HttpRequest,
    statistic_key: str,
    value_date: date_type | None = None,
    sub_domain_key: uuid.UUID | None = None,
) -> list[StatisticValueOut]:
    statistic = _get_statistic(statistic_key)
    sub_domain = _get_sub_domain(statistic, sub_domain_key)
    values = statistic.services.transformation.values_for_interval(
        value_date, sub_domain=sub_domain
    )
    return [
        StatisticValueOut(
            statistic_key=statistic.key,
            reference=sv.reference,
            sub_domain_key=sv.sub_domain.key,
            timestamp=sv.timestamp,
            value=sv.value,
        )
        for sv in values
    ]