from datetime import timedelta

from flask import current_app

from app import performance_platform_client
from app.dao.notifications_dao import (
    dao_get_total_notifications_sent_per_day_for_performance_platform,
)
from app.utils import get_midnight_in_utc


def send_processing_time_to_performance_platform(local_date):
    start_time = get_midnight_in_utc(local_date)
    end_time = get_midnight_in_utc(local_date + timedelta(days=1))

    send_processing_time_for_start_and_end(start_time, end_time, local_date)


def send_processing_time_for_start_and_end(start_time, end_time, local_date):
    result = dao_get_total_notifications_sent_per_day_for_performance_platform(start_time, end_time)

    current_app.logger.info(
        'Sending processing-time to performance platform for date {}. Total: {}, under 10 secs {}'.format(
            start_time, result.messages_total, result.messages_within_10_secs
        )
    )

    send_processing_time_data(start_time, 'messages-total', result.messages_total)
    send_processing_time_data(start_time, 'messages-within-10-secs', result.messages_within_10_secs)


def send_processing_time_data(start_time, status, count):
    payload = performance_platform_client.format_payload(
        dataset='processing-time',
        start_time=start_time,
        group_name='status',
        group_value=status,
        count=count
    )

    performance_platform_client.send_stats_to_performance_platform(payload)
