Create database.py
Browse files- database.py +80 -0
database.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import httpx
|
| 3 |
+
import asyncio
|
| 4 |
+
|
| 5 |
+
SUPABASE_URL = os.environ.get("SUPABASE_URL", "")
|
| 6 |
+
SUPABASE_KEY = os.environ.get("SUPABASE_KEY", "")
|
| 7 |
+
|
| 8 |
+
HEADERS = {
|
| 9 |
+
"apikey": SUPABASE_KEY,
|
| 10 |
+
"Authorization": f"Bearer {SUPABASE_KEY}",
|
| 11 |
+
"Content-Type": "application/json",
|
| 12 |
+
"Prefer": "return=minimal"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
async def insert(table: str, data: dict):
|
| 16 |
+
"""Insert data ke Supabase"""
|
| 17 |
+
if not SUPABASE_URL or not SUPABASE_KEY:
|
| 18 |
+
return False
|
| 19 |
+
try:
|
| 20 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 21 |
+
response = await client.post(
|
| 22 |
+
f"{SUPABASE_URL}/rest/v1/{table}",
|
| 23 |
+
headers=HEADERS,
|
| 24 |
+
json=data
|
| 25 |
+
)
|
| 26 |
+
return response.status_code in [200, 201]
|
| 27 |
+
except Exception:
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
async def fetch(table: str, limit: int = 50) -> list:
|
| 31 |
+
"""Fetch data dari Supabase"""
|
| 32 |
+
if not SUPABASE_URL or not SUPABASE_KEY:
|
| 33 |
+
return []
|
| 34 |
+
try:
|
| 35 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 36 |
+
response = await client.get(
|
| 37 |
+
f"{SUPABASE_URL}/rest/v1/{table}",
|
| 38 |
+
headers={**HEADERS, "Prefer": "return=representation"},
|
| 39 |
+
params={"order": "created_at.desc", "limit": limit}
|
| 40 |
+
)
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
return response.json()
|
| 43 |
+
except Exception:
|
| 44 |
+
pass
|
| 45 |
+
return []
|
| 46 |
+
|
| 47 |
+
async def log_activity(action: str, details: str = ""):
|
| 48 |
+
"""Simpan activity log ke Supabase"""
|
| 49 |
+
await insert("activity_log", {"action": action, "details": details})
|
| 50 |
+
|
| 51 |
+
async def log_chat(role: str, message: str, provider: str = ""):
|
| 52 |
+
"""Simpan chat history ke Supabase"""
|
| 53 |
+
await insert("chat_history", {"role": role, "message": message, "provider": provider})
|
| 54 |
+
|
| 55 |
+
async def log_module_output(module_name: str, topic: str, output: str, provider: str = ""):
|
| 56 |
+
"""Simpan output modul ke Supabase"""
|
| 57 |
+
await insert("module_output", {
|
| 58 |
+
"module_name": module_name,
|
| 59 |
+
"topic": topic,
|
| 60 |
+
"output": output,
|
| 61 |
+
"provider": provider
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
async def keepalive_ping():
|
| 65 |
+
"""Ping untuk keepalive log"""
|
| 66 |
+
await insert("keepalive_log", {"status": "alive"})
|
| 67 |
+
|
| 68 |
+
async def start_keepalive(space_url: str = ""):
|
| 69 |
+
"""Auto ping setiap 5 menit biar Space tidak sleep"""
|
| 70 |
+
while True:
|
| 71 |
+
try:
|
| 72 |
+
# Ping Supabase
|
| 73 |
+
await keepalive_ping()
|
| 74 |
+
# Ping HF Space sendiri
|
| 75 |
+
if space_url:
|
| 76 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 77 |
+
await client.get(space_url)
|
| 78 |
+
except Exception:
|
| 79 |
+
pass
|
| 80 |
+
await asyncio.sleep(300) # setiap 5 menit
|