#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import logging
import simplejson
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import Http404
from django.http import HttpResponseForbidden
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from ..experiments.models import Experiment
from . import state
from .models import Environment
from .models import Queue
from .models import Worker
logger = logging.getLogger(__name__)
# ------------------------------------------------
[docs]@login_required
def scheduler(request):
if not (request.user.is_superuser):
return HttpResponseForbidden()
# data for the cache plot
cache = state.cache()
cache_chart_data = [
dict(
label="Used (%d%%)"
% round(
100 * float(cache["size-in-megabytes"]) / cache["capacity-in-megabytes"]
),
value=cache["size-in-megabytes"],
color="#F7464A",
highlight="#FF5A5E",
),
dict(
label="Free (%d%%)"
% round(
100
* float(cache["capacity-in-megabytes"] - cache["size-in-megabytes"])
/ cache["capacity-in-megabytes"]
),
value=cache["capacity-in-megabytes"] - cache["size-in-megabytes"],
color="#46BFBD",
highlight="#5AD3D1",
),
]
cache_gb = int(cache["capacity-in-megabytes"] / 1024.0)
return render(
request,
"backend/scheduler.html",
dict(
jobs=state.jobs(),
experiments=state.experiments(),
workers=Worker.objects.order_by("-active", "name"),
queues=Queue.objects.order_by("memory_limit", "max_slots_per_user"),
cache_chart_data=simplejson.dumps(cache_chart_data),
cache_gb=cache_gb,
),
)
# ------------------------------------------------
[docs]def environment(request, name, version):
"""Displays a single database"""
# Retrieve the data base
environment = get_object_or_404(
Environment, name__iexact=name, version__iexact=version
)
if not environment.accessibility_for(request.user)[0]:
raise Http404()
# Render the page
return render(request, "backend/environment.html", {"environment": environment})
# ----------------------------------------------------------
[docs]def list_environments(request):
"""Displays all accessible (active and inactive) environments"""
objects = (
Environment.objects.for_user(request.user, True)
.filter(active=True)
.order_by("-creation_date")
)
# Render the page
return render(
request,
"backend/environment_list.html",
dict(objects=objects, author=request.user, owner=True),
)
# ----------------------------------------------------------
[docs]@login_required
def cancel_all_experiments(request):
if not (request.user.is_superuser):
return HttpResponseForbidden()
qs = Experiment.objects.filter(
status__in=(Experiment.RUNNING, Experiment.SCHEDULED)
)
counter = qs.count()
for xp in qs:
xp.cancel()
messages.success(request, "Successfuly cancelled %d experiments" % counter)
return HttpResponseRedirect(reverse("backend:scheduler"))
# ----------------------------------------------------------
[docs]@login_required
def update_workers(request):
if not (request.user.is_superuser):
return HttpResponseForbidden()
qs = Worker.objects.all()
counter = qs.count()
qs.update(update=True)
messages.success(request, "Requested %d workers for updates" % counter)
return HttpResponseRedirect(reverse("backend:scheduler") + "#workers")