|
| 1 | +__all__ = [ |
| 2 | + "MetricsGroup", |
| 3 | +] |
| 4 | + |
| 5 | +from typing import Any, Dict, List, Optional, Union |
| 6 | + |
| 7 | +from linode_api4 import drop_null_keys |
| 8 | +from linode_api4.groups import Group |
| 9 | +from linode_api4.objects.base import _flatten_request_body_recursive |
| 10 | +from linode_api4.objects.monitor_api import EntityMetricOptions, EntityMetrics |
| 11 | + |
| 12 | + |
| 13 | +class MetricsGroup(Group): |
| 14 | + """ |
| 15 | + Encapsulates Monitor-related methods of the :any:`MonitorClient`. |
| 16 | +
|
| 17 | + This group contains all features related to metrics in the API monitor-api. |
| 18 | + """ |
| 19 | + |
| 20 | + def fetch_metrics( |
| 21 | + self, |
| 22 | + service_type: str, |
| 23 | + entity_ids: list, |
| 24 | + metrics: List[Union[EntityMetricOptions, Dict[str, Any]]], |
| 25 | + **kwargs, |
| 26 | + ) -> Optional[EntityMetrics]: |
| 27 | + """ |
| 28 | + Returns metrics information for the individual entities within a specific service type. |
| 29 | +
|
| 30 | + API documentation: https://techdocs.akamai.com/linode-api/reference/post-read-metric |
| 31 | +
|
| 32 | + :param service_type: The service being monitored. |
| 33 | + Currently, only the Managed Databases (dbaas) service type is supported. |
| 34 | + :type service_type: str |
| 35 | +
|
| 36 | + :param entity_ids: The id for each individual entity from a service_type. |
| 37 | + :type entity_ids: list |
| 38 | +
|
| 39 | + :param metrics: A list of metric objects, each specifying a metric name and its corresponding aggregation function. |
| 40 | + :type metrics: list of EntityMetricOptions or Dict[str, Any] |
| 41 | +
|
| 42 | + :param kwargs: Any other arguments accepted by the api. Please refer to the API documentation for full info. |
| 43 | +
|
| 44 | + :returns: Service metrics requested. |
| 45 | + :rtype: EntityMetrics or None |
| 46 | + """ |
| 47 | + params = { |
| 48 | + "entity_ids": entity_ids, |
| 49 | + "metrics": metrics, |
| 50 | + } |
| 51 | + |
| 52 | + params.update(kwargs) |
| 53 | + |
| 54 | + result = self.client.post( |
| 55 | + f"/monitor/services/{service_type}/metrics", |
| 56 | + data=drop_null_keys(_flatten_request_body_recursive(params)), |
| 57 | + ) |
| 58 | + |
| 59 | + return EntityMetrics.from_json(result) |
0 commit comments