]> git.proxmox.com Git - pve-manager.git/blobdiff - www/manager6/Utils.js
ui: refactor sortByPreviousUsage and nextFreeDisk
[pve-manager.git] / www / manager6 / Utils.js
index 4041c010882515568632d0a1a04fe8a41164ea10..8631a67c878390b532cb3ad1f20d796947cbe3dd 100644 (file)
@@ -1757,6 +1757,52 @@ Ext.define('PVE.Utils', {
 
        return true;
     },
+
+    sortByPreviousUsage: function(vmconfig, controllerList) {
+       if (!controllerList) {
+           controllerList = ['ide', 'virtio', 'scsi', 'sata'];
+       }
+       let usedControllers = {};
+       for (const type of Object.keys(PVE.Utils.diskControllerMaxIDs)) {
+           usedControllers[type] = 0;
+       }
+
+       for (const property of Object.keys(vmconfig)) {
+           if (property.match(PVE.Utils.bus_match) && !vmconfig[property].match(/media=cdrom/)) {
+               const foundController = property.match(PVE.Utils.bus_match)[1];
+               usedControllers[foundController]++;
+           }
+       }
+
+       let sortPriority = PVE.qemu.OSDefaults.getDefaults(vmconfig.ostype).busPriority;
+
+       let sortedList = Ext.clone(controllerList);
+       sortedList.sort(function(a, b) {
+           if (usedControllers[b] === usedControllers[a]) {
+               return sortPriority[b] - sortPriority[a];
+           }
+           return usedControllers[b] - usedControllers[a];
+       });
+
+       return sortedList;
+    },
+
+    nextFreeDisk: function(controllers, config) {
+       for (const controller of controllers) {
+           for (let i = 0; i < PVE.Utils.diskControllerMaxIDs[controller]; i++) {
+               let confid = controller + i.toString();
+               if (!Ext.isDefined(config[confid])) {
+                   return {
+                       controller,
+                       id: i,
+                       confid,
+                   };
+               }
+           }
+       }
+
+       return undefined;
+    },
 },
 
     singleton: true,