flagdock 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
flagdock/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """Flagdock feature flags for Python: an OpenFeature provider that evaluates in memory."""
2
+
3
+ from .engine import Resolution, evaluate
4
+ from .provider import SDK_VERSION, FlagdockProvider
5
+
6
+ __all__ = ["FlagdockProvider", "Resolution", "SDK_VERSION", "evaluate"]
7
+ __version__ = SDK_VERSION
flagdock/_usage.py ADDED
@@ -0,0 +1,81 @@
1
+ """Counts flag checks and goal events in memory until they are sent. Keeps no personal data beyond the person's ID."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import threading
6
+ from typing import Any, Optional
7
+
8
+
9
+ class UsageCounter:
10
+ """Flag key, value key and a number: nothing else."""
11
+
12
+ def __init__(self) -> None:
13
+ self._lock = threading.Lock()
14
+ self._counts: dict[tuple[str, str], dict[str, Any]] = {}
15
+
16
+ def record(self, flag: str, value_key: Optional[str]) -> None:
17
+ key = (flag, value_key or "")
18
+ with self._lock:
19
+ entry = self._counts.get(key)
20
+ if entry is not None:
21
+ entry["count"] += 1
22
+ # A cap, so a bug that checks endless made-up flag names cannot eat memory.
23
+ elif len(self._counts) < 5000:
24
+ self._counts[key] = {"flag": flag, **({"value": value_key} if value_key is not None else {}), "count": 1}
25
+
26
+ def drain(self) -> list[dict[str, Any]]:
27
+ with self._lock:
28
+ out = list(self._counts.values())
29
+ self._counts = {}
30
+ return out
31
+
32
+ def restore(self, counts: list[dict[str, Any]]) -> None:
33
+ with self._lock:
34
+ for c in counts:
35
+ key = (c["flag"], c.get("value") or "")
36
+ entry = self._counts.get(key)
37
+ if entry is not None:
38
+ entry["count"] += c["count"]
39
+ elif len(self._counts) < 5000:
40
+ self._counts[key] = dict(c)
41
+
42
+
43
+ class ActivityLog:
44
+ """Who got which value, and what they did afterwards, between two reports."""
45
+
46
+ def __init__(self) -> None:
47
+ self._lock = threading.Lock()
48
+ self._seen: set[tuple[str, str, str]] = set()
49
+ self._exposures: list[dict[str, str]] = []
50
+ self._events: list[dict[str, Any]] = []
51
+
52
+ def expose(self, flag: str, value: str, user: str) -> None:
53
+ key = (flag, value, user)
54
+ with self._lock:
55
+ if key in self._seen:
56
+ return
57
+ # A cap on memory. Starting over only means some people are reported twice, which the server ignores.
58
+ if len(self._seen) >= 100_000:
59
+ self._seen.clear()
60
+ self._seen.add(key)
61
+ if len(self._exposures) < 10_000:
62
+ self._exposures.append({"flag": flag, "value": value, "user": user})
63
+
64
+ def track(self, event: str, user: str, amount: Optional[float] = None) -> None:
65
+ with self._lock:
66
+ if len(self._events) < 10_000:
67
+ entry: dict[str, Any] = {"event": event, "user": user}
68
+ if amount is not None and amount == amount and abs(amount) != float("inf"):
69
+ entry["amount"] = amount
70
+ self._events.append(entry)
71
+
72
+ def drain(self) -> tuple[list[dict[str, str]], list[dict[str, Any]]]:
73
+ with self._lock:
74
+ out = (self._exposures, self._events)
75
+ self._exposures, self._events = [], []
76
+ return out
77
+
78
+ def restore(self, exposures: list[dict[str, str]], events: list[dict[str, Any]]) -> None:
79
+ with self._lock:
80
+ self._exposures = (exposures + self._exposures)[:10_000]
81
+ self._events = (events + self._events)[:10_000]