-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
76 lines (60 loc) · 1.91 KB
/
state.py
File metadata and controls
76 lines (60 loc) · 1.91 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import annotations
from dataclasses import dataclass
import threading
import uuid
from typing import Any
from models import PlotRequest, validate_plot_request
class PlotNotFoundError(KeyError):
"""Raised when a plot ID does not exist in the in-memory registry."""
@dataclass(frozen=True)
class PlotRecord:
plot_id: str
request: PlotRequest
version: int
_store: dict[str, PlotRecord] = {}
_lock = threading.Lock()
def create_plot_record(
*,
data: list[dict[str, Any]],
plot_type: str,
x: str,
y: str,
) -> PlotRecord:
request = validate_plot_request(data=data, plot_type=plot_type, x=x, y=y)
with _lock:
plot_id = uuid.uuid4().hex[:8]
record = PlotRecord(plot_id=plot_id, request=request, version=1)
_store[plot_id] = record
return record
def get_plot_record(plot_id: str) -> PlotRecord:
with _lock:
try:
return _store[plot_id]
except KeyError as exc:
raise PlotNotFoundError(f"No plot found for id: {plot_id}") from exc
def update_plot_record(
plot_id: str,
*,
data: list[dict[str, Any]] | None = None,
plot_type: str | None = None,
x: str | None = None,
y: str | None = None,
) -> PlotRecord:
with _lock:
try:
existing = _store[plot_id]
except KeyError as exc:
raise PlotNotFoundError(f"No plot found for id: {plot_id}") from exc
request = validate_plot_request(
data=existing.request.data if data is None else data,
plot_type=existing.request.plot_type if plot_type is None else plot_type,
x=existing.request.x if x is None else x,
y=existing.request.y if y is None else y,
)
updated = PlotRecord(
plot_id=plot_id,
request=request,
version=existing.version + 1,
)
_store[plot_id] = updated
return updated