""" The module to create and manage measures using in the process. """
import functools
import json
import os
import sys
from google.protobuf import any_pb2
from google.protobuf.json_format import MessageToDict
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from average import Average
from count import Count
from data_points import DataPoints
from measure import Measure
from metric import Metric
from time_consumption import TimeConsumption
TEST_SCRIPT_METRICS_KEY = 'test_script_metrics'
TEST_SCRIPT_METRICS_JSONPB_FILENAME = f'{TEST_SCRIPT_METRICS_KEY}.jsonpb'
_metric = Metric()
def _create_name(*name_pieces: str) -> str:
if len(name_pieces) == 0:
raise ValueError('Need at least one name piece.')
return '/'.join(list(name_pieces))
def _register(m: Measure) -> Measure:
_metric.register(m)
return m
def average(*name_pieces: str) -> Average:
return _register(Average(_create_name(*name_pieces)))
def count(*name_pieces: str) -> Count:
return _register(Count(_create_name(*name_pieces)))
def data_points(*name_pieces: str) -> DataPoints:
return _register(DataPoints(_create_name(*name_pieces)))
def time_consumption(*name_pieces: str) -> TimeConsumption:
return _register(TimeConsumption(_create_name(*name_pieces)))
def timed_func(*name_pieces: str):
"""time_consumption() as a @decorator."""
def decorator(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
with time_consumption(*name_pieces):
func(*args, **kwargs)
return wrapped
return decorator
def tag(*args: str) -> None:
"""Adds a tag to the Metric to tag the final results; see Metric for details.
"""
_metric.tag(*args)
def clear() -> None:
"""Clears all the registered Measures."""
_metric.clear()
def size() -> int:
"""Gets the current size of registered Measures."""
return _metric.size()
def to_dict() -> dict:
"""Converts all the registered Measures to a dict.
The records are wrapped in protobuf Any message before exported as dict
so that an additional key "@type" is included.
"""
any_msg = any_pb2.Any()
any_msg.Pack(_metric.dump())
return MessageToDict(any_msg, preserving_proto_field_name=True)
def to_json() -> str:
"""Converts all the registered Measures to a json str."""
return json.dumps(to_dict(), sort_keys=True, indent=2)
def dump(dir_path: str) -> None:
"""Dumps the metric data into test_script_metrics.jsonpb in the |path|."""
os.makedirs(dir_path, exist_ok=True)
with open(os.path.join(dir_path, TEST_SCRIPT_METRICS_JSONPB_FILENAME),
'w',
encoding='utf-8') as wf:
wf.write(to_json())