Spaces:
Paused
Paused
Update periodic_tuner.py
Browse files- periodic_tuner.py +42 -6
periodic_tuner.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# 🗓️ periodic_tuner.py (V4.
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
@@ -163,8 +163,15 @@ class AutoTunerScheduler:
|
|
| 163 |
def __init__(self, trade_manager):
|
| 164 |
self.trade_manager = trade_manager
|
| 165 |
self.state_file = "scheduler_state.json"
|
|
|
|
|
|
|
| 166 |
self.last_weekly_run = None
|
| 167 |
self.last_monthly_run = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
self.is_running = False
|
| 169 |
logger.info("🕰️ [Scheduler] Auto-Tuner Armed & Ready.")
|
| 170 |
|
|
@@ -196,14 +203,21 @@ class AutoTunerScheduler:
|
|
| 196 |
self.last_weekly_run = datetime.fromisoformat(state['last_weekly'])
|
| 197 |
if state.get('last_monthly'):
|
| 198 |
self.last_monthly_run = datetime.fromisoformat(state['last_monthly'])
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
except Exception: pass
|
| 201 |
|
| 202 |
async def _save_state(self):
|
| 203 |
try:
|
| 204 |
state = {
|
| 205 |
"last_weekly": self.last_weekly_run.isoformat() if self.last_weekly_run else None,
|
| 206 |
-
"last_monthly": self.last_monthly_run.isoformat() if self.last_monthly_run else None
|
|
|
|
|
|
|
| 207 |
}
|
| 208 |
if self.trade_manager.r2:
|
| 209 |
await self.trade_manager.r2.upload_json_async(state, self.state_file)
|
|
@@ -231,9 +245,14 @@ class AutoTunerScheduler:
|
|
| 231 |
success = await run_surgical_tuning(period_type, use_fixed_list=True)
|
| 232 |
|
| 233 |
if success:
|
| 234 |
-
# 2. Update Timestamps
|
| 235 |
-
if period_type == 'weekly':
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
await self._save_state()
|
| 238 |
|
| 239 |
# 3. 🔥 HOT RELOAD LIVE SYSTEM (The Final Sync)
|
|
@@ -247,6 +266,23 @@ class AutoTunerScheduler:
|
|
| 247 |
finally:
|
| 248 |
self.is_running = False
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
if __name__ == "__main__":
|
| 251 |
parser = argparse.ArgumentParser()
|
| 252 |
parser.add_argument('--type', type=str, default='weekly', help='weekly or monthly')
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# 🗓️ periodic_tuner.py (V4.2 - GEM-Architect: Status Metrics)
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import asyncio
|
|
|
|
| 163 |
def __init__(self, trade_manager):
|
| 164 |
self.trade_manager = trade_manager
|
| 165 |
self.state_file = "scheduler_state.json"
|
| 166 |
+
|
| 167 |
+
# التوقيتات
|
| 168 |
self.last_weekly_run = None
|
| 169 |
self.last_monthly_run = None
|
| 170 |
+
|
| 171 |
+
# العدادات (Status Counters)
|
| 172 |
+
self.weekly_count = 0
|
| 173 |
+
self.monthly_count = 0
|
| 174 |
+
|
| 175 |
self.is_running = False
|
| 176 |
logger.info("🕰️ [Scheduler] Auto-Tuner Armed & Ready.")
|
| 177 |
|
|
|
|
| 203 |
self.last_weekly_run = datetime.fromisoformat(state['last_weekly'])
|
| 204 |
if state.get('last_monthly'):
|
| 205 |
self.last_monthly_run = datetime.fromisoformat(state['last_monthly'])
|
| 206 |
+
|
| 207 |
+
# استرجاع العدادات
|
| 208 |
+
self.weekly_count = state.get('weekly_count', 0)
|
| 209 |
+
self.monthly_count = state.get('monthly_count', 0)
|
| 210 |
+
|
| 211 |
+
logger.info(f" 🕰️ [Scheduler] State Restored (W:{self.weekly_count} | M:{self.monthly_count}).")
|
| 212 |
except Exception: pass
|
| 213 |
|
| 214 |
async def _save_state(self):
|
| 215 |
try:
|
| 216 |
state = {
|
| 217 |
"last_weekly": self.last_weekly_run.isoformat() if self.last_weekly_run else None,
|
| 218 |
+
"last_monthly": self.last_monthly_run.isoformat() if self.last_monthly_run else None,
|
| 219 |
+
"weekly_count": self.weekly_count,
|
| 220 |
+
"monthly_count": self.monthly_count
|
| 221 |
}
|
| 222 |
if self.trade_manager.r2:
|
| 223 |
await self.trade_manager.r2.upload_json_async(state, self.state_file)
|
|
|
|
| 245 |
success = await run_surgical_tuning(period_type, use_fixed_list=True)
|
| 246 |
|
| 247 |
if success:
|
| 248 |
+
# 2. Update Timestamps & Counters
|
| 249 |
+
if period_type == 'weekly':
|
| 250 |
+
self.last_weekly_run = datetime.now()
|
| 251 |
+
self.weekly_count += 1
|
| 252 |
+
else:
|
| 253 |
+
self.last_monthly_run = datetime.now()
|
| 254 |
+
self.monthly_count += 1
|
| 255 |
+
|
| 256 |
await self._save_state()
|
| 257 |
|
| 258 |
# 3. 🔥 HOT RELOAD LIVE SYSTEM (The Final Sync)
|
|
|
|
| 266 |
finally:
|
| 267 |
self.is_running = False
|
| 268 |
|
| 269 |
+
# ✅ دالة جلب المقاييس للواجهة
|
| 270 |
+
def get_status_metrics(self):
|
| 271 |
+
def _fmt_time(last_dt):
|
| 272 |
+
if not last_dt: return "Pending"
|
| 273 |
+
diff = datetime.now() - last_dt
|
| 274 |
+
d = diff.days
|
| 275 |
+
h = diff.seconds // 3600
|
| 276 |
+
return f"{d}d {h}h"
|
| 277 |
+
|
| 278 |
+
return {
|
| 279 |
+
"weekly_timer": _fmt_time(self.last_weekly_run),
|
| 280 |
+
"weekly_count": self.weekly_count,
|
| 281 |
+
"monthly_timer": _fmt_time(self.last_monthly_run),
|
| 282 |
+
"monthly_count": self.monthly_count,
|
| 283 |
+
"is_running": self.is_running
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
if __name__ == "__main__":
|
| 287 |
parser = argparse.ArgumentParser()
|
| 288 |
parser.add_argument('--type', type=str, default='weekly', help='weekly or monthly')
|