# Path: app/models.py
# GCM_PLATFORM = 'gcm'
# 
# class ResponseLog(models.Model):
#     """
#     Model for logging info about the device response.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     roundtrip_time = models.FloatField()
#     available = models.BooleanField()
#     date = models.DateTimeField(auto_now_add=True)
# 
# Path: app/utils.py
# def fill_log_statement(log_statement, dict_with_variables, anonymize=False):
#     """
#     Function that anonymizes and inserts variables into log statements.
# 
#     Args:
#         log_statement (str): The message to log.
#         dict_with_variables (OrderedDict): OrderedDict that contains the
#             variables we want to insert into the logging statement.
#         anonymize (bool): Boolean to check if we want to anonymize.
# 
#     Returns:
#         str: String containing the given variables.
#     """
#     anonymize_keys = [
#         LOG_SIP_USER_ID,
#         LOG_CALLER_ID,
#         LOG_CALL_TO,
#         LOG_CALL_FROM,
#         LOG_CONTACT,
#         LOG_DIGEST_USERNAME,
#         LOG_NONCE,
#         LOG_USERNAME,
#         LOG_EMAIL,
#     ]
# 
#     if anonymize:
#         for key in dict_with_variables.keys():
#             if key in anonymize_keys:
#                 dict_with_variables[key] = key.upper()
# 
#     list_with_variables = [dict_with_variables[x] for x in dict_with_variables]
#     updated_log_statement = log_statement.format(*list_with_variables)
#     return updated_log_statement
# 
# def get_metrics(start_date, end_date, platform):
#     """
#     Function to get a dict with metrics for the given date range and platform.
# 
#     Args:
#         start_date (date): Start date to get metrics for.
#         end_date (date): End date to get metrics for.
#         platform (string): Platform to get metrics for.
# 
#     Returns:
#         Dict containing the metrics.
#     """
#     def _get_min(query):
#         return query.aggregate(Min('roundtrip_time'))['roundtrip_time__min']
# 
#     def _get_max(query):
#         return query.aggregate(Max('roundtrip_time'))['roundtrip_time__max']
# 
#     def _get_avg(query):
#         return query.aggregate(Avg('roundtrip_time'))['roundtrip_time__avg']
# 
#     base_query = ResponseLog.objects.filter(
#         platform=platform, date__range=(start_date, end_date)).order_by('roundtrip_time')
#     total_count = base_query.count()
# 
#     percentile = int(total_count * 0.95)
# 
#     available_query = base_query.filter(available=True)
#     available_count = available_query.count()
#     avg_available = _get_avg(available_query[:percentile])
#     min_available = _get_min(available_query[:percentile])
#     max_available = _get_max(available_query[:percentile])
# 
#     not_available_query = base_query.filter(available=False)
#     not_available_count = not_available_query.count()
#     avg_not_available = _get_avg(not_available_query[:percentile])
#     min_not_available = _get_min(not_available_query[:percentile])
#     max_not_available = _get_max(not_available_query[:percentile])
# 
#     results = {
#         'platform': platform,
#         'start_date': start_date,
#         'end_date': end_date,
#         'total_count': total_count,
#         'available': {
#             'count': available_count,
#             'avg': avg_available,
#             'min': min_available,
#             'max': max_available,
#         },
#         'not_available': {
#             'count': not_available_count,
#             'avg': avg_not_available,
#             'min': min_not_available,
#             'max': max_not_available,
#         },
#     }
# 
#     return results


from collections import OrderedDict
from django.test import TestCase
from ..models import GCM_PLATFORM, ResponseLog
from ..utils import fill_log_statement, get_metrics
import datetime
            platform=platform,
            roundtrip_time=2.5,
            available=True,
        )
        log3.date = self.first_of_month.replace(day=3)
        log3.save()

        # Not available logs.
        log4 = ResponseLog.objects.create(
            platform=platform,
            roundtrip_time=4.0,
            available=False,
        )
        log4.date = self.first_of_month.replace(day=2)
        log4.save()

        log5 = ResponseLog.objects.create(
            platform=platform,
            roundtrip_time=6.0,
            available=False,
        )
        log5.date = self.first_of_month.replace(day=2)
        log5.save()

    def test_get_metrics(self):
        """
        Test for getting metrics for 1 platform.
        """
        self._create_entries(GCM_PLATFORM)


# Path: app/models.py
# GCM_PLATFORM = 'gcm'
# 
# class ResponseLog(models.Model):
#     """
#     Model for logging info about the device response.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     roundtrip_time = models.FloatField()
#     available = models.BooleanField()
#     date = models.DateTimeField(auto_now_add=True)
# 
# Path: app/utils.py
# def fill_log_statement(log_statement, dict_with_variables, anonymize=False):
#     """
#     Function that anonymizes and inserts variables into log statements.
# 
#     Args:
#         log_statement (str): The message to log.
#         dict_with_variables (OrderedDict): OrderedDict that contains the
#             variables we want to insert into the logging statement.
#         anonymize (bool): Boolean to check if we want to anonymize.
# 
#     Returns:
#         str: String containing the given variables.
#     """
#     anonymize_keys = [
#         LOG_SIP_USER_ID,
#         LOG_CALLER_ID,
#         LOG_CALL_TO,
#         LOG_CALL_FROM,
#         LOG_CONTACT,
#         LOG_DIGEST_USERNAME,
#         LOG_NONCE,
#         LOG_USERNAME,
#         LOG_EMAIL,
#     ]
# 
#     if anonymize:
#         for key in dict_with_variables.keys():
#             if key in anonymize_keys:
#                 dict_with_variables[key] = key.upper()
# 
#     list_with_variables = [dict_with_variables[x] for x in dict_with_variables]
#     updated_log_statement = log_statement.format(*list_with_variables)
#     return updated_log_statement
# 
# def get_metrics(start_date, end_date, platform):
#     """
#     Function to get a dict with metrics for the given date range and platform.
# 
#     Args:
#         start_date (date): Start date to get metrics for.
#         end_date (date): End date to get metrics for.
#         platform (string): Platform to get metrics for.
# 
#     Returns:
#         Dict containing the metrics.
#     """
#     def _get_min(query):
#         return query.aggregate(Min('roundtrip_time'))['roundtrip_time__min']
# 
#     def _get_max(query):
#         return query.aggregate(Max('roundtrip_time'))['roundtrip_time__max']
# 
#     def _get_avg(query):
#         return query.aggregate(Avg('roundtrip_time'))['roundtrip_time__avg']
# 
#     base_query = ResponseLog.objects.filter(
#         platform=platform, date__range=(start_date, end_date)).order_by('roundtrip_time')
#     total_count = base_query.count()
# 
#     percentile = int(total_count * 0.95)
# 
#     available_query = base_query.filter(available=True)
#     available_count = available_query.count()
#     avg_available = _get_avg(available_query[:percentile])
#     min_available = _get_min(available_query[:percentile])
#     max_available = _get_max(available_query[:percentile])
# 
#     not_available_query = base_query.filter(available=False)
#     not_available_count = not_available_query.count()
#     avg_not_available = _get_avg(not_available_query[:percentile])
#     min_not_available = _get_min(not_available_query[:percentile])
#     max_not_available = _get_max(not_available_query[:percentile])
# 
#     results = {
#         'platform': platform,
#         'start_date': start_date,
#         'end_date': end_date,
#         'total_count': total_count,
#         'available': {
#             'count': available_count,
#             'avg': avg_available,
#             'min': min_available,
#             'max': max_available,
#         },
#         'not_available': {
#             'count': not_available_count,
#             'avg': avg_not_available,
#             'min': min_not_available,
#             'max': max_not_available,
#         },
#     }
# 
#     return results


# Path: app/tests/test_utils.py
from collections import OrderedDict
from django.test import TestCase
from ..models import GCM_PLATFORM, ResponseLog
from ..utils import fill_log_statement, get_metrics
import datetime

            platform=platform,
            roundtrip_time=2.5,
            available=True,
        )
        log3.date = self.first_of_month.replace(day=3)
        log3.save()

        # Not available logs.
        log4 = ResponseLog.objects.create(
            platform=platform,
            roundtrip_time=4.0,
            available=False,
        )
        log4.date = self.first_of_month.replace(day=2)
        log4.save()

        log5 = ResponseLog.objects.create(
            platform=platform,
            roundtrip_time=6.0,
            available=False,
        )
        log5.date = self.first_of_month.replace(day=2)
        log5.save()

    def test_get_metrics(self):
        """
        Test for getting metrics for 1 platform.
        """
        self._create_entries(GCM_PLATFORM)


        metrics = get_metrics(self.first_of_month, self.end_date, GCM_PLATFORM)