diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/thread_polaris_p90.py b/api/core/tools/builtin_tool/providers/apo_select/tools/thread_polaris_p90.py new file mode 100644 index 0000000000..7178de324b --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/thread_polaris_p90.py @@ -0,0 +1,66 @@ +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 ThreadPolarisP90(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") + type = tool_parameters.get('type') + metrics = self.get_metrics(type, pod, start_time, end_time) + res = self.get_max_min(metrics) + list = json.dumps({ + 'type': 'p90', + 'data': res + }) + yield self.create_text_message(list) + + def get_max_min(self, data_json) -> str: + results = data_json["data"]["result"] + + stats = {} + + for entry in results: + tid = entry["metric"]["tid"] + values = [float(value[1]) for value in entry["values"]] + + stats[tid] = { + "max": max(values), + "min": min(values) + } + + return json.dumps(stats) + + def get_metrics(self, type: str, pod: str, start: int, end: int) -> dict: + pre_query = '''histogram_quantile(0.9, sum(rate(originx_thread_polaris_nanoseconds_bucket{pod="''' + query = pre_query + pod + '''", type="''' + type + '''"}[1m])) by (tid, vmrange))''' + step = '10m' + hour = 3600 * 1000 + + res = requests.get( + dify_config.APO_VM_URL + "/prometheus/api/v1/query_range", + params={ + 'query': query, + 'start': end / 1000 - hour, + 'end': end / 1000, + 'step': step + } + ) + + return res.json() \ No newline at end of file diff --git a/api/core/tools/builtin_tool/providers/apo_select/tools/thread_polaris_p90.yaml b/api/core/tools/builtin_tool/providers/apo_select/tools/thread_polaris_p90.yaml new file mode 100644 index 0000000000..9a178e4135 --- /dev/null +++ b/api/core/tools/builtin_tool/providers/apo_select/tools/thread_polaris_p90.yaml @@ -0,0 +1,60 @@ +identity: + name: 线程北极星P90数据 + author: APO + label: + en_US: Thread Polaris Metrics P90 + zh_Hans: 线程北极星P90数据 +description: + human: + en_US: + zh_Hans: 线程北极星P90数据 + llm: Thread Polaris Metrics P90 +parameters: + - name: pod + type: string + required: False + label: + en_US: Pod name + zh_Hans: Pod名称 + human_description: + en_US: Pod name + zh_Hans: Pod名称 + llm_description: Pod name + form: llm + - name: type + type: string + required: False + label: + en_US: Type (cpu,net,file,epoll,runq) + zh_Hans: 线程执行类型(cpu,net,file,epoll,runq) + human_description: + en_US: Type (cpu,net,file,epoll,runq) + zh_Hans: 线程执行类型(cpu,net,file,epoll,runq) + llm_description: Type (cpu,net,file,epoll,runq) + 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(Microsecond) + zh_Hans: 开始时间 (微秒) + pt_BR: Data query start time + llm_description: Data query start time(Microsecond) + 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(Microsecond) + zh_Hans: 结束时间 (微秒) + pt_BR: Data query end time(Microsecond) + llm_description: Data query end time(Microsecond) + form: llm \ No newline at end of file