From 6ae24661575d0efc2821c035f9a1b86d66549133 Mon Sep 17 00:00:00 2001 From: katmastt Date: Thu, 19 Oct 2023 10:39:14 +0000 Subject: [PATCH] Sorted vm selections for 24/7 and on-demand machines projects --- models/MachineComputeRequest.php | 18 ++++++++++-------- models/ServiceRequest.php | 10 ++++++++-- views/project/new_machine_compute_request.php | 2 +- views/project/new_service_request.php | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/models/MachineComputeRequest.php b/models/MachineComputeRequest.php index f21f45a..d45a8d8 100644 --- a/models/MachineComputeRequest.php +++ b/models/MachineComputeRequest.php @@ -89,7 +89,16 @@ public function init() $flavors=$response->data['flavors']; // print_r($flavors); // exit(0); - usort($flavors, "self::cmp"); + + //sort by cpu. If cpu is the same, sort by ram. If ram is the same sort by disk + usort($flavors, function ($a, $b) { + if ($a['vcpus'] !== $b['vcpus']) { + return $a['vcpus'] - $b['vcpus']; + } elseif($a['ram'] !== $b['ram']) { + return $a['ram'] - $b['ram']; + } + return $a['disk'] - $b['disk']; + }); foreach ($flavors as $flavor) { @@ -120,9 +129,6 @@ public function init() $this->flavourIdName[$id]=$name; } - - asort($this->flavours); - } /** * {@inheritdoc} @@ -391,8 +397,4 @@ public function getDiff($other) { return $diff; } - - public function cmp($a, $b){ - return strcmp($a['vcpus'], $b['vcpus']); - } } diff --git a/models/ServiceRequest.php b/models/ServiceRequest.php index 5d48bc6..d027d6f 100644 --- a/models/ServiceRequest.php +++ b/models/ServiceRequest.php @@ -162,6 +162,14 @@ public function init() if($flag) { $flavors=$response->data['flavors']; + + //sort by cpu. If cpu is the same, sort by ram + usort($flavors, function ($a, $b) { + if ($a['vcpus'] == $b['vcpus']) { + return $a['ram'] - $b['ram']; + } + return $a['vcpus'] - $b['vcpus']; + }); foreach ($flavors as $flavor) { @@ -204,7 +212,6 @@ public function init() } // $this->flavour=(!empty($this->vm_flavour)) ? $this->flavourIdName[$this->vm_flavour] : ''; - asort($this->flavours); } // print_r($this->vm_flavour); // print_r($this->flavour); @@ -575,5 +582,4 @@ public function getDiff($other) { return $diff; } - } diff --git a/views/project/new_machine_compute_request.php b/views/project/new_machine_compute_request.php index e6f850b..7c0e090 100644 --- a/views/project/new_machine_compute_request.php +++ b/views/project/new_machine_compute_request.php @@ -99,7 +99,7 @@
 
- field($service,'flavour')->dropDownList(array_reverse($service->flavours))->label($flavour_label)?> + field($service,'flavour')->dropDownList($service->flavours)->label($flavour_label)?> field($service,'num_of_vms')->dropDownList($num_vms_dropdown)?>
diff --git a/views/project/new_service_request.php b/views/project/new_service_request.php index 10793d1..e9667fc 100644 --- a/views/project/new_service_request.php +++ b/views/project/new_service_request.php @@ -126,7 +126,7 @@
 
- field($service,'flavour')->dropDownList(array_reverse($service->flavours))->label($flavour_label)?> + field($service,'flavour')->dropDownList($service->flavours)->label($flavour_label)?>