From d35b3c5bb7777723355f006793b3a9f78fc484f5 Mon Sep 17 00:00:00 2001 From: fuwx Date: Thu, 6 Mar 2025 21:25:18 +0800 Subject: [PATCH] add tools --- .../apo_select/tools/container_disk_time.py | 45 ++++++++++++ .../apo_select/tools/container_disk_time.yaml | 70 +++++++++++++++++++ .../apo_select/tools/host_cpu_iowait.py | 43 ++++++++++++ .../apo_select/tools/host_cpu_iowait.yaml | 57 +++++++++++++++ .../apo_select/tools/host_fd_open.py | 43 ++++++++++++ .../apo_select/tools/host_fd_open.yaml | 57 +++++++++++++++ .../tools/polar_process_all_resp.py | 43 ++++++++++++ .../tools/polar_process_all_resp.yaml | 57 +++++++++++++++ 8 files changed, 415 insertions(+) create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.py create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.yaml create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.py create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.yaml create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.py create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.yaml create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.py create mode 100644 api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.yaml diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.py b/api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.py new file mode 100644 index 0000000000..290bc1bece --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.py @@ -0,0 +1,45 @@ +import json +from collections.abc import Generator +from typing import Any, Optional + +import requests + +from configs import dify_config +from core.tools.builtin_tool.tool import BuiltinTool +from core.tools.entities.tool_entities import ToolInvokeMessage +from libs.apo_utils import APOUtils + +class ContainerDiskTimeTool(BuiltinTool): + def _invoke( + self, + user_id: str, + tool_parameters: dict[str, Any], + conversation_id: Optional[str] = None, + app_id: Optional[str] = None, + message_id: Optional[str] = None, + ) -> Generator[ToolInvokeMessage, None, None]: + pod = tool_parameters.get("pod") + namespace = tool_parameters.get("namespace") + start_time = tool_parameters.get("startTime") + end_time = tool_parameters.get("endTime") + params = { + 'metricName': '基础设施情况 - 容器磁盘 - 读写花费时间 /s - Read', + 'params': { + "pod": pod, + "namespace": namespace + }, + 'startTime': start_time, + 'endTime': end_time, + 'step': APOUtils.get_step(start_time, end_time), + } + resp = requests.post(dify_config.APO_BACKEND_URL + '/api/metric/query', json=params) + list = resp.json()['result'] + list = json.dumps({ + 'type': 'metric', + 'display': True, + 'unit': list['unit'], + 'data': { + "timeseries": list['timeseries'] + } + }) + yield self.create_text_message(list) \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.yaml b/api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.yaml new file mode 100644 index 0000000000..a6935bf95a --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/container_disk_time.yaml @@ -0,0 +1,70 @@ +identity: + name: select_container_cpu + author: APO + label: + en_US: Query the CPU utilization of the container + zh_Hans: 基础设施情况 - 容器磁盘 - 读写花费时间 /s - Read + pt_BR: Query the CPU utilization of the container +display: + type: metric + title: 基础设施情况 - 容器磁盘 - 读写花费时间 /s - Read + unit: percent(0-100)百分比 +description: + human: + en_US: Query the CPU utilization of the container + zh_Hans: 基础设施情况 - 容器磁盘 - 读写花费时间 /s - Read + pt_BR: Query the CPU utilization of the container + llm: 基础设施情况 - 容器磁盘 - 读写花费时间 /s - Read +parameters: + - name: pod + type: string + required: true + label: + en_US: pod + zh_Hans: pod + pt_BR: pod + human_description: + en_US: Specified pod name + zh_Hans: 指定的容器POD名称 + pt_BR: Specified pod name + llm_description: Specified pod name + form: llm + - name: namespace + type: string + required: true + label: + en_US: namespace + zh_Hans: namespace + pt_BR: namespace + human_description: + en_US: Specified namespace + zh_Hans: 指定的容器所在Namespace + pt_BR: Specified namespace + llm_description: Specified namespace + form: llm + - name: startTime + type: number + required: true + label: + en_US: startTime + zh_Hans: startTime + pt_BR: startTime + human_description: + en_US: Data query start time + zh_Hans: 开始时间 (微秒) + pt_BR: Data query start time + llm_description: Data query start time + form: llm + - name: endTime + type: number + required: true + label: + en_US: endTime + zh_Hans: endTime + pt_BR: endTime + human_description: + en_US: Data query end time + zh_Hans: 结束时间 (微秒) + pt_BR: Data query end time + llm_description: Data query start time + form: llm \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.py b/api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.py new file mode 100644 index 0000000000..967ec44379 --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.py @@ -0,0 +1,43 @@ +import json +from collections.abc import Generator +from typing import Any, Optional + +import requests + +from configs import dify_config +from core.tools.builtin_tool.tool import BuiltinTool +from core.tools.entities.tool_entities import ToolInvokeMessage +from libs.apo_utils import APOUtils + +class HostCPUIoWaitRespTool(BuiltinTool): + def _invoke( + self, + user_id: str, + tool_parameters: dict[str, Any], + conversation_id: Optional[str] = None, + app_id: Optional[str] = None, + message_id: Optional[str] = None, + ) -> Generator[ToolInvokeMessage, None, None]: + node = tool_parameters.get("node") + start_time = tool_parameters.get("startTime") + end_time = tool_parameters.get("endTime") + params = { + 'metricName': '宿主机监控指标 - Basic CPU / Mem / Net / Disk - CPU Basic - Busy Iowait', + 'params': { + "node": node + }, + 'startTime': start_time, + 'endTime': end_time, + 'step': APOUtils.get_step(start_time, end_time), + } + resp = requests.post(dify_config.APO_BACKEND_URL + '/api/metric/query', json=params) + list = resp.json()['result'] + list = json.dumps({ + 'type': 'metric', + 'display': True, + 'unit': list['unit'], + 'data': { + "timeseries": list['timeseries'] + } + }) + yield self.create_text_message(list) \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.yaml b/api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.yaml new file mode 100644 index 0000000000..890de8e40b --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/host_cpu_iowait.yaml @@ -0,0 +1,57 @@ +identity: + name: host_cpu_iowait + author: APO + label: + en_US: Query the CPU utilization of the host + zh_Hans: 宿主机监控指标 - Basic CPU / Mem / Net / Disk - CPU Basic - Busy Iowait + pt_BR: Query the CPU utilization of the host +description: + human: + en_US: Query the CPU utilization of the host + zh_Hans: 宿主机监控指标 - Basic CPU / Mem / Net / Disk - CPU Basic - Busy Iowait + pt_BR: Query the CPU utilization of the host + llm: 宿主机监控指标 - Basic CPU / Mem / Net / Disk - CPU Basic - Busy Iowait +display: + type: metric + title: 宿主机监控指标 - Basic CPU / Mem / Net / Disk - CPU Basic - Busy Iowait + unit: percent(0-100)百分比 +parameters: + - name: node + type: string + required: true + label: + en_US: node + zh_Hans: node + pt_BR: node + human_description: + en_US: Specified host name + zh_Hans: 指定的主机名 + pt_BR: Specified host name + llm_description: Specified host name + form: llm + - name: startTime + type: number + required: true + label: + en_US: startTime + zh_Hans: startTime + pt_BR: startTime + human_description: + en_US: Data query start time + zh_Hans: 开始时间 (微秒) + pt_BR: Data query start time + llm_description: Data query start time + form: llm + - name: endTime + type: number + required: true + label: + en_US: endTime + zh_Hans: endTime + pt_BR: endTime + human_description: + en_US: Data query end time + zh_Hans: 结束时间 (微秒) + pt_BR: Data query end time + llm_description: Data query start time + form: llm \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.py b/api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.py new file mode 100644 index 0000000000..3b31279e8e --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.py @@ -0,0 +1,43 @@ +import json +from collections.abc import Generator +from typing import Any, Optional + +import requests + +from configs import dify_config +from core.tools.builtin_tool.tool import BuiltinTool +from core.tools.entities.tool_entities import ToolInvokeMessage +from libs.apo_utils import APOUtils + +class HostCPUIoWaitRespTool(BuiltinTool): + def _invoke( + self, + user_id: str, + tool_parameters: dict[str, Any], + conversation_id: Optional[str] = None, + app_id: Optional[str] = None, + message_id: Optional[str] = None, + ) -> Generator[ToolInvokeMessage, None, None]: + node = tool_parameters.get("node") + start_time = tool_parameters.get("startTime") + end_time = tool_parameters.get("endTime") + params = { + 'metricName': '宿主机监控指标 - System Misc - File Descriptors - Open file descriptors', + 'params': { + "node": node + }, + 'startTime': start_time, + 'endTime': end_time, + 'step': APOUtils.get_step(start_time, end_time), + } + resp = requests.post(dify_config.APO_BACKEND_URL + '/api/metric/query', json=params) + list = resp.json()['result'] + list = json.dumps({ + 'type': 'metric', + 'display': True, + 'unit': list['unit'], + 'data': { + "timeseries": list['timeseries'] + } + }) + yield self.create_text_message(list) \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.yaml b/api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.yaml new file mode 100644 index 0000000000..b3d65545bd --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/host_fd_open.yaml @@ -0,0 +1,57 @@ +identity: + name: host_cpu_iowait + author: APO + label: + en_US: Query the CPU utilization of the host + zh_Hans: 宿主机监控指标 - System Misc - File Descriptors - Open file descriptors + pt_BR: Query the CPU utilization of the host +description: + human: + en_US: Query the CPU utilization of the host + zh_Hans: 宿主机当前打开文件描述符数 + pt_BR: Query the CPU utilization of the host + llm: 宿主机监控指标 - System Misc - File Descriptors - Open file descriptors +display: + type: metric + title: 宿主机监控指标 - System Misc - File Descriptors - Open file descriptors + unit: percent(0-100)百分比 +parameters: + - name: node + type: string + required: true + label: + en_US: node + zh_Hans: node + pt_BR: node + human_description: + en_US: Specified host name + zh_Hans: 指定的主机名 + pt_BR: Specified host name + llm_description: Specified host name + form: llm + - name: startTime + type: number + required: true + label: + en_US: startTime + zh_Hans: startTime + pt_BR: startTime + human_description: + en_US: Data query start time + zh_Hans: 开始时间 (微秒) + pt_BR: Data query start time + llm_description: Data query start time + form: llm + - name: endTime + type: number + required: true + label: + en_US: endTime + zh_Hans: endTime + pt_BR: endTime + human_description: + en_US: Data query end time + zh_Hans: 结束时间 (微秒) + pt_BR: Data query end time + llm_description: Data query start time + form: llm \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.py b/api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.py new file mode 100644 index 0000000000..eb5e3b2b99 --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.py @@ -0,0 +1,43 @@ +import json +from collections.abc import Generator +from typing import Any, Optional + +import requests + +from configs import dify_config +from core.tools.builtin_tool.tool import BuiltinTool +from core.tools.entities.tool_entities import ToolInvokeMessage +from libs.apo_utils import APOUtils + +class PolarProcessAllRespTool(BuiltinTool): + def _invoke( + self, + user_id: str, + tool_parameters: dict[str, Any], + conversation_id: Optional[str] = None, + app_id: Optional[str] = None, + message_id: Optional[str] = None, + ) -> Generator[ToolInvokeMessage, None, None]: + pod = tool_parameters.get("pod") + start_time = tool_parameters.get("startTime") + end_time = tool_parameters.get("endTime") + params = { + 'metricName': 'Thread Polaris Metrics - 北极星指标(进程) - 各类型耗时折线图 - 所有类型列表', + 'params': { + "pod": pod + }, + 'startTime': start_time, + 'endTime': end_time, + 'step': APOUtils.get_step(start_time, end_time), + } + resp = requests.post(dify_config.APO_BACKEND_URL + '/api/metric/query', json=params) + list = resp.json()['result'] + list = json.dumps({ + 'type': 'metric', + 'display': True, + 'unit': list['unit'], + 'data': { + "timeseries": list['timeseries'] + } + }) + yield self.create_text_message(list) \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.yaml b/api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.yaml new file mode 100644 index 0000000000..5893ebb36a --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/polar_process_all_resp.yaml @@ -0,0 +1,57 @@ +identity: + name: polar_process_all_resp + author: APO + label: + en_US: Query the CPU utilization of the host + zh_Hans: Thread Polaris Metrics - 北极星指标(进程) - 各类型耗时折线图 - 所有类型列表 + pt_BR: Query the CPU utilization of the host +description: + human: + en_US: Query the CPU utilization of the host + zh_Hans: Thread Polaris Metrics - 北极星指标(进程) - 各类型耗时折线图 - 所有类型列表 + pt_BR: Query the CPU utilization of the host + llm: Thread Polaris Metrics - 北极星指标(进程) - 各类型耗时折线图 - 所有类型列表 +display: + type: metric + title: Thread Polaris Metrics - 北极星指标(进程) - 各类型耗时折线图 - 所有类型列表 + unit: ns +parameters: + - name: pod + type: string + required: true + label: + en_US: pod + zh_Hans: pod + pt_BR: pod + human_description: + en_US: Specified pod name + zh_Hans: 指定的容器POD名称 + pt_BR: Specified pod name + llm_description: Specified pod name + form: llm + - name: startTime + type: number + required: true + label: + en_US: startTime + zh_Hans: startTime + pt_BR: startTime + human_description: + en_US: Data query start time + zh_Hans: 开始时间 (微秒) + pt_BR: Data query start time + llm_description: Data query start time + form: llm + - name: endTime + type: number + required: true + label: + en_US: endTime + zh_Hans: endTime + pt_BR: endTime + human_description: + en_US: Data query end time + zh_Hans: 结束时间 (微秒) + pt_BR: Data query end time + llm_description: Data query start time + form: llm \ No newline at end of file