aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/models.py
diff options
context:
space:
mode:
authorParker Berberian <pberberian@iol.unh.edu>2019-08-12 14:18:25 -0400
committerParker Berberian <pberberian@iol.unh.edu>2019-08-13 12:49:44 -0400
commitd78fcef6ec55dbaa225c6607bdac430539bf3f0b (patch)
tree653cc5a73b3f2508c8ec93bf3055a3d87497929d /src/api/models.py
parent3418c7a7baae772f1bb58e9a827c1e6198dbed54 (diff)
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 <pberberian@iol.unh.edu>
Diffstat (limited to 'src/api/models.py')
-rw-r--r--src/api/models.py34
1 files changed, 34 insertions, 0 deletions
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 = {}