# Path: core/models.py
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def validate_cron(value):
#     def __init__(self, *args, **kwargs):
#     def formfield(self, **kwargs):
#     def validate(self, value, model_instance):
#     def get_actual_hosts(self) -> models.QuerySet:
#     def get_playbook_name(self) -> str:
#     def get_hosts_without_groups(self) -> models.QuerySet:
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def get_vars(self):
#     def __str__(self):
#     def __str__(self):
#     def create_task(self, user, is_automatically_created=False, repeat_number=0):
#     def have_uncompleted_task(self):
#     def __str__(self):
#     def get_ansible_command(self):
#     def get_duration(self) -> datetime.timedelta:
#     def stop(self):
#     def __str__(self):
# class CronField(models.CharField):
# class TaskOperationsMixin:
# class Variable(models.Model):
#     class Meta:
# class HostGroup(models.Model):
#     class Meta:
# class Host(models.Model):
#     class Meta:
# class AnsibleUser(models.Model):
#     class Meta:
# class TaskTemplate(TaskOperationsMixin, models.Model):
#     class Meta:
# class RepeatSetting(models.Model):
# class Task(TaskOperationsMixin, models.Model):
#     class Meta:
# class TaskLog(models.Model):
#     class Meta:
# 
# Path: core/serializers.py
# class TaskLogSerializer(serializers.ModelSerializer):
#     class Meta:
#     def get_task_status(self, obj):
# 
# Path: core/consts.py
# IN_PROGRESS = 'in_progress'
# COMPLETED = 'completed'
# STOPPED = 'stopped'
# FAIL = 'fail'
# WAIT = 'wait'
# STATUSES = [IN_PROGRESS, COMPLETED, STOPPED, FAIL, WAIT]
# STATUS_CHOICES = [(status, status) for status in STATUSES]
# NOT_RUN_STATUSES = [COMPLETED, STOPPED, FAIL]
# RUN_STATUSES = [IN_PROGRESS, WAIT]
# VERBOSE_CHOICES = (
#     ('', 'silent'),
#     ('v', 'standard'),
#     ('vv', 'detail'),
#     ('vvv', 'very detail'),
#     ('vvvv', 'all'),
# )


from django.db.models import Count
from django.http import HttpResponse
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.authentication import TokenAuthentication
from prometheus_client import generate_latest
from core import models
from core import serializers
from core import consts


tokenBearer = TokenAuthentication
tokenBearer.keyword = 'Bearer'


class TaskLogs(ListAPIView):
    model = models.TaskLog
    serializer_class = serializers.TaskLogSerializer

    def get_queryset(self):
        last_log_id = self.request.GET.get('last_log_id', 0)
        return self.model.objects.filter(task_id=self.kwargs['task_id'], id__gt=last_log_id)
task_logs = TaskLogs.as_view()


class DjangoMetrics(APIView):
    authentication_classes = (tokenBearer,)

    def get(self, request):
        result = generate_latest().decode()
        return HttpResponse(result, content_type='text/plain; charset=utf-8')


class AnsibleManagerMetrics(APIView):
    authentication_classes = (tokenBearer,)

    def get(self, request):
        result = '# HELP ansible_manager_template_last_task_success show success or fail last task\n'
        result += '# TYPE ansible_manager_template_last_task_success gauge\n'

# Path: core/models.py
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def validate_cron(value):
#     def __init__(self, *args, **kwargs):
#     def formfield(self, **kwargs):
#     def validate(self, value, model_instance):
#     def get_actual_hosts(self) -> models.QuerySet:
#     def get_playbook_name(self) -> str:
#     def get_hosts_without_groups(self) -> models.QuerySet:
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def get_vars(self):
#     def __str__(self):
#     def __str__(self):
#     def create_task(self, user, is_automatically_created=False, repeat_number=0):
#     def have_uncompleted_task(self):
#     def __str__(self):
#     def get_ansible_command(self):
#     def get_duration(self) -> datetime.timedelta:
#     def stop(self):
#     def __str__(self):
# class CronField(models.CharField):
# class TaskOperationsMixin:
# class Variable(models.Model):
#     class Meta:
# class HostGroup(models.Model):
#     class Meta:
# class Host(models.Model):
#     class Meta:
# class AnsibleUser(models.Model):
#     class Meta:
# class TaskTemplate(TaskOperationsMixin, models.Model):
#     class Meta:
# class RepeatSetting(models.Model):
# class Task(TaskOperationsMixin, models.Model):
#     class Meta:
# class TaskLog(models.Model):
#     class Meta:
# 
# Path: core/serializers.py
# class TaskLogSerializer(serializers.ModelSerializer):
#     class Meta:
#     def get_task_status(self, obj):
# 
# Path: core/consts.py
# IN_PROGRESS = 'in_progress'
# COMPLETED = 'completed'
# STOPPED = 'stopped'
# FAIL = 'fail'
# WAIT = 'wait'
# STATUSES = [IN_PROGRESS, COMPLETED, STOPPED, FAIL, WAIT]
# STATUS_CHOICES = [(status, status) for status in STATUSES]
# NOT_RUN_STATUSES = [COMPLETED, STOPPED, FAIL]
# RUN_STATUSES = [IN_PROGRESS, WAIT]
# VERBOSE_CHOICES = (
#     ('', 'silent'),
#     ('v', 'standard'),
#     ('vv', 'detail'),
#     ('vvv', 'very detail'),
#     ('vvvv', 'all'),
# )


# Path: core/views/rest.py
from django.db.models import Count
from django.http import HttpResponse
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.authentication import TokenAuthentication
from prometheus_client import generate_latest
from core import models
from core import serializers
from core import consts



tokenBearer = TokenAuthentication
tokenBearer.keyword = 'Bearer'


class TaskLogs(ListAPIView):
    model = models.TaskLog
    serializer_class = serializers.TaskLogSerializer

    def get_queryset(self):
        last_log_id = self.request.GET.get('last_log_id', 0)
        return self.model.objects.filter(task_id=self.kwargs['task_id'], id__gt=last_log_id)
task_logs = TaskLogs.as_view()


class DjangoMetrics(APIView):
    authentication_classes = (tokenBearer,)

    def get(self, request):
        result = generate_latest().decode()
        return HttpResponse(result, content_type='text/plain; charset=utf-8')


class AnsibleManagerMetrics(APIView):
    authentication_classes = (tokenBearer,)

    def get(self, request):
        result = '# HELP ansible_manager_template_last_task_success show success or fail last task\n'
        result += '# TYPE ansible_manager_template_last_task_success gauge\n'

        for template in models.TaskTemplate.objects.exclude(cron=''):