From beb23e3febee61aceaf589775373750796801303 Mon Sep 17 00:00:00 2001 From: hsafaei Date: Sat, 8 Nov 2025 09:08:25 +0330 Subject: [PATCH] plans_ import --- app/for_ai/codes_import_from_excel.py | 12 ++- app/for_ai/majles_codes_update_steps.py | 7 +- app/for_ai/plans_import.py | 129 ++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 app/for_ai/plans_import.py diff --git a/app/for_ai/codes_import_from_excel.py b/app/for_ai/codes_import_from_excel.py index 830dec3..6d43ca6 100644 --- a/app/for_ai/codes_import_from_excel.py +++ b/app/for_ai/codes_import_from_excel.py @@ -595,13 +595,17 @@ def extract_codes_to_json( file_excel, tcode_label, file_json): # ذخیره کد در الاستیک برای شناسه های یافت شده if section_id and section_id != 0: - tcode_mains = [] - tcode_subs = [] + _source = {} + if code_state : + _source["tcode_mains"] = [tcode_label] + else : + _source["tcode_subs"] = [tcode_label] + + data={ 'id':section_id, - 'label': tcode_label, - 'is_main': int(code_state), + '_source' : _source, } bulk_data.append(data) diff --git a/app/for_ai/majles_codes_update_steps.py b/app/for_ai/majles_codes_update_steps.py index f739437..3904e1d 100644 --- a/app/for_ai/majles_codes_update_steps.py +++ b/app/for_ai/majles_codes_update_steps.py @@ -187,6 +187,7 @@ with open("./data/codes49.json", "r", encoding="utf8") as f: #------------------------------------- #------ استخراج برخی کدهای باقیمانده که اکسل آنها از قبل موجود هست و تولید فایل مناسب برای درج فایل محور در بانک مجلس - - -run_codes_extract_directory( "./data/tcodes/corrected_majles/not_edit", "./data/tcodes/corrected_majles/not_edit/result_json", all_codes) +#--- +#run_codes_extract_directory( "./data/tcodes/corrected_majles/not_edit", "./data/tcodes/corrected_majles/not_edit/result_json", all_codes) +#elastic.backupIndexToZipfile("G:/Project2/113", "mj_qa_section", "mj_qa_section_6_codes_no_edit_1") +#------------------------------------- diff --git a/app/for_ai/plans_import.py b/app/for_ai/plans_import.py new file mode 100644 index 0000000..f7d9a05 --- /dev/null +++ b/app/for_ai/plans_import.py @@ -0,0 +1,129 @@ +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 +