forked from mtusi2026/mtusi2026lab6
ЛР6: OTel SDK, Collector, Prometheus/Loki/Tempo, Grafana, alerts, dashboard, тесты инцидентов
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
"""Непрозрачный keyset-курсор (created_at, id)."""
|
||||
import base64
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from .errors import bad_request
|
||||
|
||||
|
||||
def encode(kind, scope, created_at, row_id):
|
||||
payload = {"k": kind, "s": str(scope), "t": created_at.astimezone(timezone.utc).isoformat(), "i": str(row_id)}
|
||||
return base64.urlsafe_b64encode(json.dumps(payload, separators=(",", ":")).encode()).rstrip(b"=").decode()
|
||||
|
||||
|
||||
def decode(value, kind, scope):
|
||||
if not value:
|
||||
raise bad_request("Некорректный cursor")
|
||||
try:
|
||||
raw = base64.urlsafe_b64decode(value + "=" * (-len(value) % 4))
|
||||
payload = json.loads(raw.decode("utf-8"))
|
||||
created_at = datetime.fromisoformat(payload["t"])
|
||||
row_id = uuid.UUID(payload["i"])
|
||||
if payload["k"] != kind or payload["s"] != str(scope) or created_at.tzinfo is None:
|
||||
raise ValueError("scope mismatch")
|
||||
return created_at, row_id
|
||||
except (ValueError, KeyError, TypeError, UnicodeDecodeError):
|
||||
raise bad_request("Некорректный cursor")
|
||||
Reference in New Issue
Block a user