Commit 89aea278 by Alexander Zobnin

graphite: improved version comparison

parent c22a192b
const versionPattern = /(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z\.]+))?/;
export class SemVersion {
major: number;
minor: number;
patch: number;
meta: string;
constructor(version: string) {
let match = versionPattern.exec(version);
if (match) {
this.major = Number(match[1]);
this.minor = Number(match[2] || 0);
this.patch = Number(match[3] || 0);
this.meta = match[4];
}
}
isGtOrEq(version: string): boolean {
let compared = new SemVersion(version);
return !(this.major < compared.major || this.minor < compared.minor || this.patch < compared.patch);
}
}
export function isVersionGtOrEq(a: string, b: string): boolean {
let a_semver = new SemVersion(a);
return a_semver.isGtOrEq(b);
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import _ from 'lodash'; import _ from 'lodash';
import * as dateMath from 'app/core/utils/datemath'; import * as dateMath from 'app/core/utils/datemath';
import {isVersionGtOrEq} from 'app/core/utils/version';
/** @ngInject */ /** @ngInject */
export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv) { export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv) {
...@@ -360,5 +361,5 @@ export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv ...@@ -360,5 +361,5 @@ export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv
} }
function supportsTags(version: string): boolean { function supportsTags(version: string): boolean {
return version >= '1.1'; return isVersionGtOrEq(version, '1.1');
} }
define([ define([
'lodash', 'lodash',
'jquery' 'jquery',
'app/core/utils/version'
], ],
function (_, $) { function (_, $, version) {
'use strict'; 'use strict';
var index = []; var index = [];
...@@ -944,13 +945,7 @@ function (_, $) { ...@@ -944,13 +945,7 @@ function (_, $) {
}; };
function isVersionRelatedFunction(func, graphiteVersion) { function isVersionRelatedFunction(func, graphiteVersion) {
return isVersionGreaterOrEqual(graphiteVersion, func.version) || !func.version; return version.isVersionGtOrEq(graphiteVersion, func.version) || !func.version;
}
function isVersionGreaterOrEqual(a, b) {
var a_num = Number(a);
var b_num = Number(b);
return a_num >= b_num;
} }
return { return {
......
import {describe, beforeEach, it, expect} from 'test/lib/common';
import {SemVersion, isVersionGtOrEq} from 'app/core/utils/version';
describe("SemVersion", () => {
let version = '1.0.0-alpha.1';
describe('parsing', () => {
it('should parse version properly', () => {
let semver = new SemVersion(version);
expect(semver.major).to.be(1);
expect(semver.minor).to.be(0);
expect(semver.patch).to.be(0);
expect(semver.meta).to.be('alpha.1');
});
});
describe('comparing', () => {
beforeEach(() => {
version = '3.4.5';
});
it('should detect greater version properly', () => {
let semver = new SemVersion(version);
let cases = [
{value: '3.4.5', expected: true},
{value: '3.4.4', expected: true},
{value: '3.4.6', expected: false},
{value: '4', expected: false},
{value: '3.5', expected: false},
];
cases.forEach((testCase) => {
expect(semver.isGtOrEq(testCase.value)).to.be(testCase.expected);
});
});
});
describe('isVersionGtOrEq', () => {
it('should compare versions properly (a >= b)', () => {
let cases = [
{values: ['3.4.5', '3.4.5'], expected: true},
{values: ['3.4.5', '3.4.4'] , expected: true},
{values: ['3.4.5', '3.4.6'], expected: false},
{values: ['3.4', '3.4.0'], expected: true},
{values: ['3', '3.0.0'], expected: true},
{values: ['3.1.1-beta1', '3.1'], expected: true},
{values: ['3.4.5', '4'], expected: false},
{values: ['3.4.5', '3.5'], expected: false},
];
cases.forEach((testCase) => {
expect(isVersionGtOrEq(testCase.values[0], testCase.values[1])).to.be(testCase.expected);
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment