fix(unifi): the Overview's history buckets on Postgres; devices name their state (#5207)
#5207 "UniFi Overview tab: func.strftime is SQLite-only": the WAN and client sparklines bucketed with strftime('%s'), which Postgres lacks, so /plugins/unifi/rows failed. Traefik's date_bin bucket moves to core/time_range.time_bucket and both plugins use it. Devices list offline and failing first, and a state other than online is named (Upgrading, Adopting, Heartbeat missed...) instead of drawn as Down. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,8 @@ from __future__ import annotations
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import TypeVar
|
||||
|
||||
from sqlalchemy import func
|
||||
|
||||
RANGES: dict[str, timedelta] = {
|
||||
"1h": timedelta(hours=1),
|
||||
"6h": timedelta(hours=6),
|
||||
@@ -33,6 +35,16 @@ def bucket_seconds(since: datetime, target_points: int = 80) -> int:
|
||||
return max(60, int(range_secs / target_points))
|
||||
|
||||
|
||||
def time_bucket(column, seconds: int):
|
||||
"""The start of the `seconds`-wide window a timestamp falls in, aligned to
|
||||
the epoch, as a Postgres timestamp labelled "bucket". Group and order by
|
||||
it. (An integer epoch divided by an int rendered as numeric division in
|
||||
Postgres, so no two scrapes shared a bucket, and overflowed int4 after
|
||||
2038 (#5256); strftime('%s') is SQLite-only (#5207).)"""
|
||||
return func.date_bin(func.make_interval(0, 0, 0, 0, 0, 0, int(seconds)), column,
|
||||
func.to_timestamp(0)).label("bucket")
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user