-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
209 lines (169 loc) · 8.62 KB
/
Copy pathstorage.py
File metadata and controls
209 lines (169 loc) · 8.62 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
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
import json
import os
from datetime import datetime, timedelta
from typing import Dict, Optional, List, Set
from models import ShiftRecord
from config import config
class ShiftStorage:
"""Хранилище данных о сменах"""
def __init__(self, data_file: str = config.DATA_FILE):
self.data_file = data_file
self.active_shifts: Dict[int, ShiftRecord] = {}
self.shift_history: List[ShiftRecord] = []
self.group_owners: Dict[int, Set[int]] = {} # chat_id -> set of owner_ids
self._load_data()
def _ensure_data_dir(self):
"""Создать папку для данных если не существует"""
data_dir = os.path.dirname(self.data_file)
if data_dir and not os.path.exists(data_dir):
os.makedirs(data_dir)
def _load_data(self):
"""Загрузить данные из файла"""
if not os.path.exists(self.data_file):
return
try:
with open(self.data_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Загружаем активные смены
self.active_shifts = {
int(user_id): ShiftRecord.from_dict(shift_data)
for user_id, shift_data in data.get('active_shifts', {}).items()
}
# Загружаем историю
self.shift_history = [
ShiftRecord.from_dict(shift_data)
for shift_data in data.get('shift_history', [])
]
self._clean_old_records()
print(f"✅ Загружено: {len(self.active_shifts)} активных смен, "
f"{len(self.shift_history)} записей истории")
except Exception as e:
print(f"⚠️ Ошибка загрузки данных: {e}")
def _clean_old_records(self):
"""Удалить записи истории, старые чем KEEP_HISTORY_DAYS дней"""
# Сохраняем исходное количество
original_count = len(self.shift_history)
# Вычисляем дату отсечки
cutoff_date = datetime.now() - timedelta(days=config.KEEP_HISTORY_DAYS)
# Оставляем только свежие записи (старше cutoff_date)
self.shift_history = [
shift for shift in self.shift_history
if shift.start_datetime > cutoff_date
]
# Подсчитываем удалённые записи
deleted_count = original_count - len(self.shift_history)
if deleted_count > 0:
print(f"️ Удалено {deleted_count} старых записей (старше {config.KEEP_HISTORY_DAYS} дней)")
self._save_data()
def _save_data(self):
"""Сохранить данные в файл"""
self._ensure_data_dir()
data = {
'active_shifts': {
str(user_id): shift.to_dict()
for user_id, shift in self.active_shifts.items()
},
'shift_history': [shift.to_dict() for shift in self.shift_history]
}
try:
with open(self.data_file, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"⚠️ Ошибка сохранения данных: {e}")
def get_user_schedule(self, user_id: int) -> tuple:
"""Получить расписание пользователя"""
return config.USER_SCHEDULES.get(user_id, (9, 18)) # По умолчанию 9-18
def is_off_day(self) -> bool:
"""Проверить: сегодня выходной?"""
weekday = datetime.now().weekday() # 0=Пн, 6=Вс
return weekday in config.OFF_DAYS
def is_working_hours(self, user_id: int) -> bool:
"""Проверить: сейчас рабочие часы пользователя?"""
from datetime import time
# Если сегодня выходной, рабочих часов нет
if self.is_off_day():
return False
current_time = datetime.now().time()
start_hour, end_hour = self.get_user_schedule(user_id)
# Если конец смены раньше чем начало (ночная смена, например 17-02)
if end_hour < start_hour:
# Ночная смена: работаем либо после start_hour, либо до end_hour
return current_time >= time(start_hour, 0) or current_time < time(end_hour, 0)
else:
# Дневная смена: работаем между start_hour и end_hour
return time(start_hour, 0) <= current_time < time(end_hour, 0)
def check_forgot_to_checkin(self, user_id: int) -> bool:
"""Проверить: пользователь забыл отметиться приходом?
Возвращает True если:
- Сейчас рабочие часы пользователя
- Но он не отметился приходом
"""
return self.is_working_hours(user_id) and not self.is_on_shift(user_id)
def check_forgot_to_checkout(self, user_id: int) -> bool:
"""Проверить: пользователь забыл отметиться уходом?
Возвращает True если:
- Пользователь на смене (есть start_time)
- Но смена длится дольше MAX_SHIFT_HOURS часов
"""
shift = self.get_active_shift(user_id)
if not shift:
return False
elapsed_time = datetime.now() - shift.start_datetime
max_hours = config.MAX_SHIFT_HOURS
return elapsed_time.total_seconds() / 3600 > max_hours
def start_shift(self, user_id: int, username: str, full_name: str) -> ShiftRecord:
"""Начать смену"""
shift = ShiftRecord(
user_id=user_id,
username=username,
full_name=full_name,
start_time=datetime.now().isoformat()
)
self.active_shifts[user_id] = shift
self._save_data()
return shift
def end_shift(self, user_id: int) -> Optional[ShiftRecord]:
"""Закончить смену"""
shift = self.active_shifts.pop(user_id, None)
if shift:
shift.end_time = datetime.now().isoformat()
self.shift_history.append(shift)
self._clean_old_records() # Очищаем при добавлении новой записи
self._save_data()
return shift
def is_on_shift(self, user_id: int) -> bool:
"""Проверка: на смене ли пользователь"""
return user_id in self.active_shifts
def get_active_shift(self, user_id: int) -> Optional[ShiftRecord]:
"""Получить активную смену пользователя"""
return self.active_shifts.get(user_id)
def get_user_history(self, user_id: int, limit: int = 10) -> List[ShiftRecord]:
"""Получить историю смен пользователя"""
user_shifts = [s for s in self.shift_history if s.user_id == user_id]
return user_shifts[-limit:]
def get_all_active_shifts(self) -> List[ShiftRecord]:
"""Получить все активные смены"""
return list(self.active_shifts.values())
async def update_group_owners(self, chat_id: int, owners: Set[int]):
"""Обновить список владельцев группы"""
self.group_owners[chat_id] = owners
def is_group_owner(self, chat_id: int, user_id: int) -> bool:
"""Проверить является ли пользователь владельцем группы"""
return user_id in self.group_owners.get(chat_id, set())
def get_all_users_stats(self) -> Dict[int, Dict]:
"""Получить статистику по всем пользователям"""
stats = {}
for shift in self.shift_history:
user_id = shift.user_id
if user_id not in stats:
stats[user_id] = {
'name': shift.full_name,
'username': shift.username,
'shifts_count': 0,
'total_hours': 0
}
stats[user_id]['shifts_count'] += 1
if shift.duration():
hours = shift.duration().total_seconds() / 3600
stats[user_id]['total_hours'] += hours
return stats