Просмотр исходного кода

graphite: improved version comparison

Alexander Zobnin 8 лет назад
Родитель
Сommit
89aea278a9

+ 28 - 0
public/app/core/utils/version.ts

@@ -0,0 +1,28 @@
+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 - 1
public/app/plugins/datasource/graphite/datasource.ts

@@ -2,6 +2,7 @@
 
 import _ from 'lodash';
 import * as dateMath from 'app/core/utils/datemath';
+import {isVersionGtOrEq} from 'app/core/utils/version';
 
 /** @ngInject */
 export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv) {
@@ -360,5 +361,5 @@ export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv
 }
 
 function supportsTags(version: string): boolean {
-  return version >= '1.1';
+  return isVersionGtOrEq(version, '1.1');
 }

+ 4 - 9
public/app/plugins/datasource/graphite/gfunc.js

@@ -1,8 +1,9 @@
 define([
   'lodash',
-  'jquery'
+  'jquery',
+  'app/core/utils/version'
 ],
-function (_, $) {
+function (_, $, version) {
   'use strict';
 
   var index = [];
@@ -944,13 +945,7 @@ function (_, $) {
   };
 
   function isVersionRelatedFunction(func, graphiteVersion) {
-    return isVersionGreaterOrEqual(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 version.isVersionGtOrEq(graphiteVersion, func.version) || !func.version;
   }
 
   return {

+ 55 - 0
public/test/core/utils/version_specs.ts

@@ -0,0 +1,55 @@
+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);
+      });
+    });
+  });
+});