From fb5e13d9f7d57781437fcc30d357743fc62324e2 Mon Sep 17 00:00:00 2001 From: init_mahdi Date: Sat, 8 Nov 2025 09:43:05 +0330 Subject: [PATCH] plan_import --- app/for_ai/plans_import.py | 141 +++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 54 deletions(-) diff --git a/app/for_ai/plans_import.py b/app/for_ai/plans_import.py index f7d9a05..d0df048 100644 --- a/app/for_ai/plans_import.py +++ b/app/for_ai/plans_import.py @@ -1,5 +1,6 @@ import os import re +import uuid import traceback import json import time @@ -15,37 +16,53 @@ 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 recersive_sections( + section, + list_bulk, + parent_id, + parent_path, + tcode_label, + child_order, + ): + """ + Recursive helper to process one section (and its subsections). + """ + title = section.get("title", "").strip() + if not title: + return + + # Generate node ID + node_id = uuid.uuid4().hex[:8] + full_path = parent_path + title + "/" + + # Append current section + list_bulk.append({ + "id": node_id, + "title": title, + "version_key": "D49_oss120", + "full_path": full_path, + "parent_id": parent_id, + "child_order": child_order, + "tcode": tcode_label, + "time_create": time.time() + }) + + # Process subsections (children) + subsections = section.get("subsections", []) + for idx, subsec in enumerate(subsections, start=1): + recersive_sections( + section=subsec, + list_bulk=list_bulk, + parent_id=node_id, # ✅ important: parent is current node + parent_path=full_path, # ✅ path extended + child_order=idx, # ✅ order among child_order + tcode_label=tcode_label + ) def import_paln_json_to_elastic(elastic, file_path, index_name): - try: + try: # Read JSON file with open(file_path, 'r', encoding="utf8") as f: data = json.load(f) @@ -54,52 +71,61 @@ def import_paln_json_to_elastic(elastic, file_path, index_name): except FileNotFoundError: raise FileNotFoundError(f"File not found: {file_path}") - - + total = len(data) list_bulk = [] batch_size = 200 processed_count = 0 total_update = 0 - - tcode_label = "" - if "law_title" in data : - tcode_label = data["tcode_label"] - + + tcode_label = data.get("law_title") if not tcode_label : print(" error tcode_label", file_path) return + + # Step 1: Add root node (the law itself) + root_id = uuid.uuid4().hex[:8] + root_path = data.get("law_title") + "/" + list_bulk.append({ + "id": root_id, + "title": data.get("law_title"), + "version_key": "D49_oss120", + "full_path": root_path, + "parent_id": 0, # or 0 if you prefer integer root parent + "child_order": 0, + "tcode": tcode_label, + "time_create": time.time() + }) + + # Step 2: Recursively process top-level sections + for idx, section in enumerate(data.get("sections", []), start=1): + recersive_sections( + section=section, + list_bulk=list_bulk, + parent_id=root_id, + parent_path=root_path, + child_order=idx, + tcode_label=tcode_label + ) - - 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) + elastic.updateBulkList(list_bulk, index_name) 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) + elastic.updateBulkList(list_bulk, index_name) total_update += len(list_bulk) print(" .... finish updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) batch_size_number += 1 + + ########################## save manual for test + # file_path = r'D:\\init_mahdi\project\dataParsers\data\plan_test\test1.json' + # with open(file_path, 'w', encoding='utf-8') as f: + # json.dump(list_bulk, f, ensure_ascii=False, indent=4) + # print(':D') - - - - # -------------------------------------------------------------------- Run @@ -121,6 +147,13 @@ if __name__ == "__main__": path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) + file_path=r'D:\init_mahdi\project\dataParsers\data\plan_آمار_برنامه_و_بودجه.json' + index_name='mj' + import_paln_json_to_elastic( + elastic=elastic, + file_path=file_path, + index_name=index_name + ) # all_codes = [] # with open("./data/codes49.json", "r", encoding="utf8") as f: