fix last run column
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import pytz
|
||||
import random
|
||||
import string
|
||||
import datetime as dt
|
||||
@@ -122,6 +123,15 @@ class AutomatedTask(BaseAuditModel):
|
||||
days = ",".join(ret)
|
||||
return f"{days} at {run_time_nice}"
|
||||
|
||||
@property
|
||||
def last_run_as_timezone(self):
|
||||
if self.last_run is not None and self.agent is not None:
|
||||
return self.last_run.astimezone(
|
||||
pytz.timezone(self.agent.timezone)
|
||||
).strftime("%b-%d-%Y - %H:%M")
|
||||
|
||||
return self.last_run
|
||||
|
||||
@staticmethod
|
||||
def generate_task_name():
|
||||
chars = string.ascii_letters
|
||||
|
||||
@@ -13,18 +13,7 @@ class TaskSerializer(serializers.ModelSerializer):
|
||||
|
||||
assigned_check = CheckSerializer(read_only=True)
|
||||
schedule = serializers.ReadOnlyField()
|
||||
last_run = serializers.SerializerMethodField()
|
||||
|
||||
def get_last_run(self, obj):
|
||||
if self.context and obj.last_run is not None:
|
||||
if self.context["agent_tz"] is not None:
|
||||
agent_tz = pytz.timezone(self.context["agent_tz"])
|
||||
else:
|
||||
agent_tz = self.context["default_tz"]
|
||||
|
||||
return obj.last_run.astimezone(agent_tz).strftime("%b-%d-%Y - %H:%M")
|
||||
|
||||
return obj.last_run
|
||||
last_run = serializers.ReadOnlyField(source="last_run_as_timezone")
|
||||
|
||||
class Meta:
|
||||
model = AutomatedTask
|
||||
|
||||
@@ -2,6 +2,7 @@ import base64
|
||||
import string
|
||||
import os
|
||||
import json
|
||||
import pytz
|
||||
import zlib
|
||||
from statistics import mean
|
||||
|
||||
@@ -177,6 +178,15 @@ class Check(BaseAuditModel):
|
||||
if self.check_type == "cpuload" or self.check_type == "memory":
|
||||
return ", ".join(str(f"{x}%") for x in self.history[-6:])
|
||||
|
||||
@property
|
||||
def last_run_as_timezone(self):
|
||||
if self.last_run is not None and self.agent is not None:
|
||||
return self.last_run.astimezone(
|
||||
pytz.timezone(self.agent.timezone)
|
||||
).strftime("%b-%d-%Y - %H:%M")
|
||||
|
||||
return self.last_run
|
||||
|
||||
@property
|
||||
def non_editable_fields(self):
|
||||
return [
|
||||
@@ -199,6 +209,10 @@ class Check(BaseAuditModel):
|
||||
"parent_check",
|
||||
"managed_by_policy",
|
||||
"overriden_by_policy",
|
||||
"created_by",
|
||||
"created_time",
|
||||
"modified_by",
|
||||
"modified_time",
|
||||
]
|
||||
|
||||
def handle_checkv2(self, data):
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import pytz
|
||||
import validators as _v
|
||||
|
||||
from rest_framework import serializers
|
||||
@@ -19,7 +18,7 @@ class CheckSerializer(serializers.ModelSerializer):
|
||||
readable_desc = serializers.ReadOnlyField()
|
||||
script = ScriptSerializer(read_only=True)
|
||||
assigned_task = serializers.SerializerMethodField()
|
||||
last_run = serializers.SerializerMethodField()
|
||||
last_run = serializers.ReadOnlyField(source="last_run_as_timezone")
|
||||
history_info = serializers.ReadOnlyField()
|
||||
|
||||
## Change to return only array of tasks after 9/25/2020
|
||||
@@ -31,17 +30,6 @@ class CheckSerializer(serializers.ModelSerializer):
|
||||
else:
|
||||
return AssignedTaskField(tasks, many=True).data
|
||||
|
||||
def get_last_run(self, obj):
|
||||
if self.context and obj.last_run is not None:
|
||||
if self.context["agent_tz"] is not None:
|
||||
agent_tz = pytz.timezone(self.context["agent_tz"])
|
||||
else:
|
||||
agent_tz = self.context["default_tz"]
|
||||
|
||||
return obj.last_run.astimezone(agent_tz).strftime("%b-%d-%Y - %H:%M")
|
||||
|
||||
return obj.last_run
|
||||
|
||||
class Meta:
|
||||
model = Check
|
||||
fields = "__all__"
|
||||
@@ -60,12 +48,11 @@ class CheckSerializer(serializers.ModelSerializer):
|
||||
.filter(check_type="diskspace")
|
||||
.exclude(managed_by_policy=True)
|
||||
)
|
||||
if checks:
|
||||
for check in checks:
|
||||
if val["disk"] in check.disk:
|
||||
raise serializers.ValidationError(
|
||||
f"A disk check for Drive {val['disk']} already exists!"
|
||||
)
|
||||
for check in checks:
|
||||
if val["disk"] in check.disk:
|
||||
raise serializers.ValidationError(
|
||||
f"A disk check for Drive {val['disk']} already exists!"
|
||||
)
|
||||
|
||||
# ping checks
|
||||
if check_type == "ping":
|
||||
|
||||
@@ -2,7 +2,7 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("checks/", views.GetAddCheck.as_view()),
|
||||
path("checks/", views.AddCheck.as_view()),
|
||||
path("<int:pk>/check/", views.GetUpdateDeleteCheck.as_view()),
|
||||
path("<pk>/loadchecks/", views.load_checks),
|
||||
path("getalldisks/", views.get_disks_for_policies),
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import pytz
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework.views import APIView
|
||||
@@ -11,7 +10,6 @@ from automation.models import Policy
|
||||
|
||||
from .models import Check
|
||||
from scripts.models import Script
|
||||
from core.models import CoreSettings
|
||||
|
||||
from .serializers import CheckSerializer
|
||||
|
||||
@@ -24,11 +22,7 @@ from automation.tasks import (
|
||||
)
|
||||
|
||||
|
||||
class GetAddCheck(APIView):
|
||||
def get(self, request):
|
||||
checks = Check.objects.all()
|
||||
return Response(CheckSerializer(checks, many=True).data)
|
||||
|
||||
class AddCheck(APIView):
|
||||
def post(self, request):
|
||||
policy = None
|
||||
agent = None
|
||||
@@ -190,13 +184,8 @@ def run_checks(request, pk):
|
||||
|
||||
@api_view()
|
||||
def load_checks(request, pk):
|
||||
agent = get_object_or_404(Agent, pk=pk)
|
||||
ctx = {
|
||||
"default_tz": pytz.timezone(CoreSettings.objects.first().default_time_zone),
|
||||
"agent_tz": agent.time_zone,
|
||||
}
|
||||
checks = Check.objects.filter(agent__pk=pk)
|
||||
return Response(CheckSerializer(checks, many=True, context=ctx).data)
|
||||
return Response(CheckSerializer(checks, many=True).data)
|
||||
|
||||
|
||||
@api_view()
|
||||
|
||||
Reference in New Issue
Block a user