this.inputs = [];
this.graph_neighbors = neighbors;
this.filter_items = items;
+ this.currentLab = null;
+ this.available_resources = {};
this.result = {};
this.dropdown_count = 0;
this.make_selection(initial);
}
- make_selection( initial_data ){
+ make_selection(initial_data){
if(!initial_data || jQuery.isEmptyObject(initial_data))
return;
for(let item_class in initial_data) {
const toCheck = [root];
while(toCheck.length > 0){
const node = toCheck.pop();
+
if(!node['marked']) {
continue; //already visited, just continue
}
+
node['marked'] = false; //mark as visited
if(node['follow'] || node == root){ //add neighbors if we want to follow this node
const neighbors = this.graph_neighbors[node.id];
node['selected'] = true;
elem.classList.remove('bg-white', 'not-allowed', 'bg-light');
elem.classList.add('selected_node');
+
+ if(node['class'] == 'resource')
+ this.reserveResource(node);
+
}
clear(node) {
elem.classList.add('not-allowed', 'bg-light');
}
+ labCheck(node){
+ // if lab is not already selected update available resources
+ if(!node['selected']) {
+ this.currentLab = node;
+ this.available_resources = JSON.parse(node['available_resources']);
+ this.updateAvailibility();
+ } else {
+ // a lab is already selected, clear already selected resources
+ if(confirm('Unselecting a lab will reset all selected resources, are you sure?'))
+ location.reload();
+ }
+ }
+
+ updateAvailibility() {
+ const lab_resources = this.graph_neighbors[this.currentLab.id];
+
+ // need to loop through and update all quantities
+ for(let i in lab_resources) {
+ const resource_node = this.filter_items[lab_resources[i]];
+ const required_resources = JSON.parse(resource_node['required_resources']);
+ let elem = document.getElementById(resource_node.id).getElementsByClassName("grid-item-description")[0];
+ let leastAvailable = 100;
+ let currCount;
+ let quantityDescription;
+ let quantityNode;
+
+ // console.log(this.available_resources);
+ for(let resource in required_resources) {
+ currCount = Math.floor(this.available_resources[resource] / required_resources[resource]);
+ if(currCount < leastAvailable)
+ leastAvailable = currCount;
+
+ if(!currCount || currCount < 0) {
+ leastAvailable = 0
+ break;
+ }
+ }
+
+ if (elem.children[0]){
+ elem.removeChild(elem.children[0]);
+ }
+
+ quantityDescription = '<br> Quantity Currently Available: ' + leastAvailable;
+ quantityNode = document.createElement('P');
+ if (leastAvailable > 0) {
+ quantityDescription = quantityDescription.fontcolor('green');
+ } else {
+ quantityDescription = quantityDescription.fontcolor('red');
+ }
+
+ quantityNode.innerHTML = quantityDescription;
+ elem.appendChild(quantityNode)
+ }
+ }
+
+ reserveResource(node){
+ const required_resources = JSON.parse(node['required_resources']);
+
+ for(let resource in required_resources){
+ this.available_resources[resource] -= required_resources[resource];
+ }
+
+ this.updateAvailibility();
+ }
+
+ releaseResource(node){
+ const required_resources = JSON.parse(node['required_resources']);
+
+ for(let resource in required_resources){
+ this.available_resources[resource] += required_resources[resource];
+ }
+
+ this.updateAvailibility();
+ }
+
processClick(id){
const node = this.filter_items[id];
if(!node['selectable'])
return;
+ // If they are selecting a lab, update accordingly
+ if (node['class'] == 'lab')
+ this.labCheck(node);
+
+ // Can only select a resource if a lab is selected
+ if (!this.currentLab) {
+ alert('You must select a lab before selecting a resource');
+ return;
+ }
+
if(node['multiple']){
return this.processClickMultiple(node);
} else {
this.select(node);
} else {
this.clear(node);
+ this.releaseResource(node); // can't do this in clear since clear removes border
}
this.process(node);
this.updateResult(node);
const parent = div.parentNode;
div.parentNode.removeChild(div);
this.result[node.class][node.id]['count']--;
+ this.releaseResource(node); // This can't be done on clear b/c clear removes border
//checks if we have removed last item in class
if(this.result[node.class][node.id]['count'] == 0){
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
+# Copyright (c) 2020 Sawyer Bergeron, Sean Smith, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
from resource_inventory.models import (
OPNFVRole,
Installer,
- Scenario,
+ Scenario
)
from resource_inventory.resource_manager import ResourceManager
from booking.lib import get_user_items, get_user_field_opts
'selectable': true,
'follow': multiple_hosts,
'multiple': false,
- 'class': 'lab'
+ 'class': 'lab',
+ 'available_resources': json.dumps(lab.get_available_resources())
}
+
items[lab_node['id']] = lab_node
neighbors[lab_node['id']] = []
labs[lab_node['id']] = lab_node
'selectable': true,
'follow': false,
'multiple': multiple_hosts,
- 'class': 'resource'
+ 'class': 'resource',
+ 'required_resources': json.dumps(template.get_required_resources())
}
+
if multiple_hosts:
resource_node['values'] = [] # place to store multiple values
+
items[resource_node['id']] = resource_node
neighbors[lab_node['id']].append(resource_node['id'])
+
if resource_node['id'] not in neighbors:
neighbors[resource_node['id']] = []
+
neighbors[resource_node['id']].append(lab_node['id'])
resources[resource_node['id']] = resource_node
'neighbors': neighbors,
'filter_items': items
}
+
return context