File size: 1,189 Bytes
04bc9df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Validation helpers for incoming request data."""

from typing import List

from fastapi import HTTPException


def validate_history(history: List[float], min_length: int = 2) -> None:
    """Raise HTTP 422 if the time-series history is unusable.

    Checks performed:
    * Minimum length constraint
    * No NaN or infinite values
    * At least some non-zero variance (all-identical series cannot be
      meaningfully extrapolated by a trend model, but we allow it and
      simply emit a warning via the response)
    """
    if len(history) < min_length:
        raise HTTPException(
            status_code=422,
            detail=(
                f"history must contain at least {min_length} data points, "
                f"got {len(history)}"
            ),
        )

    for i, v in enumerate(history):
        if v != v:  # NaN check (works without math.isnan)
            raise HTTPException(
                status_code=422,
                detail=f"history[{i}] is NaN",
            )
        if v == float("inf") or v == float("-inf"):
            raise HTTPException(
                status_code=422,
                detail=f"history[{i}] is infinite",
            )