plan_import

This commit is contained in:
init_mahdi
2025-11-08 09:43:05 +03:30
parent 98c4449328
commit fb5e13d9f7
+87 -54
View File
@@ -1,5 +1,6 @@
import os import os
import re import re
import uuid
import traceback import traceback
import json import json
import time import time
@@ -15,37 +16,53 @@ from app.common.qanon_functions import *
from app.common.elastic_helper import ElasticHelper from app.common.elastic_helper import ElasticHelper
from app.common.config_base import * from app.common.config_base import *
def recersive_sections(tcode_label, data, list_bulk, parent_id, child_order , parnet_path=""): def recersive_sections(
sections = data["subsections"] section,
for item in sections: list_bulk,
child_sections = [] parent_id,
title = item["title"] parent_path,
if "subsections" in item : tcode_label,
c_sections = item["subsections"] child_order,
child_order += 1 ):
parent_path += item["title"] + '/' """
Recursive helper to process one section (and its subsections).
id = uuid.uuid4().hex[:8] """
json_ = { title = section.get("title", "").strip()
"id" : id, if not title:
"title" : title, return
"version_key" : "D49_oss120",
"fill_path" : parent_path, # Generate node ID
"parent_id": parent_id, node_id = uuid.uuid4().hex[:8]
"child_order": child_order, full_path = parent_path + title + "/"
"tcode": tcode_label,
"time_create": time.time() # Append current section
} list_bulk.append({
"id": node_id,
list_bulk.append(json_) "title": title,
"version_key": "D49_oss120",
c_order = 0 "full_path": full_path,
recersive_sections(c_sections, list_bulk, parent_id, c_order, parent_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): def import_paln_json_to_elastic(elastic, file_path, index_name):
try: try:
# Read JSON file # Read JSON file
with open(file_path, 'r', encoding="utf8") as f: with open(file_path, 'r', encoding="utf8") as f:
data = json.load(f) data = json.load(f)
@@ -54,52 +71,61 @@ def import_paln_json_to_elastic(elastic, file_path, index_name):
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError(f"File not found: {file_path}") raise FileNotFoundError(f"File not found: {file_path}")
total = len(data)
list_bulk = [] list_bulk = []
batch_size = 200 batch_size = 200
processed_count = 0 processed_count = 0
total_update = 0 total_update = 0
tcode_label = "" tcode_label = data.get("law_title")
if "law_title" in data :
tcode_label = data["tcode_label"]
if not tcode_label : if not tcode_label :
print(" error tcode_label", file_path) print(" error tcode_label", file_path)
return 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 : if len(list_bulk) >= batch_size :
elastic.updateBulkList(list_bulk, index_name_o) elastic.updateBulkList(list_bulk, index_name)
total_update += len(list_bulk) total_update += len(list_bulk)
list_bulk = [] list_bulk = []
print("updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) print("updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path)
batch_size_number += 1 batch_size_number += 1
if list_bulk: if list_bulk:
elastic.updateBulkList(list_bulk, index_name_o) elastic.updateBulkList(list_bulk, index_name)
total_update += len(list_bulk) total_update += len(list_bulk)
print(" .... finish updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) print(" .... finish updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path)
batch_size_number += 1 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 # -------------------------------------------------------------------- Run
@@ -121,6 +147,13 @@ if __name__ == "__main__":
path_base_map = "G:/Project2/gateway2_main/repo/_other/" path_base_map = "G:/Project2/gateway2_main/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map) 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 = [] # all_codes = []
# with open("./data/codes49.json", "r", encoding="utf8") as f: # with open("./data/codes49.json", "r", encoding="utf8") as f: