import json
import datetime
import time
from uuid import uuid4
from elasticsearch import Elasticsearch
from loguru import logger
from typing import List
def now():
return int(time.time() * 1000)
class ESClient(object):
def __init__(self):
self.client = Elasticsearch(["http://9.134.105.179:9200"],
http_auth=("elastic", pwd),
timeout=30)
self.index = "loleinaliao_test"
def add_record(self, record):
record_id = uuid4()
es_rsp = self.client.index(index=self.index, body=record, id=record_id)
logger.info('add es records, name: {}, id: {}, rsp: {}', record.get('name'), record_id, es_rsp)
def update_record(self, body, name):
source_scripts = []
for k in body:
source_scripts.append('ctx._source.{}=params.{}'.format(k, k))
source_script = ';'.join(source_scripts)
es_rsp = self.client.update_by_query(index=self.index,
conflicts='proceed',
body={"query": {"term": {"name": name}},
"script": {"source": source_script,
"lang": "painless",
"params": body}})
print(es_rsp)
def search_records(self, busi: str, table_type: str, env: str, size=10, scroll_id=None, time_range=None):
must = []
must_not = []
must.append({"match_phrase": {"busi": busi}})
must.append({"match_phrase": {"type": table_type}})
must.append({"match_phrase": {"env": env}})
must.append({"match_phrase": {"status": "ok"}})
now_time = datetime.datetime.now() - datetime.timedelta(hours=8)
start = now()
if not time_range:
collect_time = {
"lt": now_time
}
else:
start_time = now_time - datetime.timedelta(days=time_range[0])
end_time = now_time - datetime.timedelta(days=time_range[1])
collect_time = {
"lt": end_time,
"gte": start_time
}
search_body = {
"query": {
"bool": {
"must": must,
"must_not": must_not,
"filter": {
"range": {
"collect_time": collect_time
}
}
}
},
"size": size
}
if scroll_id:
es_rsp = self.client.scroll(scroll_id=scroll_id,
scroll='1m')
else:
es_rsp = self.client.search(index=self.index,
body=search_body,
scroll='1m')
print(es_rsp)
hits = es_rsp['hits']['hits']
if hits:
hits = [hit['_source'] for hit in hits]
else:
pass
return hits, es_rsp.get('_scroll_id'), es_rsp['hits']['total']['value']
ES_CLIENT = ESClient()
按需求封装好es的client,里面包含常用的一些操作,比如add,update,serach 等,然后client使用大概如下
from loleinaliao.elasticsearch.es_client import ES_CLIENT
import datetime
def _gen_es_add_record(msg: dict, rule: dict, env: str, host: str, tags: list, delay: int, case_full_path: str):
db_table = '{}.{}'.format(msg['database'], msg['table'])
sql = "SELECT * FROM {} WHERE {};".format(db_table, rule["where_condition"])
record = {
'name': rule["name"],
'type': rule["type"],
'busi': rule["busi"],
'env': env,
'desc': rule["desc"],
'host': host,
'sql': sql,
'status': rule["status"],
'tags': tags,
'case_full_path': case_full_path,
'create_time': datetime.datetime.now(),
'collect_time': datetime.datetime.now() - datetime.timedelta(hours=8) + datetime.timedelta(hours=delay),
'modify_time': datetime.datetime.now() - datetime.timedelta(hours=8),
}
return record
def gen_record():
msg ={
"database": "database2",
"table":"table2"
}
rule={
"name":"name2",
"type": "type2",
"busi":"busi2",
"desc":"desc2",
"status":"ok",
"where_condition":"flsate =2",
}
case_full_path = "base_test.py"
delay_collect_hours = 1
env ='bvt'
host='12233445'
tags ='tags1'
record = _gen_es_add_record(msg, rule, env, host, tags, delay_collect_hours, case_full_path)
ES_CLIENT.add_record(record)
def update_record():
name = "name1"
body = {"status" : "ok"}
ES_CLIENT.update_record(body, name)
def search_records():
return ES_CLIENT.search_records("busi1", "type1","bvt")
if __name__ == '__main__':
# gen_record()
# update_record()
hits,_,_ = search_records()
print(hits)