import os import re import traceback import json import time import pandas as pd from dotenv import load_dotenv import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) # from app.common.general_functions import * from app.common.qanon_functions import * from app.common.elastic_helper import ElasticHelper from app.common.config_base import * def recersive_sections(tcode_label, data, list_bulk, parent_id, child_order , parnet_path=""): sections = data["subsections"] for item in sections: child_sections = [] title = item["title"] if "subsections" in item : c_sections = item["subsections"] child_order += 1 parent_path += item["title"] + '/' id = uuid.uuid4().hex[:8] json_ = { "id" : id, "title" : title, "version_key" : "D49_oss120", "fill_path" : parent_path, "parent_id": parent_id, "child_order": child_order, "tcode": tcode_label, "time_create": time.time() } list_bulk.append(json_) c_order = 0 recersive_sections(c_sections, list_bulk, parent_id, c_order, parent_path) def import_paln_json_to_elastic(elastic, file_path, index_name): try: # Read JSON file with open(file_path, 'r', encoding="utf8") as f: data = json.load(f) except json.JSONDecodeError as e: raise ValueError(f"Failed to parse JSON file: {str(e)}") except FileNotFoundError: raise FileNotFoundError(f"File not found: {file_path}") list_bulk = [] batch_size = 200 processed_count = 0 total_update = 0 tcode_label = "" if "law_title" in data : tcode_label = data["tcode_label"] if not tcode_label : print(" error tcode_label", file_path) return parent_id = 0 child_order = 0 # tree_order = 0 sections = data["sections"] parent_path = "" for item in sections: child_sections = [] if "subsections" in item : c_sections = item["subsections"] c_order = 0 parent_path += item["title"] + '/' recersive_sections(tcode_label, c_sections, list_bulk, parent_id, c_order, parent_path) if len(list_bulk) >= batch_size : elastic.updateBulkList(list_bulk, index_name_o) total_update += len(list_bulk) list_bulk = [] print("updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) batch_size_number += 1 if list_bulk: elastic.updateBulkList(list_bulk, index_name_o) total_update += len(list_bulk) print(" .... finish updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) batch_size_number += 1 # -------------------------------------------------------------------- Run if __name__ == "__main__": 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_113') # password=os.getenv('HOST_113_PASS') print('.... codes_import_from_excel ......', host) path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) # all_codes = [] # with open("./data/codes49.json", "r", encoding="utf8") as f: # codes_1 = json.load(f) # all_codes = codes_1