views.py 2.73 KB
from django.shortcuts import render
from common.models import Sequence, Schedule
from django.contrib.auth.decorators import login_required
from scheduler.simulator import Simulator
from operator import attrgetter

SIMULATION_FILE = 'file:./scheduler/sequences_cador.html'

def retrieve_schedule(request):
    if (len(Schedule.objects.all()) > 0):                       # checking if the schedule is empty
        schedule = Schedule.objects.order_by("-created")[0]     # Sorting Schedule
        shs_list = schedule.shs.order_by("tsp")                 # getting all the schedule has sequences references
        sequences = [(shs.sequence, shs) for shs in shs_list]   # getting all sequences

        nb_scheduled_sequences = len(shs_list)
        executed_sequences = len([shs for shs in shs_list if shs.status == Sequence.EXECUTED])
    else: # if empty set everything to 0 / None (variables are checked in src/templates/scheduler/current_schedule.html)
        schedule = None
        sequences = None
        shs_list = None
        nb_scheduled_sequences = 0
        executed_sequences = 0
    if request.user.is_authenticated:
        return render(request, 'scheduler/retrieve_schedule.html', {'sequences' : sequences, 'schedule' : schedule, 'nb_scheduled_sequences' : nb_scheduled_sequences, 'executed_sequences' : executed_sequences, 'base_template' : "base.html"})
    return render(request, 'scheduler/retrieve_schedule.html', {'sequences' : sequences, 'schedule' : schedule, 'nb_scheduled_sequences' : nb_scheduled_sequences, 'executed_sequences' : executed_sequences, 'base_template' : "base.html"})


def current_schedule(request):
    if request.user.is_authenticated:
        return render(request, 'scheduler/current_schedule.html', {'base_template' : "base.html"})
    return render(request, 'scheduler/current_schedule.html', {'base_template' : "base.html"})

def schedule_simulation(request):
    simulator = Simulator()
    schedule, sequences = simulator.simulate(SIMULATION_FILE)

    nb_scheduled_sequences = len([sequence for sequence in sequences if sequence[1].status != Sequence.PLANNED])
    executed_sequences = len([sequence for sequence in sequences if sequence[1].status == Sequence.EXECUTED])


    sequences.sort(key=lambda seq: seq[1].tsp)
    if request.user.is_authenticated:
        return render(request, 'scheduler/current_schedule.html', {'sequences' : sequences, 'schedule' : schedule, 'nb_scheduled_sequences' : nb_scheduled_sequences, 'executed_sequences' : executed_sequences, 'base_template' : "base.html"})
    return render(request, 'scheduler/current_schedule.html', {'sequences' : sequences, 'schedule' : schedule, 'nb_scheduled_sequences' : nb_scheduled_sequences, 'executed_sequences' : executed_sequences, 'base_template' : "base.html"})