Integrate Meilisearch + Redis deeply for cache, locks, searchable decisions/insights, and health telemetry
This commit is contained in:
78
services.py
78
services.py
@@ -4,9 +4,63 @@ import time
|
||||
import uuid
|
||||
import requests
|
||||
import redis
|
||||
import meilisearch_python_sdk
|
||||
from config import settings
|
||||
|
||||
_redis = None
|
||||
_meili = None
|
||||
|
||||
def meili_client():
|
||||
global _meili
|
||||
if _meili is None:
|
||||
try:
|
||||
_meili = meilisearch_python_sdk.Client(settings.meili_url, settings.meili_api_key or None)
|
||||
except Exception:
|
||||
_meili = None
|
||||
return _meili
|
||||
|
||||
def meili_index_doc(index_uid: str, doc: dict):
|
||||
c = meili_client()
|
||||
if not c:
|
||||
return False
|
||||
try:
|
||||
idx = c.index(index_uid)
|
||||
idx.add_documents([doc], primary_key='id')
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def meili_search(index_uid: str, q: str, limit: int = 20):
|
||||
c = meili_client()
|
||||
if not c:
|
||||
return {"hits": []}
|
||||
try:
|
||||
idx = c.index(index_uid)
|
||||
res = idx.search(q, {'limit': limit})
|
||||
if isinstance(res, dict):
|
||||
return res
|
||||
# sdk object fallback
|
||||
return {
|
||||
'hits': getattr(res, 'hits', []),
|
||||
'estimatedTotalHits': getattr(res, 'estimated_total_hits', None),
|
||||
'processingTimeMs': getattr(res, 'processing_time_ms', None),
|
||||
'query': q,
|
||||
}
|
||||
except Exception:
|
||||
return {"hits": []}
|
||||
|
||||
def meili_health():
|
||||
c = meili_client()
|
||||
if not c:
|
||||
return {"ok": False, "error": "client_unavailable"}
|
||||
try:
|
||||
h = c.health()
|
||||
status = getattr(h, 'status', None)
|
||||
if status is None and isinstance(h, dict):
|
||||
status = h.get('status')
|
||||
return {"ok": True, "status": status or 'available'}
|
||||
except Exception as e:
|
||||
return {"ok": False, "error": str(e)[:160]}
|
||||
|
||||
def redis_client():
|
||||
global _redis
|
||||
@@ -300,16 +354,22 @@ def trilium_log(title: str, body: str):
|
||||
|
||||
def qdrant_memory_health():
|
||||
try:
|
||||
# collection-specific
|
||||
r = requests.get(f"{settings.qdrant_url}/collections/{settings.qdrant_collection}", timeout=10)
|
||||
if not r.ok:
|
||||
return {"ok": False, "status": r.status_code}
|
||||
j = r.json().get("result", {})
|
||||
return {
|
||||
"ok": True,
|
||||
"collection": settings.qdrant_collection,
|
||||
"points_count": j.get("points_count"),
|
||||
"indexed_vectors_count": j.get("indexed_vectors_count"),
|
||||
}
|
||||
if r.ok:
|
||||
j = r.json().get("result", {})
|
||||
return {
|
||||
"ok": True,
|
||||
"collection": settings.qdrant_collection,
|
||||
"points_count": j.get("points_count"),
|
||||
"indexed_vectors_count": j.get("indexed_vectors_count"),
|
||||
}
|
||||
# fallback global health/list
|
||||
r2 = requests.get(f"{settings.qdrant_url}/collections", timeout=10)
|
||||
if r2.ok:
|
||||
names=[c.get('name') for c in r2.json().get('result',{}).get('collections',[])]
|
||||
return {"ok": True, "collection": settings.qdrant_collection, "exists": settings.qdrant_collection in names, "collections": names[:20]}
|
||||
return {"ok": False, "status": r.status_code}
|
||||
except Exception as e:
|
||||
return {"ok": False, "error": str(e)[:160]}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user