[pve-devel] [PATCH manager v2 1/4] gui: add compare_ceph_versions

Thomas Lamprecht t.lamprecht at proxmox.com
Fri May 31 13:53:02 CEST 2019


Am 5/31/19 um 12:15 PM schrieb Dominik Csapak:> this correctly compares ceph versions by its numeric parts
> the way it is written, it can be used as a function for 'sort'
> 
now one version gets added at another sublevel and we're wrong
again, or only two levels on another major release... Why did
you not one of my proposed way, which would work in general,
and also for more than ceph versions?
And if that's really wrong or not good approach at least
mention why..

Something like:

let avers = a.toString().split('.');
let bvers = b.toString().split('.');


while (1) {
    let av = avers.shift();
    let bv = bvers.shift();    

    if (av === undefined && bv === undefined) {
	return 0;
    } else if (av === undefined)  {
	return -1;
    } else if (bv === undefined) {
	return 1;
    } else {
	let diff = Number(av) - Number(bv); // or whatever conversion you prefer, parseInt, ~~, ...
	if (diff != 0) return diff;
	// else we need to look at the next parts
    }
}

does the same, but shorter and for arbitary (\d+\.) repetitions.
Additional error handling, like to ensure that's really only \d+\.
in a, b could be added easily too, if we cannot trust the API result

> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
> new in v2
>  www/manager6/Utils.js | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
> index b3a28400..4dc8ab6a 100644
> --- a/www/manager6/Utils.js
> +++ b/www/manager6/Utils.js
> @@ -117,6 +117,38 @@ Ext.define('PVE.Utils', { utilities: {
>  	return undefined;
>      },
>  
> +    compare_ceph_versions: function(a, b) {
> +	if (a === b) {
> +	    return 0;
> +	}
> +
> +	var match_a = a.toString().match(/^(\d+)\.(\d+).(\d+)$/);
> +	var match_b = b.toString().match(/^(\d+)\.(\d+).(\d+)$/);
> +
> +	if (!match_a && !match_b) {
> +	    // both versions invalid/undefined
> +	    return 0;
> +	} else if(!match_a) {
> +	    // a is undefined/invalid but b not
> +	    return -1;
> +	} else if(!match_b) {
> +	    // b is undefined/invalid but a not
> +	    return 1;
> +	}
> +
> +	var i;
> +	for (i = 1; i <= 3; i++) {
> +	    var ver_a = parseInt(match_a[i], 10);
> +	    var ver_b = parseInt(match_b[i], 10);
> +	    if (ver_a !== ver_b) {
> +		return ver_a - ver_b;
> +	    }
> +	}
> +
> +	// all parts were the same
> +	return 0;
> +    },
> +
>      get_ceph_icon_html: function(health, fw) {
>  	var state = PVE.Utils.map_ceph_health[health];
>  	var cls = PVE.Utils.get_health_icon(state);
> 




More information about the pve-devel mailing list