openreplay/api/chalicelib/core/log_tool_elasticsearch.py
Kraiem Taha Yassine bf8126639f
Dev (#2377)
* refactor(chalice): upgraded dependencies

* refactor(chalice): upgraded dependencies
feat(chalice): support heatmaps

* feat(chalice): support table-of-browsers showing user-count

* feat(chalice): support table-of-devices showing user-count

* feat(chalice): support table-of-URLs showing user-count

* fix(chalice): fixed Math-operators validation
refactor(chalice): search for sessions that have events for heatmaps

* refactor(chalice): search for sessions that have at least 1 location event for heatmaps

* fix(chalice): fixed Elasticsearch-integration
2024-07-09 17:40:21 +02:00

90 lines
2.9 KiB
Python

import logging
from elasticsearch import Elasticsearch
from chalicelib.core import log_tools
from schemas import schemas
logger = logging.getLogger(__name__)
IN_TY = "elasticsearch"
def get_all(tenant_id):
return log_tools.get_all_by_tenant(tenant_id=tenant_id, integration=IN_TY)
def get(project_id):
return log_tools.get(project_id=project_id, integration=IN_TY)
def update(tenant_id, project_id, changes):
options = {}
if "host" in changes:
options["host"] = changes["host"]
if "apiKeyId" in changes:
options["apiKeyId"] = changes["apiKeyId"]
if "apiKey" in changes:
options["apiKey"] = changes["apiKey"]
if "indexes" in changes:
options["indexes"] = changes["indexes"]
if "port" in changes:
options["port"] = changes["port"]
return log_tools.edit(project_id=project_id, integration=IN_TY, changes=options)
def add(tenant_id, project_id, host, api_key_id, api_key, indexes, port):
options = {
"host": host, "apiKeyId": api_key_id, "apiKey": api_key, "indexes": indexes, "port": port
}
return log_tools.add(project_id=project_id, integration=IN_TY, options=options)
def delete(tenant_id, project_id):
return log_tools.delete(project_id=project_id, integration=IN_TY)
def add_edit(tenant_id, project_id, data: schemas.IntegrationElasticsearchSchema):
s = get(project_id)
if s is not None:
return update(tenant_id=tenant_id, project_id=project_id,
changes={"host": data.host, "apiKeyId": data.api_key_id, "apiKey": data.api_key,
"indexes": data.indexes, "port": data.port})
else:
return add(tenant_id=tenant_id, project_id=project_id,
host=data.host, api_key=data.api_key, api_key_id=data.api_key_id,
indexes=data.indexes, port=data.port)
def __get_es_client(host, port, api_key_id, api_key, use_ssl=False, timeout=15):
scheme = "http" if host.startswith("http") else "https"
host = host.replace("http://", "").replace("https://", "")
try:
args = {
"hosts": [{"host": host, "port": port, "scheme": scheme}],
"verify_certs": use_ssl,
"request_timeout": timeout,
"api_key": api_key
}
es = Elasticsearch(
**args
)
r = es.ping()
if not r and not use_ssl:
return __get_es_client(host, port, api_key_id, api_key, use_ssl=True, timeout=timeout)
if not r:
return None
except Exception as err:
logger.error("================exception connecting to ES host:")
logger.exception(err)
return None
return es
def ping(tenant_id, data: schemas.IntegrationElasticsearchTestSchema):
es = __get_es_client(data.host, data.port, data.api_key_id, data.api_key, timeout=3)
if es is None:
return {"state": False}
return {"state": es.ping()}