File size: 3,164 Bytes
d8ad0fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from typing import Dict, List, Optional

from mypy_boto3_cloudwatch.type_defs import DimensionTypeDef, MetricDataQueryTypeDef

from aws_lambda_powertools.metrics import MetricUnit
from tests.e2e.utils.data_builder.common import build_random_value


def build_metric_query_data(
    namespace: str,
    metric_name: str,
    period: int = 60,
    stat: str = "Sum",
    dimensions: Optional[List[DimensionTypeDef]] = None,
) -> List[MetricDataQueryTypeDef]:
    """Create input for CloudWatch GetMetricData API call

    Parameters
    ----------
    namespace : str
        Metric namespace to search for
    metric_name : str
        Metric name to search for
    period : int, optional
        Time period in seconds to search metrics, by default 60
    stat : str, optional
        Aggregate function to use for results, by default "Sum"
    dimensions : Optional[List[DimensionTypeDef]], optional
        Metric dimensions to search for, by default None

    Returns
    -------
    List[MetricDataQueryTypeDef]
        _description_
    """
    dimensions = dimensions or []
    data_query: List[MetricDataQueryTypeDef] = [
        {
            "Id": metric_name.lower(),
            "MetricStat": {
                "Metric": {"Namespace": namespace, "MetricName": metric_name},
                "Period": period,
                "Stat": stat,
            },
            "ReturnData": True,
        },
    ]

    if dimensions:
        data_query[0]["MetricStat"]["Metric"]["Dimensions"] = dimensions

    return data_query


def build_add_metric_input(metric_name: str, value: float, unit: str = MetricUnit.Count.value) -> Dict:
    """Create a metric input to be used with Metrics.add_metric()

    Parameters
    ----------
    metric_name : str
        metric name
    value : float
        metric value
    unit : str, optional
        metric unit, by default Count

    Returns
    -------
    Dict
        Metric input
    """
    return {"name": metric_name, "unit": unit, "value": value}


def build_multiple_add_metric_input(
    metric_name: str,
    value: float,
    unit: str = MetricUnit.Count.value,
    quantity: int = 1,
) -> List[Dict]:
    """Create list of metrics input to be used with Metrics.add_metric()

    Parameters
    ----------
    metric_name : str
        metric name
    value : float
        metric value
    unit : str, optional
        metric unit, by default Count
    quantity : int, optional
        number of metrics to be created, by default 1

    Returns
    -------
    List[Dict]
        List of metrics
    """
    return [{"name": metric_name, "unit": unit, "value": value} for _ in range(quantity)]


def build_add_dimensions_input(**dimensions) -> List[DimensionTypeDef]:
    """Create dimensions input to be used with either get_metrics or Metrics.add_dimension()

    Parameters
    ----------
    dimensions : str
        key=value pair as dimension

    Returns
    -------
    List[DimensionTypeDef]
        Metric dimension input
    """
    return [{"Name": name, "Value": value} for name, value in dimensions.items()]


def build_metric_name() -> str:
    return f"test_metric{build_random_value()}"