import os from dotenv import load_dotenv from app.common.text_normalizer import TextNormalizer # from general import * from app.common.general_functions import * from app.common.elastic_helper import ElasticHelper load_dotenv(dotenv_path='config.env') host=os.getenv('HOST_160') password=os.getenv('HOST_160_PASS') # host=os.getenv('HOST_112') # password=os.getenv('HOST_112_PASS') # host=os.getenv('HOST_60') # password=os.getenv('HOST_60_PASS') # host=os.getenv('HOST_80') # password=os.getenv('HOST_80_PASS') print(host) # print(password) path_base_map = "G:/Project2/gateway_php/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) elastic.createIndex("gn_log_action", '', "gn_log_action" ) def meta_to_meta_action(elastic, index_name_i, index_name_o): try: try: body={} list_data = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) lst = list(list_data) index = 0 percent_prev = 0 # text_normal = TextNormalizer() prev_user_id = 0 prev_content = '' prev_data={} update = False for mentry in lst: #print(mentry) index += 1 percent = int((index / len(lst)) * 100) if percent != percent_prev: print("progress -> %.2f %%" % percent) percent_prev = percent id = mentry["id"] entry = mentry["source"] meta = entry.get("meta", []) meta_action={} for m in meta: meta_action[m["key"]] = m["value"] data = entry data["meta_action"]= meta_action eid = id try: resp = elastic.update_index_doc(update, index_name_o, eid, data) except Exception as e: save_error(id, e) except Exception as e: save_error(id, e) def refactorLogSearch(elastic, index_name_i, index_name_o): try: try: body={ "sort":{ "userid": {"order": "asc"}, "query_String.keyword": {"order": "asc"}, "time": {"order": "desc"} } } list_data = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) lst = list(list_data) index = 0 percent_prev = 0 # text_normal = TextNormalizer() prev_user_id = 0 prev_content = '' prev_data={} update = False for mentry in lst: #print(mentry) index += 1 percent = int((index / len(lst)) * 100) if percent != percent_prev: print("progress -> %.2f %%" % percent) percent_prev = percent id = mentry["id"] entry = mentry["source"] user_id = entry.get("userid", "") content = entry.get("query_String", "").strip() if not content : continue content = content.strip("\", ") id_click = entry.get("id_click", "") time_edit = round(float(entry.get("time", ""))/1000) ip = entry.get("id", "") facet = entry.get("facet", "") os_version = entry.get("os_version", "") browser = entry.get("browser", "") action_key = "normal" ref_key="none" values=[] meta={} if id_click : value ={ "id" : id_click, "type": "click", "value": 1 } values.append(value) meta["ip"] = ip if facet : meta["filter"] = facet if browser : meta["browser"] = browser if os_version : meta["os"] = os_version if prev_user_id == user_id and prev_content == content : values = prev_data["values"] if id_click : founded = False i = 0 for value in values : if value["id"] == id_click : founded = True value["value"] = value["value"] + 1 values[i] = value break i +=1 if not founded : values.append( { "id" : id_click, "type": "click", "value": 1 }) prev_data["values"] = values continue elif prev_data and prev_data["id"] : eid = prev_data["id"] try: resp = elastic.update_index_doc(update, index_name_o, eid, prev_data) except Exception as e: save_error(id, e) prev_data = {} data = {} data["id"]= id data["content"]= content data["user_id"]= user_id data["time_edit"]= time_edit data["action_key"]= action_key data["ref_key"]= ref_key data["values"]= values data["meta_action"]= meta prev_data = data prev_user_id = user_id prev_content = content if prev_data : eid = prev_data["id"] try: resp = elastic.update_index_doc(update, index_name_o, eid, prev_data) except Exception as e: save_error(id, e) except Exception as e: save_error(id, e) def refactorLogSearch_0(elastic, index_name_i, index_name_o): try: try: body={ "sort":{ "userid": {"order": "asc"}, "query_String.keyword": {"order": "asc"}, "time": {"order": "desc"} } } list_data = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) lst = list(list_data) index = 0 percent_prev = 0 # text_normal = TextNormalizer() prev_user_id = 0 prev_content = '' prev_data={} update = False for mentry in lst: #print(mentry) index += 1 percent = int((index / len(lst)) * 100) if percent != percent_prev: print("progress -> %.2f %%" % percent) percent_prev = percent id = mentry["id"] entry = mentry["source"] user_id = entry.get("userid", "") content = entry.get("query_String", "").strip() if not content : continue content = content.strip("\", ") id_click = entry.get("id_click", "") time_edit = round(float(entry.get("time", ""))/1000) ip = entry.get("id", "") facet = entry.get("facet", "") os_version = entry.get("os_version", "") browser = entry.get("browser", "") action_key = "normal" ref_key="none" values=[] meta=[] if id_click : value ={ "id" : id_click, "type": "click", "value": 1 } values.append(value) ip_obj ={ "key" : "ip", "value": ip } meta.append(ip_obj) if facet : m ={ "key" : "filter", "value": facet } meta.append(m) if browser : m ={ "key" : "browser", "value": browser } meta.append(m) if os_version : m ={ "key" : "os", "value": os_version } meta.append(m) if prev_user_id == user_id and prev_content == content : values = prev_data["values"] if id_click : founded = False i = 0 for value in values : if value["id"] == id_click : founded = True value["value"] = value["value"] + 1 values[i] = value break i +=1 if not founded : values.append( { "id" : id_click, "type": "click", "value": 1 }) prev_data["values"] = values continue elif prev_data and prev_data["id"] : eid = prev_data["id"] try: resp = elastic.update_index_doc(update, index_name_o, eid, prev_data) except Exception as e: save_error(id, e) prev_data = {} data = {} data["id"]= id data["content"]= content data["user_id"]= user_id data["time_edit"]= time_edit data["action_key"]= action_key data["ref_key"]= ref_key data["values"]= values data["meta"]= meta prev_data = data prev_user_id = user_id prev_content = content if prev_data : eid = prev_data["id"] try: resp = elastic.update_index_doc(update, index_name_o, eid, prev_data) except Exception as e: save_error(id, e) except Exception as e: save_error(id, e) #refactorLogSearch(elastic, "gn_log_search", "gn_log_action") meta_to_meta_action(elastic, "gn_log_action", "gn_log_action")