From d78fcef6ec55dbaa225c6607bdac430539bf3f0b Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Mon, 12 Aug 2019 14:18:25 -0400 Subject: Adds Downtime Awareness This adds a Downtime model and relevant operations so that the dashboard knows when a lab is down for maintenance and can act accordingly. This change doesn't modify the front end at all, but it does pass relevant downtime info to the templates so that they can be updated in a future change. Change-Id: Idb88b15838b949f352f11a31a1fce9749d283d28 Signed-off-by: Parker Berberian --- src/api/models.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/api/models.py') diff --git a/src/api/models.py b/src/api/models.py index 1f708ae..682785b 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -13,6 +13,7 @@ from django.db import models from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 from django.urls import reverse +from django.utils import timezone import json import uuid @@ -30,6 +31,7 @@ from resource_inventory.models import ( ) from resource_inventory.idf_templater import IDFTemplater from resource_inventory.pdf_templater import PDFTemplater +from account.models import Downtime class JobStatus(object): @@ -67,6 +69,38 @@ class LabManager(object): def __init__(self, lab): self.lab = lab + def get_downtime(self): + return Downtime.objects.filter(start__lt=timezone.now(), end__gt=timezone.now(), lab=self.lab) + + def get_downtime_json(self): + downtime = self.get_downtime().first() # should only be one item in queryset + if downtime: + return { + "is_down": True, + "start": downtime.start, + "end": downtime.end, + "description": downtime.description + } + return {"is_down": False} + + def create_downtime(self, form): + """ + takes in a dictionary that describes the model. + { + "start": utc timestamp + "end": utc timestamp + "description": human text (optional) + } + For timestamp structure, https://docs.djangoproject.com/en/2.2/ref/forms/fields/#datetimefield + """ + Downtime.objects.create( + start=form.cleaned_data['start'], + end=form.cleaned_data['end'], + description=form.cleaned_data['description'], + lab=self.lab + ) + return self.get_downtime_json() + def update_host_remote_info(self, data, host_id): host = get_object_or_404(Host, labid=host_id, lab=self.lab) info = {} -- cgit 1.2.3-korg