-
Notifications
You must be signed in to change notification settings - Fork 8
/
top.py
211 lines (176 loc) · 5.72 KB
/
top.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from collections import defaultdict
from itertools import islice
from typing import Union
from pydantic import BaseModel, field_validator
from api_db import (
DB,
DataCompressed,
LIMITS,
LIMITS_JOINED,
)
from api_top_db_v2 import (
Columns,
TopDBCached,
HEADERS_TO_COLUMNS_NAMES,
SORT_REVERSED,
)
from c_bosses import ALL_FIGHT_NAMES
from c_path import Directories
from h_debug import running_time
from h_server_fix import server_cnv
API_EXAMPLES = [
{
"server": "Lordaeron",
"boss": "The Lich King",
"mode": "25H",
"class_i": 5,
"spec_i": 3,
"limit": LIMITS[0],
},
]
def spec_db_query(class_i: int, spec_i: int):
if class_i == -1:
spec_q = ""
elif spec_i == -1:
spec_q = f"WHERE {Columns.SPEC} between {class_i*4} and {class_i*4+3}"
else:
spec_i = class_i * 4 + spec_i
spec_q = f"WHERE {Columns.SPEC}={spec_i}"
return spec_q
class TopValidation(BaseModel):
server: str
boss: str
mode: str
class_i: int
spec_i: int
sort_by: str = Columns.USEFUL_DPS
limit: int = LIMITS[1]
best_only: bool = True
model_config = {
"json_schema_extra": {
"examples": API_EXAMPLES,
}
}
def build_query_string(self):
if self.best_only:
select_q = f"{Columns.FORMATTED_STRING}, guid"
else:
select_q = Columns.FORMATTED_STRING
table_name = DB.get_table_name(self.boss, self.mode)
spec_q = spec_db_query(self.class_i, self.spec_i)
order = "ASC" if self.sort_by in SORT_REVERSED else "DESC"
return f'''
SELECT {select_q}
FROM [{table_name}]
{spec_q}
ORDER BY {self.sort_by} {order}
'''
@field_validator("server")
@classmethod
def validate_server(cls, server: str):
server = server_cnv(server)
servers = Directories.top.files_stems()
if server not in servers:
_list = ", ".join(servers)
raise ValueError(f"[server] value value must be from [{_list}]")
return server
@field_validator("boss")
@classmethod
def validate_boss(cls, boss: str):
if boss not in ALL_FIGHT_NAMES:
_list = ", ".join(ALL_FIGHT_NAMES)
raise ValueError(f"[boss] value value must be from [{_list}]")
return boss
@field_validator("mode")
@classmethod
def validate_mode(cls, mode: str):
mode = mode.upper()
modes = ["10N", "10H", "25N", "25H"]
if mode not in modes:
_list = ", ".join(modes)
raise ValueError(f"[boss] value value must be from [{_list}]")
return mode
@field_validator("class_i")
@classmethod
def validate_class_i(cls, class_i: Union[str, int]):
class_i = int(class_i)
if class_i not in range(-1, 10):
raise ValueError("[class_i] value must be from -1 to 9")
return class_i
@field_validator("spec_i")
@classmethod
def validate_spec_i(cls, spec_i: Union[str, int]):
spec_i = int(spec_i)
if spec_i not in {-1, 1, 2, 3}:
raise ValueError("[spec_i] value must be from [-1, 1, 2, 3]")
return spec_i
@field_validator("sort_by")
@classmethod
def validate_sort_by(cls, sort_by: str):
try:
return HEADERS_TO_COLUMNS_NAMES[sort_by]
except KeyError:
_list = ", ".join(HEADERS_TO_COLUMNS_NAMES)
raise ValueError(f"[sort_by] value value must be from [{_list}]")
@field_validator("limit")
@classmethod
def validate_limit(cls, limit: Union[str, int]):
limit = int(limit)
if limit not in LIMITS:
raise ValueError(f"[limit] value value must be from [{LIMITS_JOINED}]")
return limit
class Top(TopDBCached):
cache: defaultdict[str, dict[int, DataCompressed]] = defaultdict(dict)
def __init__(self, model: TopValidation) -> None:
super().__init__(model.server)
self.json_query = model.model_dump_json()
self.db_query = model.build_query_string()
self.limit = model.limit
self.best_only = model.best_only
def get_data(self):
if self.db_was_updated():
self.cache[self.server].clear()
server_data = self.cache[self.server]
if self.json_query not in server_data:
server_data[self.json_query] = self._renew_data()
return server_data[self.json_query]
@running_time
def _renew_data(self):
try:
db_json_strings = self._db_json_strings()
except Exception: # sqlite3.OperationalError
db_json_strings = []
_json = self._combine_json(db_json_strings)
return DataCompressed(_json)
def _db_json_strings(self):
if self.best_only:
return self._best()
return self._all()
@staticmethod
def _combine_json(data: list[bytes]):
if not data:
return b'[]'
data[0] = b"[" + data[0]
data[-1] = data[-1] + b"]"
return b','.join(data)
def _best(self) -> list[bytes]:
rows_generator = self.cursor.execute(self.db_query)
s = {}
for json_text, guid in rows_generator:
if guid in s:
continue
s[guid] = json_text
if len(s) >= self.limit:
break
return list(s.values())
def _all(self) -> list[bytes]:
rows_generator = self.cursor.execute(self.db_query)
return [v for v, in islice(rows_generator, self.limit)]
def _test1():
conf = API_EXAMPLES[0]
q = TopValidation(**conf)
z = Top(q)
for x in z._db_json_strings():
print(x)
if __name__ == "__main__":
_test1()