From e460b8d73db0b1ba6792a8176b8fbc62e2a7439d Mon Sep 17 00:00:00 2001 From: royzhao Date: Mon, 28 Oct 2024 11:20:25 +0800 Subject: [PATCH] fix(knext): reasoner command add default cfg config (#377) --- .../knext/command/sub_command/reasoner.py | 27 ++++++++++++++----- python/knext/knext/reasoner/client.py | 21 ++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/python/knext/knext/command/sub_command/reasoner.py b/python/knext/knext/command/sub_command/reasoner.py index 881c56b0..5d3525ad 100644 --- a/python/knext/knext/command/sub_command/reasoner.py +++ b/python/knext/knext/command/sub_command/reasoner.py @@ -11,7 +11,10 @@ # or implied. import os import sys +from pathlib import Path + import click +from knext.common.env import init_kag_config from knext.reasoner.client import ReasonerClient @@ -19,17 +22,29 @@ from knext.reasoner.client import ReasonerClient @click.option("--file", help="Path of DSL file.") @click.option("--dsl", help="DSL string enclosed in double quotes.") @click.option("--output", help="Output file.") -def execute_reasoner_job(file, dsl, output=None): +@click.option("--proj_path", help="Path of config.", default="./") +def execute_reasoner_job(file, dsl, output=None, proj_path="./"): """ Submit asynchronous reasoner jobs to server by providing DSL file or string. """ - with_server = eval(os.getenv("KAG_PROJECT_WITH_SERVER", "False")) - if not with_server: + config_path = os.path.join(proj_path, "kag_config.cfg") + if not Path(config_path).exists(): + # find *.cfg file + cfg_files = list(Path(proj_path).glob("*.cfg")) + if len(cfg_files) == 0: + click.secho("ERROR: No .cfg file found.", fg="bright_red") + sys.exit() + config_path = cfg_files[0] + init_kag_config(config_path) + host_addr = os.getenv("KAG_PROJECT_ADDR", "http://127.0.0.1:8887") + project_id = os.getenv("KAG_PROJECT_ID") + if not project_id: click.secho( - "ERROR: Reasoner must be executed with SPG Server.", fg="bright_red" + "ERROR: Reasoner must be executed with SPG Server. Need assign proj_path", + fg="bright_red", ) sys.exit() - client = ReasonerClient() + client = ReasonerClient(host_addr=host_addr, project_id=int(project_id)) if file and not dsl: with open(file, "r") as f: dsl_content = f.read() @@ -38,4 +53,4 @@ def execute_reasoner_job(file, dsl, output=None): else: click.secho("ERROR: Please choose either --file or --dsl.", fg="bright_red") sys.exit() - client.execute(dsl_content, output) + client.execute(dsl_content, output_file=output) diff --git a/python/knext/knext/reasoner/client.py b/python/knext/knext/reasoner/client.py index a9cbbf23..059e76c0 100644 --- a/python/knext/knext/reasoner/client.py +++ b/python/knext/knext/reasoner/client.py @@ -10,11 +10,12 @@ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. import os +import datetime +from knext.reasoner.rest.models.reason_task_response import ReasonTaskResponse import knext.common.cache from knext.common.base.client import Client from knext.common.rest import ApiClient, Configuration -from knext.project.client import ProjectClient from knext.reasoner import ReasonTask from knext.reasoner import rest from knext.reasoner.rest import SpgTypeQueryRequest @@ -100,8 +101,22 @@ class ReasonerClient(Client): """ Execute a synchronous builder job in local runner. """ - task = self.syn_execute(dsl_content) - print(task) + task_response: ReasonTaskResponse = self.syn_execute(dsl_content) + task: ReasonTask = task_response.task + if task.status != "FINISH": + print(f"RUN {task.status} {dsl_content}") + else: + default_output_file = output_file or ( + f"./{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.csv" + ) + show_data = [ + task.result_table_result.header + ] + task.result_table_result.rows + import pandas as pd + + df = pd.DataFrame(show_data) + print(df) + df.to_csv(default_output_file, index=False) def query_node(self, label, id_value): req = SpgTypeQueryRequest(