-
Notifications
You must be signed in to change notification settings - Fork 11
/
rule.py
40 lines (31 loc) · 1017 Bytes
/
rule.py
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
from dataclasses import dataclass
from typing import Dict, Optional, Tuple
@dataclass
class Rule:
group: str = "default"
method: str = "*"
second: Optional[int] = None
minute: Optional[int] = None
hour: Optional[int] = None
day: Optional[int] = None
month: Optional[int] = None
block_time: Optional[int] = None
zone: Optional[str] = None
def ruleset(self, path: str, user: str) -> Dict[str, Tuple[int, int]]:
"""
builds a dictionary of keys, values where keys are
the redis keys and values is a tuple of (limit, ttl)
"""
return {
f"{path}:{self.method}:{user}:{name}": (limit, TTL[name])
for name, limit in map(lambda name: (name, getattr(self, name)), RULENAMES)
if limit is not None
}
TTL = {
"second": 1,
"minute": 60,
"hour": 60 * 60,
"day": 24 * 60 * 60,
"month": 31 * 24 * 60 * 60,
}
RULENAMES: Tuple[str, ...] = ("second", "minute", "hour", "day", "month")