Add Elasticsearch support
parent
50820172b1
commit
8afe638f0d
@ -0,0 +1,18 @@
|
||||
from core.lib.elastic import store_msg
|
||||
|
||||
|
||||
def get_balance_hook(user_id, user_name, account_id, account_name, balance):
|
||||
"""
|
||||
Called every time the balance is fetched on an account.
|
||||
Store this into Elasticsearch.
|
||||
"""
|
||||
store_msg(
|
||||
"balances",
|
||||
{
|
||||
"user_id": user_id,
|
||||
"user_name": user_name,
|
||||
"account_id": account_id,
|
||||
"account_name": account_name,
|
||||
"balance": balance,
|
||||
},
|
||||
)
|
@ -0,0 +1,32 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from elasticsearch import Elasticsearch
|
||||
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
client = None
|
||||
|
||||
|
||||
def initialise_elasticsearch():
|
||||
"""
|
||||
Initialise the Elasticsearch client.
|
||||
"""
|
||||
auth = (settings.ELASTICSEARCH_USERNAME, settings.ELASTICSEARCH_PASSWORD)
|
||||
client = Elasticsearch(
|
||||
settings.ELASTICSEARCH_HOST, http_auth=auth, verify_certs=False
|
||||
)
|
||||
return client
|
||||
|
||||
|
||||
def store_msg(index, msg):
|
||||
global client
|
||||
if not client:
|
||||
client = initialise_elasticsearch()
|
||||
if "ts" not in msg:
|
||||
msg["ts"] = datetime.utcnow().isoformat()
|
||||
result = client.index(index=index, body=msg)
|
||||
if not result["result"] == "created":
|
||||
log.error(f"Indexing of '{msg}' failed: {result}")
|
Loading…
Reference in New Issue