#!/usr/bin/python

"""
Shows backlog count of ScheduledJobs of type Email
"""

from __future__ import absolute_import

from django.conf import settings
from django.core.management.base import BaseCommand

from zerver.models import ScheduledJob

from datetime import datetime, timedelta

class Command(BaseCommand):
    help = """Shows backlog count of ScheduledJobs of type Email
(The number of currently overdue (by at least a minute) email jobs)

This is run as part of the nagios health check for the deliver_email command.
Please note that this is only currently useful for Zulip Enterprise deploys.

Usage: python manage.py print_email_delivery_backlog
"""

    def handle(self):
        print len(ScheduledJob.objects.filter(type=ScheduledJob.EMAIL,
                                                  scheduled_timestamp__lte=datetime.utcnow()-timedelta(minutes=1)))
        return


add print_email_delivery_backlog for nagios reasons