Get stats for status codes by date.

    Returns:
        list: status codes + date grouped by type: 2xx, 3xx, 4xx, 5xx, attacks.

def status_codes_by_date_stats():
    """
    Get stats for status codes by date.

    Returns:
        list: status codes + date grouped by type: 2xx, 3xx, 4xx, 5xx, attacks.
    """

    def date_counter(queryset):
        return dict(Counter(map(
            lambda dt: ms_since_epoch(datetime.combine(
                make_naive(dt), datetime.min.time())),
            list(queryset.values_list('datetime', flat=True)))))

    codes = {low: date_counter(
        RequestLog.objects.filter(status_code__gte=low, status_code__lt=high))
        for low, high in ((200, 300), (300, 400), (400, 500))}
    codes[500] = date_counter(RequestLog.objects.filter(status_code__gte=500))
    codes['attacks'] = date_counter(RequestLog.objects.filter(
        status_code__in=(400, 444, 502)))

    stats = {}
    for code in (200, 300, 400, 500, 'attacks'):
        for date, count in codes[code].items():
            if stats.get(date, None) is None:
                stats[date] = {200: 0, 300: 0, 400: 0, 500: 0, 'attacks': 0}
            stats[date][code] += count

    stats = sorted([(k, v) for k, v in stats.items()], key=lambda x: x[0])
    return stats