-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_gui
More file actions
364 lines (286 loc) · 14.2 KB
/
admin_gui
File metadata and controls
364 lines (286 loc) · 14.2 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import tkinter as tk
from tkinter import ttk, messagebox
import mysql.connector
# ---------------- MySQL Configuration ----------------
MYSQL_CONFIG = {
"host": "10.243.143.218",
"user": "streamer_user",
"password": "1234",
"database": "stock_streamer_db"
}
# ---------------- Database Functions ----------------
def get_connection():
return mysql.connector.connect(**MYSQL_CONFIG)
def fetch_topics():
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("SELECT * FROM topics ORDER BY created_at DESC")
rows = cursor.fetchall()
conn.close()
return rows
def fetch_users():
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("SELECT id, username, email, created_at, last_login FROM users ORDER BY created_at DESC")
rows = cursor.fetchall()
conn.close()
return rows
def approve_topic(topic_name, companies):
conn = get_connection()
cursor = conn.cursor()
cursor.execute("UPDATE topics SET status='approved', companies=%s WHERE topic_name=%s", (companies, topic_name))
conn.commit()
conn.close()
print(f"[Admin] ✅ Approved: {topic_name}")
def deactivate_topic(topic_name):
conn = get_connection()
cursor = conn.cursor()
cursor.execute("UPDATE topics SET status='deactivate' WHERE topic_name=%s", (topic_name,))
conn.commit()
conn.close()
print(f"[Admin] 📴 Deactivated: {topic_name}")
def reject_topic(topic_name):
conn = get_connection()
cursor = conn.cursor()
cursor.execute("UPDATE topics SET status='rejected' WHERE topic_name=%s", (topic_name,))
conn.commit()
conn.close()
print(f"[Admin] ❌ Rejected: {topic_name}")
def fetch_user_subscriptions():
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT us.id, u.username, u.email, t.topic_name, t.status, us.subscribed_at
FROM user_subscriptions us
JOIN users u ON us.user_id = u.id
JOIN topics t ON us.topic_id = t.id
ORDER BY us.subscribed_at DESC
""")
rows = cursor.fetchall()
conn.close()
return rows
def fetch_subscriptions_by_user(user_id):
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT t.topic_name, t.status, t.companies, us.subscribed_at
FROM user_subscriptions us
JOIN topics t ON us.topic_id = t.id
WHERE us.user_id = %s
ORDER BY us.subscribed_at DESC
""", (user_id,))
rows = cursor.fetchall()
conn.close()
return rows
def fetch_subscriptions_by_topic(topic_name):
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT u.id, u.username, u.email, us.subscribed_at
FROM user_subscriptions us
JOIN users u ON us.user_id = u.id
JOIN topics t ON us.topic_id = t.id
WHERE t.topic_name = %s
ORDER BY us.subscribed_at DESC
""", (topic_name,))
rows = cursor.fetchone()
conn.close()
return rows
# ---------------- Tkinter UI ----------------
class AdminGUI:
def __init__(self, root):
self.root = root
self.root.title("🧠 Stock Streamer Admin")
self.root.geometry("1000x700")
# Notebook (Tabs)
self.notebook = ttk.Notebook(root)
self.notebook.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Tab 1: Topics
self.topics_tab = tk.Frame(self.notebook)
self.notebook.add(self.topics_tab, text="📋 Topics")
self.setup_topics_tab()
# Tab 2: Users
self.users_tab = tk.Frame(self.notebook)
self.notebook.add(self.users_tab, text="👥 Users")
self.setup_users_tab()
# Tab 3: Subscriptions
self.subs_tab = tk.Frame(self.notebook)
self.notebook.add(self.subs_tab, text="📊 Subscriptions")
self.setup_subscriptions_tab()
# ============ TOPICS TAB ============
def setup_topics_tab(self):
tk.Label(self.topics_tab, text="📋 Topics Management", font=("Arial", 12, "bold")).pack(pady=(10, 5))
self.tree = ttk.Treeview(self.topics_tab, columns=("ID", "Topic", "Companies", "Status", "Created"), show="headings", height=10)
for col in ("ID", "Topic", "Companies", "Status", "Created"):
self.tree.heading(col, text=col)
self.tree.column("ID", width=50)
self.tree.column("Topic", width=150)
self.tree.column("Companies", width=250)
self.tree.column("Status", width=100)
self.tree.column("Created", width=150)
self.tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
form_frame = tk.Frame(self.topics_tab, relief=tk.RIDGE, bd=1, bg="#f0f0f0")
form_frame.pack(fill=tk.X, padx=10, pady=10)
tk.Label(form_frame, text="Topic:", bg="#f0f0f0", font=("Arial", 9, "bold")).grid(row=0, column=0, padx=10, pady=8, sticky="e")
self.topic_entry = tk.Entry(form_frame, width=30, state='readonly')
self.topic_entry.grid(row=0, column=1, padx=10, pady=8)
tk.Label(form_frame, text="Companies:", bg="#f0f0f0", font=("Arial", 9, "bold")).grid(row=1, column=0, padx=10, pady=8, sticky="e")
self.companies_entry = tk.Entry(form_frame, width=30)
self.companies_entry.grid(row=1, column=1, padx=10, pady=8)
btn_frame = tk.Frame(self.topics_tab)
btn_frame.pack(pady=10)
tk.Button(btn_frame, text="✅ Approve", command=self.approve_selected, bg="#4CAF50", fg="white", width=15).grid(row=0, column=0, padx=5)
tk.Button(btn_frame, text="❌ Reject", command=self.reject_selected, bg="#F44336", fg="white", width=15).grid(row=0, column=1, padx=5)
tk.Button(btn_frame, text="📴 Deactivate", command=self.deactivate_selected, bg="#9C27B0", fg="white", width=15).grid(row=0, column=2, padx=5)
tk.Button(btn_frame, text="👥 Subscribers", command=self.view_topic_subscribers, bg="#607D8B", fg="white", width=15).grid(row=0, column=3, padx=5)
tk.Button(btn_frame, text="🔄 Refresh", command=self.load_topics, bg="#555", fg="white", width=15).grid(row=0, column=4, padx=5)
self.tree.bind("<ButtonRelease-1>", self.select_topic)
self.load_topics()
def load_topics(self):
for item in self.tree.get_children():
self.tree.delete(item)
for t in fetch_topics():
self.tree.insert("", "end", values=(t["id"], t["topic_name"], t["companies"], t["status"], t["created_at"]))
def select_topic(self, event):
selected = self.tree.focus()
if not selected:
return
data = self.tree.item(selected)["values"]
self.topic_entry.config(state='normal')
self.topic_entry.delete(0, tk.END)
self.topic_entry.insert(0, data[1])
self.topic_entry.config(state='readonly')
self.companies_entry.delete(0, tk.END)
if data[2]:
self.companies_entry.insert(0, data[2])
def approve_selected(self):
topic = self.topic_entry.get().strip().lower()
companies = self.companies_entry.get().strip()
if not topic or not companies:
messagebox.showwarning("Missing Data", "Select topic and enter companies")
return
try:
approve_topic(topic, companies)
messagebox.showinfo("Success", f"✅ '{topic}' approved!\n\nProducer will activate it.")
self.load_topics()
except Exception as e:
messagebox.showerror("Error", str(e))
def reject_selected(self):
topic = self.topic_entry.get().strip().lower()
if not topic:
messagebox.showwarning("Missing Data", "Select a topic first")
return
if not messagebox.askyesno("Confirm", f"Reject topic '{topic}'?\n\nIt will not be created in Kafka."):
return
try:
reject_topic(topic)
messagebox.showinfo("Success", f"❌ '{topic}' rejected successfully!")
self.load_topics()
except Exception as e:
messagebox.showerror("Error", str(e))
def deactivate_selected(self):
topic = self.topic_entry.get().strip().lower()
if not topic:
messagebox.showwarning("Missing Data", "Select a topic first")
return
if not messagebox.askyesno("Confirm", f"Deactivate '{topic}'?\n\nThis will delete the Kafka topic."):
return
try:
deactivate_topic(topic)
messagebox.showinfo("Success", f"📴 '{topic}' deactivated!")
self.load_topics()
except Exception as e:
messagebox.showerror("Error", str(e))
def view_topic_subscribers(self):
topic = self.topic_entry.get().strip().lower()
if not topic:
messagebox.showwarning("Missing Data", "Select a topic")
return
try:
subs = fetch_subscriptions_by_topic(topic)
win = tk.Toplevel(self.root)
win.title(f"Subscribers: {topic}")
win.geometry("700x400")
tk.Label(win, text=f"Users subscribed to '{topic}'", font=("Arial", 12, "bold")).pack(pady=10)
tree = ttk.Treeview(win, columns=("User ID", "Username", "Email", "Subscribed"), show="headings")
for col in ("User ID", "Username", "Email", "Subscribed"):
tree.heading(col, text=col)
tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
if subs:
for s in subs:
tree.insert("", "end", values=(s["id"], s["username"], s["email"], s["subscribed_at"]))
else:
tk.Label(win, text="No subscribers yet", fg="#999").pack(pady=20)
tk.Button(win, text="Close", command=win.destroy, bg="#555", fg="white").pack(pady=10)
except Exception as e:
messagebox.showerror("Error", str(e))
# ============ USERS TAB ============
def setup_users_tab(self):
tk.Label(self.users_tab, text="👥 User Management", font=("Arial", 12, "bold")).pack(pady=(10, 5))
self.users_tree = ttk.Treeview(self.users_tab, columns=("ID", "Username", "Email", "Created", "Last Login"), show="headings", height=12)
for col in ("ID", "Username", "Email", "Created", "Last Login"):
self.users_tree.heading(col, text=col)
self.users_tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
btn_frame = tk.Frame(self.users_tab)
btn_frame.pack(pady=10)
tk.Button(btn_frame, text="📋 View Subscriptions", command=self.view_user_subs, bg="#2196F3", fg="white", width=20).grid(row=0, column=0, padx=5)
tk.Button(btn_frame, text="🔄 Refresh", command=self.load_users, bg="#555", fg="white", width=20).grid(row=0, column=1, padx=5)
self.load_users()
def load_users(self):
for item in self.users_tree.get_children():
self.users_tree.delete(item)
for u in fetch_users():
self.users_tree.insert("", "end", values=(u["id"], u["username"], u["email"], u["created_at"], u["last_login"] or "Never"))
def view_user_subs(self):
selected = self.users_tree.focus()
if not selected:
messagebox.showwarning("No Selection", "Select a user")
return
data = self.users_tree.item(selected)["values"]
user_id = data[0]
username = data[1]
try:
subs = fetch_subscriptions_by_user(user_id)
win = tk.Toplevel(self.root)
win.title(f"Subscriptions: {username}")
win.geometry("700x400")
tk.Label(win, text=f"Subscriptions for '{username}'", font=("Arial", 12, "bold")).pack(pady=10)
tree = ttk.Treeview(win, columns=("Topic", "Status", "Companies", "Subscribed"), show="headings")
for col in ("Topic", "Status", "Companies", "Subscribed"):
tree.heading(col, text=col)
tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
if subs:
for s in subs:
tree.insert("", "end", values=(s["topic_name"], s["status"], s["companies"], s["subscribed_at"]))
tk.Label(win, text=f"Total: {len(subs)}", font=("Arial", 10, "bold")).pack(pady=5)
else:
tk.Label(win, text="No subscriptions", fg="#999").pack(pady=20)
tk.Button(win, text="Close", command=win.destroy, bg="#555", fg="white").pack(pady=10)
except Exception as e:
messagebox.showerror("Error", str(e))
# ============ SUBSCRIPTIONS TAB ============
def setup_subscriptions_tab(self):
tk.Label(self.subs_tab, text="📊 All Subscriptions", font=("Arial", 12, "bold")).pack(pady=(10, 5))
self.subs_tree = ttk.Treeview(self.subs_tab, columns=("ID", "Username", "Email", "Topic", "Status", "Subscribed"), show="headings", height=15)
for col in ("ID", "Username", "Email", "Topic", "Status", "Subscribed"):
self.subs_tree.heading(col, text=col)
self.subs_tree.column("ID", width=50)
self.subs_tree.column("Username", width=120)
self.subs_tree.column("Email", width=180)
self.subs_tree.column("Topic", width=150)
self.subs_tree.column("Status", width=100)
self.subs_tree.column("Subscribed", width=150)
self.subs_tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
tk.Button(self.subs_tab, text="🔄 Refresh", command=self.load_subscriptions, bg="#555", fg="white", width=20).pack(pady=10)
self.load_subscriptions()
def load_subscriptions(self):
for item in self.subs_tree.get_children():
self.subs_tree.delete(item)
subs = fetch_user_subscriptions()
for s in subs:
self.subs_tree.insert("", "end", values=(s["id"], s["username"], s["email"], s["topic_name"], s["status"], s["subscribed_at"]))
# ---------------- Run ----------------
if __name__ == "__main__":
root = tk.Tk()
app = AdminGUI(root)
root.mainloop()