plan_import
This commit is contained in:
+74
-41
@@ -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,32 +16,48 @@ 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).
|
||||||
|
"""
|
||||||
|
title = section.get("title", "").strip()
|
||||||
|
if not title:
|
||||||
|
return
|
||||||
|
|
||||||
id = uuid.uuid4().hex[:8]
|
# Generate node ID
|
||||||
json_ = {
|
node_id = uuid.uuid4().hex[:8]
|
||||||
"id" : id,
|
full_path = parent_path + title + "/"
|
||||||
|
|
||||||
|
# Append current section
|
||||||
|
list_bulk.append({
|
||||||
|
"id": node_id,
|
||||||
"title": title,
|
"title": title,
|
||||||
"version_key": "D49_oss120",
|
"version_key": "D49_oss120",
|
||||||
"fill_path" : parent_path,
|
"full_path": full_path,
|
||||||
"parent_id": parent_id,
|
"parent_id": parent_id,
|
||||||
"child_order": child_order,
|
"child_order": child_order,
|
||||||
"tcode": tcode_label,
|
"tcode": tcode_label,
|
||||||
"time_create": time.time()
|
"time_create": time.time()
|
||||||
}
|
})
|
||||||
|
|
||||||
list_bulk.append(json_)
|
# Process subsections (children)
|
||||||
|
subsections = section.get("subsections", [])
|
||||||
c_order = 0
|
for idx, subsec in enumerate(subsections, start=1):
|
||||||
recersive_sections(c_sections, list_bulk, parent_id, c_order, parent_path)
|
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):
|
||||||
@@ -54,51 +71,60 @@ 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()
|
||||||
|
})
|
||||||
|
|
||||||
parent_id = 0
|
# Step 2: Recursively process top-level sections
|
||||||
child_order = 0
|
for idx, section in enumerate(data.get("sections", []), start=1):
|
||||||
# tree_order = 0
|
recersive_sections(
|
||||||
sections = data["sections"]
|
section=section,
|
||||||
parent_path = ""
|
list_bulk=list_bulk,
|
||||||
for item in sections:
|
parent_id=root_id,
|
||||||
child_sections = []
|
parent_path=root_path,
|
||||||
if "subsections" in item :
|
child_order=idx,
|
||||||
c_sections = item["subsections"]
|
tcode_label=tcode_label
|
||||||
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')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user