From a0c00f4b772c24a0c81ee5934987524cbf7033c5 Mon Sep 17 00:00:00 2001 From: init_mahdi Date: Mon, 2 Feb 2026 19:26:02 +0330 Subject: [PATCH] rules_labels --- app/import_context_rules.py | 144 +++++++++++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 18 deletions(-) diff --git a/app/import_context_rules.py b/app/import_context_rules.py index c7e9494..7f5a2a0 100644 --- a/app/import_context_rules.py +++ b/app/import_context_rules.py @@ -1,4 +1,4 @@ -import mariadb +# import mariadb from dotenv import load_dotenv import os import datetime @@ -9,6 +9,7 @@ import zipfile import sys import time from tqdm import tqdm +from pydantic import BaseModel sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) @@ -16,12 +17,31 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from app.common.elastic_helper import ElasticHelper from app.common.general_functions import * from app.common.text_normalizer import TextNormalizer +from app.common.data_helper import DataHelper from app.majles_options import * -load_dotenv(dotenv_path="config.env") +class OutputLabels(BaseModel): + class Subject(BaseModel): + subject_type: str + subject_detail: str + + class Clause(BaseModel): + exists: bool + text: str + + subject: Subject + action: str + object: str + condition: Clause + rule_verdict: str + scope: Clause | None + exception: Clause | None + + +load_dotenv(dotenv_path=r"D:\init_mahdi\dataParsers\config.env") host = os.getenv("HOST_160") password = os.getenv("HOST_160_PASS") @@ -33,16 +53,80 @@ password = os.getenv("HOST_160_PASS") # print(password) -path_base_map = ( - "G:\Project2\gateaway_py\elastic_backend/elastic_backend/app/schemas/mappings/" -) -elastic = ElasticHelper(host, password, "elastic", path_base_map) +# path_base_map = ( +# r"G:/Project2/gateaway_py/elastic_backend/elastic_backend/app/schemas/mappings/" +# ) +elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic") text_normalize = TextNormalizer() log_file = None +def convert_OutputLabels_to_es_schema(item: OutputLabels): + return { + "action": item.action, + "condition": item.condition.text, + "exception": item.exception.text if item.exception.text else None, + "object": item.object, + "rule_verdict": item.rule_verdict, + "scope": item.scope.text if item.scope.text else None, + "subject_detail": item.subject.subject_detail, + "subject_type": item.subject.subject_type, + } + + +def extract_json_context_id_rules_labels( + json_path, + elastic, + index_name="mj_qa_rule", +): + input_data = DataHelper.load_orjson(json_path) + print( + f'input_data {len(input_data)}' + ) + bulk_data = [] + total_count = 0 + failed = 0 + + for _id1, (context_id, rule_label_data) in enumerate(input_data.items(), start=1): + print(f"context_id {context_id}") + for _id2, item in enumerate(rule_label_data, start=1): + rule_id = context_id + "-" + str(_id2) + try: + old_data = elastic.get_document(index_name, rule_id) + old_data = old_data["_source"] + item_obj = OutputLabels.model_validate(item) + new_item = convert_OutputLabels_to_es_schema(item_obj) + old_data["label_object"] = new_item + except: + failed += 1 + continue + + bulk_data.append( + {"_id": old_data["id"], "_source": old_data} + ) + # qs648706-1 + + if len(bulk_data) >= 100: + total_count += len(bulk_data) + print( + "=" * 10, "to_elastic , total=" + str(total_count) + ) + elastic.updateBulkList(bulk_data, index_name) + bulk_data = [] + + if len(bulk_data) >= 0: + total_count += len(bulk_data) + print( + "=" * 10, "to_elastic , total=" + str(total_count) + ) + elastic.updateBulkList(bulk_data, index_name) + bulk_data = [] + # qs648706-1 + print(f"failed -=>{failed}<=-") + + def extract_zip_files(elastic, filezip, file_import_ids, file_type="groups"): bulk_data = [] @@ -83,7 +167,14 @@ def extract_zip_files(elastic, filezip, file_import_ids, file_type="groups"): if qanon_id in jump_ids: continue - print(f"***** {file_type} : ", index_name_o, "=>", filename, " qanon_id=" , qanon_id) + print( + f"***** {file_type} : ", + index_name_o, + "=>", + filename, + " qanon_id=", + qanon_id, + ) with zObject.open(filename) as file: data = json.loads(file.read()) @@ -144,8 +235,8 @@ def extract_zip_files(elastic, filezip, file_import_ids, file_type="groups"): print("=" * 10, " to_elastic , total=" + str(total_count)) if file_type == "rules" or file_type == "groups": elastic.importBulkList(bulk_data, index_name_o) - else : - elastic.updateBulkList(bulk_data, index_name_o) + else: + elastic.updateBulkList(bulk_data, index_name_o) bulk_data = [] with open(file_import_ids, "w") as f: @@ -331,7 +422,7 @@ def import_terms_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_name_o): - + index_name_i = "mj_qa_context" for key, value in data.items(): @@ -350,7 +441,12 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na res = elastic.get_document(index_name_i, context_id) source = res["_source"] except: - error = " section_mom_id not found in qa_context :" + qanon_id + ":" + context_id + error = ( + " section_mom_id not found in qa_context :" + + qanon_id + + ":" + + context_id + ) if log_file: log_file.write(5 * "*" + error + "\n") continue @@ -389,16 +485,18 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na "ts_date": ts_date, "ts_ref": ts_ref, "rule_order": rule_order, - "rule_type": rule_type, + "rule_type": rule_type, "content": content, "group_section_ids": group_section_ids, "time_create": int(round(time.time() * 1000)), } bulk_data.append({"_id": eid, "_source": data}) - + if len(bulk_data) >= 100: total_count += len(bulk_data) - print("=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid) + print( + "=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid + ) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] @@ -442,16 +540,26 @@ if __name__ == "__main__": # file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt" # extract_zip_files(elastic, filezip, file_import_ids, data_key) # log_file.close() - - + + # ### ---------------------------------------------- + data_key = "rules_labels" + # label_object + index_name = "mj_qa_rule" + json_path = r"D:\init_mahdi\dataParsers\merge_labels.json" + extract_json_context_id_rules_labels( + elastic=elastic, index_name=index_name, json_path=json_path + ) + # ### ---------------------------------------------- # elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_context", "mj_qa_context") # elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_rule", "mj_qa_rule") # mapping_file_path = "G:/\Project2/_back/_to_majles/14041022/src/repo/_other/majles/mj_qa_context.json" # elastic.restorFileToElastic2('G:/Project2/102_gpu/mj_qa_context.zip', 'mj_qa_context', 'majles', False, 'mj_qa_context', mapping_file_path) - + # mapping_file_path = "G:/\Project2/_back/_to_majles/14041022/src/repo/_other/majles/mj_qa_rule.json" # elastic.restorFileToElastic2('G:/Project2/102_gpu/mj_qa_rule.zip', 'mj_qa_rule', 'majles', False, 'mj_qa_rule', mapping_file_path) - + # ### ---------------------------------------------- +# python -m dataParsers.app.import_context_rules +print(":D")