[pve-devel] [PATCH v2 manager 5/6] ui: CPUModelSelector: use API call for store

Stefan Reiter s.reiter at proxmox.com
Mon May 4 12:58:42 CEST 2020


CPU models are retrieved from the new /nodes/X/cpu call and ordered by
vendor to approximate the previous sort order (less change for accustomed
users).

With this, custom CPU models are now selectable via the GUI.

Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---

v2:
* Put vendor map and order map into PVE.Utils
* Add gettext for 'Custom'

I thought about the sorting method with the 'calculated' field Dominik
suggested, but I felt it just made the code a bit more confusing (since it
splits the ordering stuff across the file). Left it as is for now.

 www/manager6/Utils.js                 |  14 ++
 www/manager6/form/CPUModelSelector.js | 209 +++++++++-----------------
 2 files changed, 83 insertions(+), 140 deletions(-)

diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index 872b7c29..4301f00e 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -1454,6 +1454,20 @@ Ext.define('PVE.Utils', { utilities: {
 	    }
 	});
     },
+
+    cpu_vendor_map: {
+	'default': 'QEMU',
+	'AuthenticAMD': 'AMD',
+	'GenuineIntel': 'Intel'
+    },
+
+    cpu_vendor_order: {
+	"AMD": 1,
+	"Intel": 2,
+	"QEMU": 3,
+	"Host": 4,
+	"_default_": 5, // includes custom models
+    },
 },
 
     singleton: true,
diff --git a/www/manager6/form/CPUModelSelector.js b/www/manager6/form/CPUModelSelector.js
index 1d28ee88..deef23fb 100644
--- a/www/manager6/form/CPUModelSelector.js
+++ b/www/manager6/form/CPUModelSelector.js
@@ -1,9 +1,19 @@
+Ext.define('PVE.data.CPUModel', {
+    extend: 'Ext.data.Model',
+    fields: [
+	{name: 'name'},
+	{name: 'vendor'},
+	{name: 'custom'},
+	{name: 'displayname'}
+    ]
+});
+
 Ext.define('PVE.form.CPUModelSelector', {
     extend: 'Proxmox.form.ComboGrid',
     alias: ['widget.CPUModelSelector'],
 
-    valueField: 'value',
-    displayField: 'value',
+    valueField: 'name',
+    displayField: 'displayname',
 
     emptyText: Proxmox.Utils.defaultText + ' (kvm64)',
     allowBlank: true,
@@ -19,157 +29,76 @@ Ext.define('PVE.form.CPUModelSelector', {
 	columns: [
 	    {
 		header: gettext('Model'),
-		dataIndex: 'value',
+		dataIndex: 'displayname',
 		hideable: false,
 		sortable: true,
-		flex: 2
+		flex: 3
 	    },
 	    {
 		header: gettext('Vendor'),
 		dataIndex: 'vendor',
 		hideable: false,
 		sortable: true,
-		flex: 1
+		flex: 2
 	    }
 	],
-	width: 320
+	width: 360
     },
 
     store: {
-	fields: [ 'value', 'vendor' ],
-	data: [
-	    {
-		value: 'athlon',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'phenom',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'Opteron_G1',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'Opteron_G2',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'Opteron_G3',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'Opteron_G4',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'Opteron_G5',
-		vendor: 'AMD'
-	    },
-	    {
-		value: 'EPYC',
-		vendor: 'AMD'
-	    },
-	    {
-		value: '486',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'core2duo',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'coreduo',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'pentium',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'pentium2',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'pentium3',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Conroe',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Penryn',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Nehalem',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Westmere',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'SandyBridge',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'IvyBridge',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Haswell',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Haswell-noTSX',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Broadwell',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Broadwell-noTSX',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Skylake-Client',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Skylake-Server',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'Cascadelake-Server',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'KnightsMill',
-		vendor: 'Intel'
-	    },
-	    {
-		value: 'kvm32',
-		vendor: 'QEMU'
-	    },
-	    {
-		value: 'kvm64',
-		vendor: 'QEMU'
-	    },
-	    {
-		value: 'qemu32',
-		vendor: 'QEMU'
-	    },
-	    {
-		value: 'qemu64',
-		vendor: 'QEMU'
-	    },
-	    {
-		value: 'host',
-		vendor: 'Host'
+	autoLoad: true,
+	model: 'PVE.data.CPUModel',
+	proxy: {
+	    type: 'proxmox',
+	    url: '/api2/json/nodes/localhost/cpu'
+	},
+	sorters: [
+	    {
+		sorterFn: function(recordA, recordB) {
+		    let a = recordA.data;
+		    let b = recordB.data;
+
+		    let vendorOrder = PVE.Utils.cpu_vendor_order;
+		    let orderA = vendorOrder[a.vendor] || vendorOrder['_default_'];
+		    let orderB = vendorOrder[b.vendor] || vendorOrder['_default_'];
+
+		    if (orderA > orderB) {
+			return 1;
+		    } else if (orderA < orderB) {
+			return -1;
+		    }
+
+		    // Within same vendor, sort alphabetically
+		    return a.name.localeCompare(b.name);
+		},
+		direction: 'ASC'
+	    }
+	],
+	listeners: {
+	    load: function(store, records, success) {
+		if (success) {
+		    records.forEach(rec => {
+			rec.data.displayname = rec.data.name.replace(/^custom-/, '');
+
+			let vendor = rec.data.vendor;
+
+			if (rec.data.name === 'host') {
+			    vendor = 'Host';
+			}
+
+			// We receive vendor names as given to QEMU as CPUID
+			vendor = PVE.Utils.cpu_vendor_map[vendor] || vendor;
+
+			if (rec.data.custom) {
+			    vendor = gettext('Custom') + ` (${vendor})`;
+			}
+
+			rec.data.vendor = vendor;
+		    });
+
+		    store.sort();
+		}
 	    }
-	]
+	}
     }
 });
-- 
2.20.1





More information about the pve-devel mailing list