-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathmetrics_common.py
More file actions
65 lines (56 loc) · 2.03 KB
/
metrics_common.py
File metadata and controls
65 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""Location: ./mcpgateway/utils/metrics_common.py
Copyright 2025
SPDX-License-Identifier: Apache-2.0
Authors: Mihai Criveti
Common utilities for metrics handling across service modules.
"""
# Standard
from typing import List
# First-Party
from mcpgateway.schemas import TopPerformer
def build_top_performers(results: List) -> List[TopPerformer]:
"""
Convert database query results to TopPerformer objects.
This utility function eliminates code duplication across service modules
that need to convert database query results with metrics into TopPerformer objects.
Args:
results: List of database query results, each containing:
- id: Entity ID
- name: Entity name
- execution_count: Total executions
- avg_response_time: Average response time
- success_rate: Success rate percentage
- last_execution: Last execution timestamp
Returns:
List[TopPerformer]: List of TopPerformer objects with proper type conversions
Examples:
>>> from unittest.mock import MagicMock
>>> result = MagicMock()
>>> result.id = 1
>>> result.name = "test"
>>> result.execution_count = 10
>>> result.avg_response_time = 1.5
>>> result.success_rate = 85.0
>>> result.last_execution = None
>>> performers = build_top_performers([result])
>>> len(performers)
1
>>> performers[0].id
1
>>> performers[0].execution_count
10
>>> performers[0].avg_response_time
1.5
"""
return [
TopPerformer(
id=result.id,
name=result.name,
execution_count=result.execution_count or 0,
avg_response_time=float(result.avg_response_time) if result.avg_response_time is not None else None,
success_rate=float(result.success_rate) if result.success_rate is not None else None,
last_execution=result.last_execution,
)
for result in results
]