plan majles

This commit is contained in:
hsafaei
2026-05-09 16:04:08 +03:30
parent cb90e83ba6
commit 488eb98fec
9 changed files with 1570 additions and 824 deletions
-214
View File
@@ -1,214 +0,0 @@
import os
import re
import uuid
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(
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,
"_source":{
"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": int(round(time.time() * 1000))
}
})
# 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(all_codes, 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}")
print("-"*10, " ... import_paln_json_to_elastic ....", file_path)
total = len(list(data))
list_bulk = []
batch_size = 20
processed_count = 0
total_update = 0
batch_size_number = 1
file_name = os.path.basename(file_path)
_name = os.path.splitext(file_name)[0]
code_name = _name
tcode_label = data.get("law_title")
item_found = [item for item in all_codes if item.get("title") == tcode_label]
if not item_found :
item_found = [item for item in all_codes if item.get("title") == code_name]
if item_found :
tcode_label = code_name
else :
print("################### error tcode_label , code_name ....", tcode_label, code_name, file_path)
return
# Step 1: Add root node (the law itself)
root_id = uuid.uuid4().hex[:8]
root_path = tcode_label + "/"
# list_bulk.append({
# "_id": root_id,
# "_source": {
# "id": root_id,
# "title": tcode_label,
# "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": int(round(time.time() * 1000))
# }
# })
# Step 2: Recursively process top-level sections
for idx, section in enumerate(data.get("sections", []), start=1):
# print("..start..", idx, section["title"])
recersive_sections(
section=section,
list_bulk=list_bulk,
parent_id=0,
parent_path=root_path,
child_order=idx,
tcode_label=tcode_label
)
if len(list_bulk) >= batch_size :
elastic.importBulkList(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.importBulkList(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')
def list_json_files(directory):
import os
json_files = []
# مرور در دایرکتوری
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.json'):
# افزودن مسیر کامل فایل به لیست
json_files.append(os.path.join(root, file))
return json_files
# -------------------------------------------------------------------- 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/gateway_php/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
index_name='mj_plan'
# file_path=r'data/plans/gpt120b_49_1/data_1/آمار، برنامه و بودجه.json'
file_path=r'data/plans/gpt120b_49_1/data_1/'
for file in list_json_files(file_path):
import_paln_json_to_elastic(
all_codes=all_codes,
elastic=elastic,
file_path=file,
index_name=index_name
)
file_path=r'data/plans/gpt120b_49_1/data_2/'
for file in list_json_files(file_path):
import_paln_json_to_elastic(
all_codes=all_codes,
elastic=elastic,
file_path=file,
index_name=index_name
)
# all_codes = []
# with open("./data/codes49.json", "r", encoding="utf8") as f:
# codes_1 = json.load(f)
# all_codes = codes_1