# import mariadb from dotenv import load_dotenv import os import datetime import json import uuid import pandas as pd import zipfile import sys import time from tqdm import tqdm from pydantic import BaseModel sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) from app.common.elastic_helper import ElasticHelper from app.common.general_functions import * from app.common.text_normalizer import TextNormalizer from app.common.data_helper import DataHelper log_file = None def import_contexts_of_json_files(elastic, input_file, file_jump_ids, version_key="base_v3", index_name_o = "mj_qa_context"): jump_ids = [] if os.path.exists(file_jump_ids): with open(file_jump_ids, "r") as f: jump_ids = f.readlines() data_all = [] try: data_all = DataHelper.load_orjson(input_file) except Exception as e: print("********** error read data file json " , input_file , str(e)) return total = len(data_all) print( "-" * 50) print( " total count --> ", total) print( "-" * 50) percent_prev = 0 count = 0 bulk_data = [] total_count = 0 for item in data_all : count += 1 _id = item["_id"] _source = item["_source"] percent = int((count / total) * 100) if percent != percent_prev: print("import_contexts_of_json_files -> %.2f %%" % percent, total) percent_prev = percent _source["title_type"]= getTitleType(_source["qanon_title"]) contexts = _source["contexts"] if contexts : context_new = [] for ct in contexts: context_new.append( { "context_type": ct["type"], "title": ct["mention"], "content": ct["resolved"] } ) _source["context"] = context_new del _source["contexts"] _source["version_key"] = version_key bulk_data.append({"_id": _id, "_source": _source}) if len(bulk_data) > 100: total_count += len(bulk_data) print("=" * 10, " to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] if len(bulk_data) > 0: total_count += len(bulk_data) print("=" * 10, " to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] with open(file_jump_ids, "w") as f: f.write("\n".join(jump_ids)) print("end success import_to_elastic") def import_rules_of_json_files(elastic, input_file, file_jump_ids, version_key="base_v3", index_name_o = "mj_qa_rule"): jump_ids = [] if os.path.exists(file_jump_ids): with open(file_jump_ids, "r") as f: jump_ids = f.readlines() data_all = [] try: data_all = DataHelper.load_orjson(input_file) except Exception as e: print("********** error read data file json " , input_file , str(e)) return total = len(data_all) print( "-" * 50) print( " total count --> ", total) print( "-" * 50) percent_prev = 0 count = 0 bulk_data = [] total_count = 0 for item in data_all : count += 1 _id = item["_id"] if _id in jump_ids : continue jump_ids.append(_id) _source = item["_source"] percent = int((count / total) * 100) if percent != percent_prev: print("import_rules_of_json_files -> %.2f %%" % percent, total) percent_prev = percent del _source["tcodes"] _source["version_key"] = version_key bulk_data.append({"_id": _id, "_source": _source}) if len(bulk_data) > 100: total_count += len(bulk_data) print("=" * 10, " to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] if len(bulk_data) > 0: total_count += len(bulk_data) print("=" * 10, " to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] with open(file_jump_ids, "w") as f: f.write("\n".join(jump_ids)) print("end success import_to_elastic") 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_H101") # password = os.getenv("HOST_H101_PASS") # path_base_map = ( # r"G:/Project2/gateaway_py/elastic_backend/elastic_backend/app/schemas/mappings/" # ) elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic") text_normalize = TextNormalizer() # input_file = r"G:\Project2\102_gpu\v3_base\mj_qa_group_v3.json"; # jump_ids_file = r"G:\Project2\102_gpu\v3_base\proccess_group_v3.json"; # import_contexts_of_json_files(elastic, input_file, jump_ids_file ) input_file = r"G:\Project2\102_gpu\v3_base\mj_qa_rule_v3.json"; jump_ids_file = r"G:\Project2\102_gpu\v3_base\proccess_rule_v3.json"; import_rules_of_json_files(elastic, input_file, jump_ids_file ) ## ----------------------------------------------------------------------- # .\.env\Scripts\activate # python -m app.majles.import_context_rules_v3