<commit_before>from django.contrib import admin

from harvest.models import (
    Job, JobLog,Process
)
from borg.admin import site
from harvest.jobstates import JobState,JobStateOutcome,Failed,Completed
from harvest.harveststates import Waiting


def harvest(modeladmin, request, queryset):
    for output in queryset:
        output.harvest()
harvest.short_description = "Harvest and update selected outputs"

class JobAdmin(admin.ModelAdmin):
    readonly_fields = ("id","batch_id","_publish","job_type", "state","job_action" ,"previous_state", "_message","retry_times","last_execution_end_time", "created", "launched", "finished","_table_data","_style_file","sync_status","job_logs")
    list_display = ("id","batch_id", "_publish","job_type", "state", "created", "launched", "finished","job_action","sync_status","job_logs")
    search_fields = ["publish__name","batch_id"]
    actions = None

    def _message(self,o):
        if o.message:
            return "<p style='white-space:pre'>" + o.message + "</p>"
        else:
            return ''

    _message.allow_tags = True
    _message.short_description = "Message"

    def job_action(self,o):
        job_state = JobState.get_jobstate(o.state);
        if not job_state or job_state.is_end_state:
            return ""
        value = "";
        if job_state.is_interactive_state :
            if o.user_action and o.user_action == JobStateOutcome.approved_by_custodian:
                value = "Approved"
            elif not o.user_action or o.user_action != JobStateOutcome.cancelled_by_custodian:
                value = "<a href='/job/" + str(o.id) +"/approve?job_state=" + o.state + "'>Approve</a>"
        if job_state.cancellable:
            if o.user_action and o.user_action == JobStateOutcome.cancelled_by_custodian:
                if value:
                    value += " | Cancelled"
                else:
                    value = "Cancelled"
            else:
                if value:
                    value += " | <a href='/job/" + str(o.id) +"/cancel?job_state=" + o.state + "'>Cancel</a>"
                else:
                    value = "<a href='/job/" + str(o.id) +"/cancel?job_state=" + o.state + "'>Cancel</a>"

        return value

    job_action.short_description = "action"
    job_action.allow_tags = True

    def _publish(self,o):
        return str(o.publish)
    _publish.short_description = "Publish"
    _publish.admin_order_field = "publish__name"


    def job_logs(self,o):
        return "<a href='/harvest/joblog/?q={0}'>Logs</a>".format(o.id)
    job_logs.allow_tags = True
    job_logs.short_description = "Logs"

    def sync_status(self,o):
        if o.state == Completed.instance().name and o.launched:
            return "<a href='/monitor/publishsyncstatus/?q={0}'>Sync status</a>".format(o.id)
        else:
            return ""
    sync_status.allow_tags = True
    sync_status.short_description = "Logs"

    def has_add_permission(self,request):
        return False

    def has_delete_permission(self,request,obj=None):
        return False


    def _style_file(self,o):
        if o.style_file:
            return "<a href='{0}' target='_blank'>Download</a>".format(o.style_file.url)
        else:
            return ""
    _style_file.short_description = "Style file"

    def _table_data(self,o):
        if o.pgdump_file:
            return "<a href='{0}' target='_blank'>Download</a>".format(o.pgdump_file.url)
        else:
            return ''
    _table_data.allow_tags = True
    _table_data.short_description = "Table Data"

    class Media:
        js = ('/static/js/admin-model-readonly.js',)

class FailingJobAdmin(JobAdmin):
    def get_queryset(self,request):
        qs = super(FailingJobAdmin,self).get_queryset(request)
        return qs.filter(state__in = JobState.all_failed_class_names)

class FailingJob(Job):
    class Meta:
        proxy = True
        verbose_name="Job (Failing)"
        verbose_name_plural="Jobs (Failing)"

class RunningJobAdmin(JobAdmin):
    def get_queryset(self,request):
        qs = super(RunningJobAdmin,self).get_queryset(request)
        return qs.exclude(state__in = [Failed.instance().name,Completed.instance().name])

class RunningJob(Job):
    class Meta:
        proxy = True
        verbose_name="Job (Running)"
        verbose_name_plural="Jobs (Running)"

class EffectiveJobAdmin(JobAdmin):
    def get_queryset(self,request):
        qs = super(EffectiveJobAdmin,self).get_queryset(request)
        return qs.exclude(state__in = [Failed.instance().name,Completed.instance().name],launched=None)

class EffectiveJob(Job):
    class Meta:
        proxy = True
        verbose_name="Job (Effective)"
        verbose_name_plural="Jobs (Effective)"

class JobLogAdmin(admin.ModelAdmin):
    readonly_fields = ("id","_job", "state", "outcome", "_message", "next_state", "start_time", "end_time")
    list_display = ("id","_job","state", "outcome", "_message", "start_time", "end_time")
    search_fields = ["job_id"]

    actions = None

    def _message(self,o):
        if o.message:
            return "<p style='white-space:pre'>" + o.message + "</p>"
        else:
            return ''

    _message.allow_tags = True
    _message.short_description = "Message"
    _message.admin_order_field = "message"

    def _job(self,o):
        if o.job:
            return o.job.pk
        else:
            return ''
    _job.short_description = "Job"
    _job.admin_order_field = "job__pk"

    def has_add_permission(self,request):
        return False

    def has_delete_permission(self,request,obj=None):
        return False

    def get_search_results(self,request,queryset,search_term):
        try:
            #import ipdb; ipdb.set_trace()
            job_id = int(search_term)
            return self.model.objects.filter(job_id = job_id).order_by("start_time"),False
        except:
            return super(JobLogAdmin,self).get_search_results(request,queryset,search_term)

    class Media:
        js = ('/static/js/admin-model-readonly.js',)

class ProcessAdmin(admin.ModelAdmin):
    readonly_fields = ("id","name","desc","server","pid","status","next_scheduled_time","last_starttime","last_endtime","_last_message")<commit_msg>Return empty queryset when query job logs with incorrect job id.<commit_after>from django.contrib import admin

from harvest.models import (
    Job, JobLog,Process
)
from borg.admin import site
from harvest.jobstates import JobState,JobStateOutcome,Failed,Completed
from harvest.harveststates import Waiting


def harvest(modeladmin, request, queryset):
    for output in queryset:
        output.harvest()
harvest.short_description = "Harvest and update selected outputs"

class JobAdmin(admin.ModelAdmin):
    readonly_fields = ("id","batch_id","_publish","job_type", "state","job_action" ,"previous_state", "_message","retry_times","last_execution_end_time", "created", "launched", "finished","_table_data","_style_file","sync_status","job_logs")
    list_display = ("id","batch_id", "_publish","job_type", "state", "created", "launched", "finished","job_action","sync_status","job_logs")
    search_fields = ["publish__name","batch_id"]
    actions = None

    def _message(self,o):
        if o.message:
            return "<p style='white-space:pre'>" + o.message + "</p>"
        else:
            return ''

    _message.allow_tags = True
    _message.short_description = "Message"

    def job_action(self,o):
        job_state = JobState.get_jobstate(o.state);
        if not job_state or job_state.is_end_state:
            return ""
        value = "";
        if job_state.is_interactive_state :
            if o.user_action and o.user_action == JobStateOutcome.approved_by_custodian:
                value = "Approved"
            elif not o.user_action or o.user_action != JobStateOutcome.cancelled_by_custodian:
                value = "<a href='/job/" + str(o.id) +"/approve?job_state=" + o.state + "'>Approve</a>"
        if job_state.cancellable:
            if o.user_action and o.user_action == JobStateOutcome.cancelled_by_custodian:
                if value:
                    value += " | Cancelled"
                else:
                    value = "Cancelled"
            else:
                if value:
                    value += " | <a href='/job/" + str(o.id) +"/cancel?job_state=" + o.state + "'>Cancel</a>"
                else:
                    value = "<a href='/job/" + str(o.id) +"/cancel?job_state=" + o.state + "'>Cancel</a>"

        return value

    job_action.short_description = "action"
    job_action.allow_tags = True

    def _publish(self,o):
        return str(o.publish)
    _publish.short_description = "Publish"
    _publish.admin_order_field = "publish__name"


    def job_logs(self,o):
        return "<a href='/harvest/joblog/?q={0}'>Logs</a>".format(o.id)
    job_logs.allow_tags = True
    job_logs.short_description = "Logs"

    def sync_status(self,o):
        if o.state == Completed.instance().name and o.launched:
            return "<a href='/monitor/publishsyncstatus/?q={0}'>Sync status</a>".format(o.id)
        else:
            return ""
    sync_status.allow_tags = True
    sync_status.short_description = "Logs"

    def has_add_permission(self,request):
        return False

    def has_delete_permission(self,request,obj=None):
        return False


    def _style_file(self,o):
        if o.style_file:
            return "<a href='{0}' target='_blank'>Download</a>".format(o.style_file.url)
        else:
            return ""
    _style_file.short_description = "Style file"

    def _table_data(self,o):
        if o.pgdump_file:
            return "<a href='{0}' target='_blank'>Download</a>".format(o.pgdump_file.url)
        else:
            return ''
    _table_data.allow_tags = True
    _table_data.short_description = "Table Data"

    class Media:
        js = ('/static/js/admin-model-readonly.js',)

class FailingJobAdmin(JobAdmin):
    def get_queryset(self,request):
        qs = super(FailingJobAdmin,self).get_queryset(request)
        return qs.filter(state__in = JobState.all_failed_class_names)

class FailingJob(Job):
    class Meta:
        proxy = True
        verbose_name="Job (Failing)"
        verbose_name_plural="Jobs (Failing)"

class RunningJobAdmin(JobAdmin):
    def get_queryset(self,request):
        qs = super(RunningJobAdmin,self).get_queryset(request)
        return qs.exclude(state__in = [Failed.instance().name,Completed.instance().name])

class RunningJob(Job):
    class Meta:
        proxy = True
        verbose_name="Job (Running)"
        verbose_name_plural="Jobs (Running)"

class EffectiveJobAdmin(JobAdmin):
    def get_queryset(self,request):
        qs = super(EffectiveJobAdmin,self).get_queryset(request)
        return qs.exclude(state__in = [Failed.instance().name,Completed.instance().name],launched=None)

class EffectiveJob(Job):
    class Meta:
        proxy = True
        verbose_name="Job (Effective)"
        verbose_name_plural="Jobs (Effective)"

class JobLogAdmin(admin.ModelAdmin):
    readonly_fields = ("id","_job", "state", "outcome", "_message", "next_state", "start_time", "end_time")
    list_display = ("id","_job","state", "outcome", "_message", "start_time", "end_time")
    search_fields = ["job_id"]

    actions = None

    def _message(self,o):
        if o.message:
            return "<p style='white-space:pre'>" + o.message + "</p>"
        else:
            return ''

    _message.allow_tags = True
    _message.short_description = "Message"
    _message.admin_order_field = "message"

    def _job(self,o):
        if o.job:
            return o.job.pk
        else:
            return ''
    _job.short_description = "Job"
    _job.admin_order_field = "job__pk"

    def has_add_permission(self,request):
        return False

    def has_delete_permission(self,request,obj=None):
        return False

    def get_search_results(self,request,queryset,search_term):
        try:
            #import ipdb; ipdb.set_trace()
            job_id = int(search_term)
            return self.model.objects.filter(job_id = job_id).order_by("start_time"),False
        except:
            if search_term:
                return (self.model.objects.none(),False)
            else:
                return super(JobLogAdmin,self).get_search_results(request,queryset,search_term)


    class Media:
        js = ('/static/js/admin-model-readonly.js',)

class ProcessAdmin(admin.ModelAdmin):
    readonly_fields = ("id","name","desc","server","pid","status","next_scheduled_time","last_starttime","last_endtime","_last_message")
{'fraction_non_alphanumeric': 0.13113065525711115, 'fraction_numerical': 0.0006338641945963078, 'mean_word_length': 2.9065304859176724, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3899419', 'n_tokens_mistral': 4000, 'n_tokens_neox': 3892, 'n_words': 720}