-
Notifications
You must be signed in to change notification settings - Fork 8
/
logs_calendar.py
229 lines (189 loc) · 6.51 KB
/
logs_calendar.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import calendar
import json
import shutil
from collections import defaultdict
from datetime import datetime
import pandas
import pytz
import logs_main
from constants import DEFAULT_SERVER_NAME
from c_path import (
Directories,
PathExt,
)
from h_debug import running_time
from h_other import get_report_name_info
CALENDAR = calendar.Calendar()
DF_MAIN_NAME = "_logs_list"
DF_MAIN_EXT = "pkl"
DF_MAIN_FULL_NAME = f"{DF_MAIN_NAME}.{DF_MAIN_EXT}"
DF_MAIN_PATH = Directories.main / DF_MAIN_FULL_NAME
PATH_BKP = Directories.main / f"{DF_MAIN_NAME}.bkp"
PATH_TMP = Directories.main / f"{DF_MAIN_NAME}.tmp"
COLUMN_TYPES = {
"year": "int8",
"month": "int8",
"day": "int8",
# "time": "string",
# "author": "string",
# "server": "string",
# "players": "string",
# "fights": "string",
}
def get_calend_days(y, m):
this_month = CALENDAR.monthdatescalendar(y, m + 1)
return [
[
(cell.strftime("%m-%d"), cell.day)
for cell in row
]
for row in this_month
]
@DF_MAIN_PATH.cache_until_new_self
@running_time
def _read_df(path: PathExt):
try:
return pandas.read_pickle(path)
except FileNotFoundError:
return pandas.DataFrame()
def read_main_df() -> pandas.DataFrame:
return _read_df()
@running_time
def _save_df(df: pandas.DataFrame, path, comp=None):
df.to_pickle(path, compression=comp)
def _save_df_with_backup(df: pandas.DataFrame):
if DF_MAIN_PATH.is_file():
shutil.copy2(DF_MAIN_PATH, PATH_BKP)
_save_df(df, PATH_TMP)
PATH_TMP.replace(DF_MAIN_PATH)
##################################################################
def df_filter_by(df: pandas.DataFrame, category: str, filter_by):
try:
filter_by = int(filter_by)
return df[df[category] == filter_by]
except ValueError:
return df[df[category].apply(lambda x: filter_by in x)]
def normalize_filter(filter: dict):
df = read_main_df()
return {k:v for k,v in filter.items() if v and k in df.columns}
@running_time
def get_logs_list_df_filter(df: pandas.DataFrame, filter: dict):
print(filter)
for filter_key, filter_value in filter.items():
# if filter_key == "server":
# df1 = df_filter_by(df, filter_key, filter_value)
# filter_value = filter_value.replace(" ", "-")
# df2 = df_filter_by(df, filter_key, filter_value)
# print(">>>>>>>>>> get_logs_list_df_filter")
# print(filter_value)
df = df_filter_by(df, filter_key, filter_value)
if df.empty:
break
return df
def separate_to_days(df: pandas.DataFrame):
if df.empty:
return {}
# ['year', 'month', 'day', 'time', 'author', 'server', 'player', 'fight']
columns = list(df.columns)
i_day = columns.index("day") + 1
i_month = columns.index("month") + 1
i_time = columns.index("time") + 1
i_server = columns.index("server") + 1
i_author = columns.index("author") + 1
reports_by_day = defaultdict(list)
for data in df.itertuples():
day_key = f"{data[i_month]:0>2}-{data[i_day]:0>2}"
formatted_report_info = f"{data[i_time]} | {data[i_server]} | {data[i_author]}"
reports_by_day[day_key].append((data[0], formatted_report_info))
return reports_by_day
@running_time
def get_logs_list_filter_json(_filter):
df = read_main_df()
df = get_logs_list_df_filter(df, _filter)
return json.dumps(list(df.index))
def get_logs_list_df_filter_to_calendar_wrap(_filter):
df = read_main_df()
if df.empty:
return {}
_filter = normalize_filter(_filter)
df = get_logs_list_df_filter(df, _filter)
df.sort_values(by="time", inplace=True)
return separate_to_days(df)
##################################################################
def get_timezone_file(report_id):
return Directories.pending_archive / f"{report_id}.timezone"
def get_timezone(report_id):
timezone = get_timezone_file(report_id).read_text()
try:
return pytz.timezone(timezone)
except (ValueError, pytz.exceptions.UnknownTimeZoneError):
return pytz.utc
def get_datetime(report_id):
_info = get_report_name_info(report_id)
date_str = f'{_info["date"]}--{_info["time"]}'
return datetime.strptime(date_str, "%y-%m-%d--%H-%M")
def convert_timezone(report_id):
timezone = get_timezone(report_id)
dt_current = get_datetime(report_id)
dt = timezone.normalize(timezone.localize(dt_current)).astimezone(pytz.utc)
return {
"year": dt.year%1000,
"month": dt.month,
"day": dt.day,
"time": dt.strftime("%H:%M"),
}
def report_data(report_id: str):
report = logs_main.THE_LOGS(report_id)
date = convert_timezone(report_id)
report_name_info = get_report_name_info(report_id)
return date | {
"author": report_name_info["author"],
"server": report_name_info["server"],
"player": tuple(report.get_players_guids().values()),
"fight": tuple(report.get_enc_data()),
}
def make_new(report_ids: list[str]):
if not report_ids:
return
data = {}
for report_id in report_ids:
logs_dir = Directories.logs / report_id
needed_files = (
file_path.is_file()
for file_path in [
logs_dir / "PLAYERS_DATA.json",
logs_dir / "ENCOUNTER_DATA.json",
]
)
if not all(needed_files):
continue
try:
data[report_id] = report_data(report_id)
except Exception:
continue
if not data:
return
return pandas.DataFrame.from_dict(data, orient="index").astype(COLUMN_TYPES)
def _get_default_server(name: str):
_server = get_report_name_info(name)["server"]
return name.replace(_server, DEFAULT_SERVER_NAME)
def add_new_logs(new_reports: list[str]=None):
if new_reports is None:
new_reports = Directories.logs.iterdir()
new_reports = set(new_reports)
df = read_main_df()
df_index = set(df.index)
reports_copies_with_default_name = {
_get_default_server(report_id)
for report_id in new_reports
if DEFAULT_SERVER_NAME not in report_id
}
reports_copies_with_default_name = reports_copies_with_default_name & df_index
folders = new_reports - df_index
new_df = make_new(folders)
if new_df is None and not reports_copies_with_default_name:
return
df.drop(reports_copies_with_default_name, inplace=True)
df = pandas.concat([df, new_df])
df.sort_index()
_save_df_with_backup(df)