-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache_commands.py
More file actions
202 lines (169 loc) · 7.62 KB
/
Copy pathcache_commands.py
File metadata and controls
202 lines (169 loc) · 7.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
"""
פקודות ניהול Cache לבוט
Cache Management Commands for Bot
"""
import logging
import json
import hashlib
from datetime import datetime, timezone
from telegram import Update
from telegram.ext import ContextTypes, CommandHandler
from cache_manager import cache
from html import escape as html_escape
from chatops.permissions import admin_required
logger = logging.getLogger(__name__)
def _get_db():
"""Attempt to fetch DB via webapp.app.get_db to reuse connection config."""
try:
from webapp.app import get_db as _get
return _get()
except Exception:
return None
async def cache_stats_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""הצגת סטטיסטיקות cache"""
user_id = update.effective_user.id
# בדיקה אם המשתמש הוא admin (אופציונלי)
# if user_id not in ADMIN_USER_IDS:
# await update.message.reply_text("❌ פקודה זמינה רק למנהלים")
# return
try:
stats = cache.get_stats()
if not stats.get("enabled", False):
await update.message.reply_text(
"📊 <b>סטטיסטיקות Cache</b>\n\n"
"❌ Redis Cache מושבת\n"
"💡 להפעלה: הגדר <code>REDIS_URL</code> במשתני הסביבה",
parse_mode='HTML'
)
return
if "error" in stats:
await update.message.reply_text(
f"📊 <b>סטטיסטיקות Cache</b>\n\n"
f"⚠️ <b>שגיאה:</b> {html_escape(stats['error'])}",
parse_mode='HTML'
)
return
# הצגת סטטיסטיקות מפורטות
hit_rate = stats.get('hit_rate', 0)
hit_emoji = "🎯" if hit_rate > 80 else "📈" if hit_rate > 60 else "📉"
message = (
f"📊 <b>סטטיסטיקות Cache</b>\n\n"
f"✅ <b>סטטוס:</b> פעיל\n"
f"💾 <b>זיכרון בשימוש:</b> {stats.get('used_memory', 'N/A')}\n"
f"👥 <b>חיבורים:</b> {stats.get('connected_clients', 0)}\n\n"
f"🎯 <b>ביצועי Cache:</b>\n"
f"{hit_emoji} <b>Hit Rate:</b> {hit_rate}%\n"
f"✅ <b>Hits:</b> {stats.get('keyspace_hits', 0):,}\n"
f"❌ <b>Misses:</b> {stats.get('keyspace_misses', 0):,}\n\n"
f"💡 <b>טיפ:</b> Hit Rate גבוה = ביצועים טובים יותר!"
)
await update.message.reply_text(message, parse_mode='HTML')
except Exception as e:
logger.error(f"שגיאה בהצגת סטטיסטיקות cache: {e}")
await update.message.reply_text(
"❌ שגיאה בקבלת סטטיסטיקות cache"
)
async def clear_cache_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""ניקוי cache של המשתמש"""
user_id = update.effective_user.id
try:
deleted = cache.invalidate_user_cache(user_id)
await update.message.reply_text(
f"🧹 <b>Cache נוקה בהצלחה!</b>\n\n"
f"🗑️ נמחקו {deleted} ערכים\n"
f"⚡ הפעולות הבאות יהיו מעט איטיות יותר עד שה-cache יתמלא מחדש",
parse_mode='HTML'
)
except Exception as e:
logger.error(f"שגיאה בניקוי cache: {e}")
await update.message.reply_text("❌ שגיאה בניקוי cache")
def setup_cache_handlers(application):
"""הוספת handlers לפקודות cache"""
application.add_handler(CommandHandler("cache_stats", cache_stats_command))
application.add_handler(CommandHandler("clear_cache", clear_cache_command))
application.add_handler(CommandHandler("cache_warm", cache_warm_command))
application.add_handler(CommandHandler("debug_cache", admin_required(debug_cache_command)))
logger.info("Cache handlers הוגדרו בהצלחה")
async def cache_warm_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""חימום קאש מהיר לסטטיסטיקות משתמש (/cache_warm)."""
user_id = update.effective_user.id
try:
db = _get_db()
if db is None:
await update.message.reply_text("❌ DB לא זמין לחימום קאש כרגע")
return
# חשב סטטיסטיקות כמו ב-/api/stats (תמציתי)
active_query = {
'user_id': user_id,
'$or': [
{'is_active': True},
{'is_active': {'$exists': False}}
]
}
stats = {
'total_files': db.code_snippets.count_documents(active_query),
'languages': list(db.code_snippets.distinct('programming_language', active_query)),
'recent_activity': []
}
recent = db.code_snippets.find(active_query, {'file_name': 1, 'created_at': 1}).sort('created_at', -1).limit(10)
for item in recent:
stats['recent_activity'].append({
'file_name': item.get('file_name', ''),
'created_at': (item.get('created_at') or datetime.now(timezone.utc)).isoformat()
})
# בניית מפתח הקאש לפי אותו פורמט
_params = {}
_raw = json.dumps(_params, sort_keys=True, ensure_ascii=False)
_hash = hashlib.sha256(_raw.encode('utf-8')).hexdigest()[:16]
stats_cache_key = f"api:stats:user:{user_id}:{_hash}"
# שמירה עם TTL דינמי
try:
cache.set_dynamic(
stats_cache_key,
stats,
"user_stats",
{
"user_id": user_id,
"user_tier": "regular",
"endpoint": "api_stats",
"access_frequency": "high",
},
)
except Exception:
cache.set(stats_cache_key, stats, 120)
await update.message.reply_text("✅ קאש חומם בהצלחה לסטטיסטיקות משתמש")
except Exception as e:
logger.error(f"שגיאה בחימום קאש: {e}")
await update.message.reply_text("❌ שגיאה בחימום קאש")
async def debug_cache_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""/debug_cache <seconds> – הפעלת לוגים זמניים של HIT/MISS/SET (מנהלים בלבד).
שימוש:
- /debug_cache 300 (5 דקות)
- /debug_cache 0 (כיבוי)
"""
try:
raw = (context.args[0] if getattr(context, "args", None) else "").strip()
except Exception:
raw = ""
if not raw:
await update.message.reply_text("שימוש: /debug_cache <seconds>\nדוגמה: /debug_cache 300", parse_mode=None)
return
try:
seconds = int(raw)
except Exception:
await update.message.reply_text("❌ seconds חייב להיות מספר שלם. דוגמה: /debug_cache 300", parse_mode=None)
return
until_ts = cache.enable_debug_for(seconds)
# עדכון ידידותי: אם מכובה, אין טעם להציג תאריך
if seconds <= 0:
await update.message.reply_text("✅ Debug Cache כובה", parse_mode=None)
return
try:
until_dt = datetime.fromtimestamp(float(until_ts), tz=timezone.utc).isoformat()
except Exception:
until_dt = str(until_ts)
await update.message.reply_text(
f"✅ Debug Cache הופעל/הוארך ל-{seconds} שניות\n"
f"⏱️ debug_until (UTC): {html_escape(until_dt)}",
parse_mode="HTML",
)