# Path: app/models.py
# ANDROID_PLATFORM = 'android'
# 
# APNS_PLATFORM = 'apns'
# 
# class App(models.Model):
#     """
#     Model that contains information about the supported apps by the middleware.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     app_id = models.CharField(max_length=255)
# 
#     push_key = models.CharField(max_length=255)
#     logentries_token = models.CharField(max_length=255, blank=False, null=False, default='')
#     partner_logentries_token = models.CharField(max_length=255, blank=True, null=True, default='')
# 
#     def __str__(self):
#         return '{0} for {1}'.format(self.app_id, self.platform)
# 
#     class Meta:
#         unique_together = ('app_id', 'platform')
# 
# class Device(models.Model):
#     """
#     Model for all device who register at the middleware.
#     """
#     # FIXME: We need this to be backwards compatible for one release.
#     id = models.CharField(max_length=255, unique=True)
# 
#     name = models.CharField(max_length=255, blank=True, null=True)
#     sip_user_id = models.CharField(max_length=255, unique=True, primary_key=True)
#     os_version = models.CharField(max_length=255, blank=True, null=True)
#     client_version = models.CharField(max_length=255, blank=True, null=True)
#     token = models.CharField(max_length=250)
#     sandbox = models.BooleanField(default=False)
#     last_seen = models.DateTimeField(blank=True, null=True)
#     app = models.ForeignKey(App)
#     remote_logging_id = models.CharField(max_length=255, blank=True, null=True)
# 
#     def __str__(self):
#         return '{0} - {1}'.format(self.sip_user_id, self.name)
# 
#     # FIXME: We need this to be backwards compatible for one release.
#     def save(self, *args, **kwargs):
#         """
#         Make sure the id fields stays the same as sip_user_id so we are
#         backwards compatible.
#         """
#         if self.sip_user_id:
#             self.id = self.sip_user_id
#             obj = super(Device, self).save(*args, **kwargs)
#             return obj
# 
# 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 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


import datetime
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import render
from .models import ANDROID_PLATFORM, APNS_PLATFORM, App, Device, GCM_PLATFORM, ResponseLog
from .utils import get_metrics




class DeviceAdmin(admin.ModelAdmin):
    search_fields = ('name', 'sip_user_id',)


class AppAdmin(admin.ModelAdmin):

# Path: app/models.py
# ANDROID_PLATFORM = 'android'
# 
# APNS_PLATFORM = 'apns'
# 
# class App(models.Model):
#     """
#     Model that contains information about the supported apps by the middleware.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     app_id = models.CharField(max_length=255)
# 
#     push_key = models.CharField(max_length=255)
#     logentries_token = models.CharField(max_length=255, blank=False, null=False, default='')
#     partner_logentries_token = models.CharField(max_length=255, blank=True, null=True, default='')
# 
#     def __str__(self):
#         return '{0} for {1}'.format(self.app_id, self.platform)
# 
#     class Meta:
#         unique_together = ('app_id', 'platform')
# 
# class Device(models.Model):
#     """
#     Model for all device who register at the middleware.
#     """
#     # FIXME: We need this to be backwards compatible for one release.
#     id = models.CharField(max_length=255, unique=True)
# 
#     name = models.CharField(max_length=255, blank=True, null=True)
#     sip_user_id = models.CharField(max_length=255, unique=True, primary_key=True)
#     os_version = models.CharField(max_length=255, blank=True, null=True)
#     client_version = models.CharField(max_length=255, blank=True, null=True)
#     token = models.CharField(max_length=250)
#     sandbox = models.BooleanField(default=False)
#     last_seen = models.DateTimeField(blank=True, null=True)
#     app = models.ForeignKey(App)
#     remote_logging_id = models.CharField(max_length=255, blank=True, null=True)
# 
#     def __str__(self):
#         return '{0} - {1}'.format(self.sip_user_id, self.name)
# 
#     # FIXME: We need this to be backwards compatible for one release.
#     def save(self, *args, **kwargs):
#         """
#         Make sure the id fields stays the same as sip_user_id so we are
#         backwards compatible.
#         """
#         if self.sip_user_id:
#             self.id = self.sip_user_id
#             obj = super(Device, self).save(*args, **kwargs)
#             return obj
# 
# 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 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/admin.py
import datetime
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import render
from .models import ANDROID_PLATFORM, APNS_PLATFORM, App, Device, GCM_PLATFORM, ResponseLog
from .utils import get_metrics





class DeviceAdmin(admin.ModelAdmin):
    search_fields = ('name', 'sip_user_id',)


class AppAdmin(admin.ModelAdmin):

    pass