# Path: django_datawatch/querysets.py
# class CheckExecutionQuerySet(models.QuerySet):
#     def ghost_executions(self):
#         """
#         :return: check executions that do not have checks anymore (check has been deleted)
#         """
#         return self.exclude(slug__in=datawatch.get_all_registered_check_slugs())
# 
# Path: django_datawatch/datawatch.py
# class DatawatchHandler(object):
# class Scheduler(object):
#     def __init__(self):
#     def autodiscover_checks(self, module_name='checks'):
#     def register(self, check_class):
#     def get_all_registered_checks(self):
#     def get_all_registered_check_slugs(self):
#     def get_check_class(self, slug):
#     def get_checks_for_related_model(self, model):
#     def get_checks_for_model(self, model):
#     def get_slug(self, module, class_name):
#     def get_backend(self):
#     def delete_results(self, sender, instance):
#     def update_related(self, sender, instance):
#     def run_checks(self, force=False, slug=None):
#     def get_last_executions(self):
# def make_model_uid(model):
# def delete_results(sender, instance, using, **kwargs):
# def run_checks(sender, instance, created, raw, using, **kwargs):
# 
# Path: django_datawatch/querysets.py
# class ResultQuerySet(models.QuerySet):
#     def for_user(self, user):
#         return self.filter(Q(assigned_to_group__isnull=True) | Q(assigned_to_group__in=user.groups.all()),
#                            Q(assigned_to_user__isnull=True) | Q(assigned_to_user=user))
# 
#     def failed(self):
#         return self.exclude(status__in=(self.model.STATUS.unknown, self.model.STATUS.ok))
# 
#     def ok(self):
#         return self.filter(status=self.model.STATUS.ok)
# 
#     def unknown(self):
#         return self.filter(status=self.model.STATUS.unknown)
# 
#     def unacknowledged(self):
#         return self.exclude(acknowledged_until__gt=timezone.now())
# 
#     def with_status_name(self):
#         case = Case(output_field=models.CharField())
#         for status_value in self.model.STATUS._db_values:
#             case.cases.append(
#                 When(status=status_value, then=Value(str(self.model.STATUS[status_value]))),
#             )
#         return self.annotate(status_name=case)
# 
#     def get_stats(self):
#         return self.values('status').annotate(amount=Count('id')).with_status_name()
# 
#     def ghost_results(self):
#         """
#         :return: results that do not have checks anymore (check has been deleted)
#         """
#         return self.exclude(slug__in=datawatch.get_all_registered_check_slugs())


from dateutil import relativedelta
from django.utils import timezone
from django.conf import settings
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from django_extensions.db.fields.json import JSONField
from model_utils.choices import Choices
from model_utils.models import TimeStampedModel
from django_datawatch.querysets import CheckExecutionQuerySet
from .datawatch import datawatch
from .querysets import ResultQuerySet
                       ('config', 'Can change the configuration for results'), ('refresh', 'Can refresh results'))

    def acknowledge(self, user, days, reason=None, commit=True):
        # calculate end of requested acknowledgement
        acknowledged_until = timezone.now() + relativedelta.relativedelta(days=days)

        # check that we're not accidentally overriding the current setup
        if self.status in (self.STATUS.warning, self.STATUS.critical):
            if self.is_acknowledged() and self.acknowledged_until > acknowledged_until:
                raise AlreadyAcknowledged()
        self.acknowledged_at = timezone.now()
        self.acknowledged_by = user
        self.acknowledged_until = acknowledged_until
        self.acknowledged_reason = reason or ''
        if commit:
            self.save(update_fields=['acknowledged_at', 'acknowledged_by', 'acknowledged_until', 'acknowledged_reason'])

    def is_acknowledged(self):
        return self.acknowledged_until and self.acknowledged_until >= timezone.now()

    def __str__(self):
        return self.slug

    def get_check_instance(self):
        return datawatch.get_check_class(self.slug)()

    def get_payload(self):
        return self.get_check_instance().get_payload(self.identifier)

    def get_formatted_data(self):

# Path: django_datawatch/querysets.py
# class CheckExecutionQuerySet(models.QuerySet):
#     def ghost_executions(self):
#         """
#         :return: check executions that do not have checks anymore (check has been deleted)
#         """
#         return self.exclude(slug__in=datawatch.get_all_registered_check_slugs())
# 
# Path: django_datawatch/datawatch.py
# class DatawatchHandler(object):
# class Scheduler(object):
#     def __init__(self):
#     def autodiscover_checks(self, module_name='checks'):
#     def register(self, check_class):
#     def get_all_registered_checks(self):
#     def get_all_registered_check_slugs(self):
#     def get_check_class(self, slug):
#     def get_checks_for_related_model(self, model):
#     def get_checks_for_model(self, model):
#     def get_slug(self, module, class_name):
#     def get_backend(self):
#     def delete_results(self, sender, instance):
#     def update_related(self, sender, instance):
#     def run_checks(self, force=False, slug=None):
#     def get_last_executions(self):
# def make_model_uid(model):
# def delete_results(sender, instance, using, **kwargs):
# def run_checks(sender, instance, created, raw, using, **kwargs):
# 
# Path: django_datawatch/querysets.py
# class ResultQuerySet(models.QuerySet):
#     def for_user(self, user):
#         return self.filter(Q(assigned_to_group__isnull=True) | Q(assigned_to_group__in=user.groups.all()),
#                            Q(assigned_to_user__isnull=True) | Q(assigned_to_user=user))
# 
#     def failed(self):
#         return self.exclude(status__in=(self.model.STATUS.unknown, self.model.STATUS.ok))
# 
#     def ok(self):
#         return self.filter(status=self.model.STATUS.ok)
# 
#     def unknown(self):
#         return self.filter(status=self.model.STATUS.unknown)
# 
#     def unacknowledged(self):
#         return self.exclude(acknowledged_until__gt=timezone.now())
# 
#     def with_status_name(self):
#         case = Case(output_field=models.CharField())
#         for status_value in self.model.STATUS._db_values:
#             case.cases.append(
#                 When(status=status_value, then=Value(str(self.model.STATUS[status_value]))),
#             )
#         return self.annotate(status_name=case)
# 
#     def get_stats(self):
#         return self.values('status').annotate(amount=Count('id')).with_status_name()
# 
#     def ghost_results(self):
#         """
#         :return: results that do not have checks anymore (check has been deleted)
#         """
#         return self.exclude(slug__in=datawatch.get_all_registered_check_slugs())


# Path: django_datawatch/models.py
from dateutil import relativedelta
from django.utils import timezone
from django.conf import settings
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from django_extensions.db.fields.json import JSONField
from model_utils.choices import Choices
from model_utils.models import TimeStampedModel
from django_datawatch.querysets import CheckExecutionQuerySet
from .datawatch import datawatch
from .querysets import ResultQuerySet

                       ('config', 'Can change the configuration for results'), ('refresh', 'Can refresh results'))

    def acknowledge(self, user, days, reason=None, commit=True):
        # calculate end of requested acknowledgement
        acknowledged_until = timezone.now() + relativedelta.relativedelta(days=days)

        # check that we're not accidentally overriding the current setup
        if self.status in (self.STATUS.warning, self.STATUS.critical):
            if self.is_acknowledged() and self.acknowledged_until > acknowledged_until:
                raise AlreadyAcknowledged()
        self.acknowledged_at = timezone.now()
        self.acknowledged_by = user
        self.acknowledged_until = acknowledged_until
        self.acknowledged_reason = reason or ''
        if commit:
            self.save(update_fields=['acknowledged_at', 'acknowledged_by', 'acknowledged_until', 'acknowledged_reason'])

    def is_acknowledged(self):
        return self.acknowledged_until and self.acknowledged_until >= timezone.now()

    def __str__(self):
        return self.slug

    def get_check_instance(self):
        return datawatch.get_check_class(self.slug)()

    def get_payload(self):
        return self.get_check_instance().get_payload(self.identifier)

    def get_formatted_data(self):

        return datawatch.get_check_class(self.slug)().format_result_data(self)