# Path: microcosm_pubsub/metrics.py
# class PubSubSendBatchMetrics:
#     """
#     Send metrics relating to a batch of handled messages
# 
#     """
# 
#     def __init__(self, graph):
#         self.metrics = self.get_metrics(graph)
#         self.enabled = bool(
#             self.metrics
#             and self.metrics.host != "localhost"
#             and graph.config.pubsub_send_metrics.enabled
#         )
# 
#     def get_metrics(self, graph):
#         """
#         Fetch the metrics client from the graph.
# 
#         Metrics will be disabled if the not configured.
# 
#         """
#         try:
#             return graph.metrics
#         except NotBoundError:
#             return None
# 
#     def __call__(self, elapsed_time: float, message_batch_size: int):
#         """
#         Send metrics if enabled, and if the batch processed contains at least one
#         non-ignored message
# 
#         """
#         if not self.enabled:
#             return
# 
#         if not message_batch_size > 0:
#             return
# 
#         tags = [
#             "source:microcosm-pubsub",
#         ]
# 
#         self.metrics.histogram(
#             "message_batch",
#             elapsed_time,
#             tags=tags,
#         )
# 
#         self.metrics.histogram(
#             "message_batch_size",
#             message_batch_size,
#             tags=tags,
#         )
# 
# class PubSubSendMetrics:
#     """
#     Send metrics relating to a single MessageHandlingResult
# 
#     """
# 
#     def __init__(self, graph):
#         self.metrics = self.get_metrics(graph)
#         self.enabled = bool(
#             self.metrics
#             and self.metrics.host != "localhost"
#             and graph.config.pubsub_send_metrics.enabled
#         )
# 
#     def get_metrics(self, graph):
#         """
#         Fetch the metrics client from the graph.
# 
#         Metrics will be disabled if the not configured.
# 
#         """
#         try:
#             return graph.metrics
#         except NotBoundError:
#             return None
# 
#     def __call__(self, result: MessageHandlingResult):
#         """
#         Send metrics if enabled.
# 
#         """
#         if not self.enabled:
#             return
# 
#         if result.result == MessageHandlingResultType.IGNORED:
#             return
# 
#         tags = [
#             "source:microcosm-pubsub",
#             f"result:{result.result}",
#             f"media-type:{result.media_type}",
#         ]
#         self.metrics.histogram(
#             "message",
#             result.elapsed_time,
#             tags=tags,
#         )
# 
#         if result.handle_start_time:
#             self.metrics.histogram(
#                 "message_handle_start",
#                 result.handle_start_time,
#                 tags=tags,
#             )


from unittest.mock import Mock, patch
from hamcrest import assert_that, equal_to, is_
from microcosm.api import create_object_graph, load_from_dict
from microcosm_pubsub.metrics import PubSubSendBatchMetrics, PubSubSendMetrics

def test_configure_batch_metrics_default_metrics_not_installed():
    """
    Disable metrics by default if metrics not installed.

    """
    with patch.object(PubSubSendBatchMetrics, "get_metrics") as mocked:
        mocked.return_value = None

        graph = create_object_graph("example", testing=True)
        assert_that(graph.pubsub_send_batch_metrics.enabled, is_(equal_to(False)))


def test_configure_batch_metrics_default_metrics_installed():
    """
    Enabled metrics by default if installed.

    """
    with patch.object(PubSubSendBatchMetrics, "get_metrics") as mocked:
        mocked.return_value = Mock(host="localhost")

        graph = create_object_graph("example", testing=True)
        assert_that(graph.pubsub_send_batch_metrics.enabled, is_(equal_to(False)))


def test_configure_batch_metrics_default_metrics_configured():
    """
    Enabled metrics by default if installed.

    """

# Path: microcosm_pubsub/metrics.py
# class PubSubSendBatchMetrics:
#     """
#     Send metrics relating to a batch of handled messages
# 
#     """
# 
#     def __init__(self, graph):
#         self.metrics = self.get_metrics(graph)
#         self.enabled = bool(
#             self.metrics
#             and self.metrics.host != "localhost"
#             and graph.config.pubsub_send_metrics.enabled
#         )
# 
#     def get_metrics(self, graph):
#         """
#         Fetch the metrics client from the graph.
# 
#         Metrics will be disabled if the not configured.
# 
#         """
#         try:
#             return graph.metrics
#         except NotBoundError:
#             return None
# 
#     def __call__(self, elapsed_time: float, message_batch_size: int):
#         """
#         Send metrics if enabled, and if the batch processed contains at least one
#         non-ignored message
# 
#         """
#         if not self.enabled:
#             return
# 
#         if not message_batch_size > 0:
#             return
# 
#         tags = [
#             "source:microcosm-pubsub",
#         ]
# 
#         self.metrics.histogram(
#             "message_batch",
#             elapsed_time,
#             tags=tags,
#         )
# 
#         self.metrics.histogram(
#             "message_batch_size",
#             message_batch_size,
#             tags=tags,
#         )
# 
# class PubSubSendMetrics:
#     """
#     Send metrics relating to a single MessageHandlingResult
# 
#     """
# 
#     def __init__(self, graph):
#         self.metrics = self.get_metrics(graph)
#         self.enabled = bool(
#             self.metrics
#             and self.metrics.host != "localhost"
#             and graph.config.pubsub_send_metrics.enabled
#         )
# 
#     def get_metrics(self, graph):
#         """
#         Fetch the metrics client from the graph.
# 
#         Metrics will be disabled if the not configured.
# 
#         """
#         try:
#             return graph.metrics
#         except NotBoundError:
#             return None
# 
#     def __call__(self, result: MessageHandlingResult):
#         """
#         Send metrics if enabled.
# 
#         """
#         if not self.enabled:
#             return
# 
#         if result.result == MessageHandlingResultType.IGNORED:
#             return
# 
#         tags = [
#             "source:microcosm-pubsub",
#             f"result:{result.result}",
#             f"media-type:{result.media_type}",
#         ]
#         self.metrics.histogram(
#             "message",
#             result.elapsed_time,
#             tags=tags,
#         )
# 
#         if result.handle_start_time:
#             self.metrics.histogram(
#                 "message_handle_start",
#                 result.handle_start_time,
#                 tags=tags,
#             )


# Path: microcosm_pubsub/tests/test_metrics.py
from unittest.mock import Mock, patch
from hamcrest import assert_that, equal_to, is_
from microcosm.api import create_object_graph, load_from_dict
from microcosm_pubsub.metrics import PubSubSendBatchMetrics, PubSubSendMetrics


def test_configure_batch_metrics_default_metrics_not_installed():
    """
    Disable metrics by default if metrics not installed.

    """
    with patch.object(PubSubSendBatchMetrics, "get_metrics") as mocked:
        mocked.return_value = None

        graph = create_object_graph("example", testing=True)
        assert_that(graph.pubsub_send_batch_metrics.enabled, is_(equal_to(False)))


def test_configure_batch_metrics_default_metrics_installed():
    """
    Enabled metrics by default if installed.

    """
    with patch.object(PubSubSendBatchMetrics, "get_metrics") as mocked:
        mocked.return_value = Mock(host="localhost")

        graph = create_object_graph("example", testing=True)
        assert_that(graph.pubsub_send_batch_metrics.enabled, is_(equal_to(False)))


def test_configure_batch_metrics_default_metrics_configured():
    """
    Enabled metrics by default if installed.

    """

    with patch.object(PubSubSendBatchMetrics, "get_metrics") as mocked: