From 3f028c7e2d7017a4caa88f3e341e775a87a47b82 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 29 Mar 2016 12:12:49 +0300 Subject: [PATCH] Added migrations module for target format converting. --- src/datasource-zabbix/datasource.js | 3 ++ src/datasource-zabbix/migrations.js | 53 +++++++++++++++++++++++ src/datasource-zabbix/query.controller.js | 3 ++ 3 files changed, 59 insertions(+) create mode 100644 src/datasource-zabbix/migrations.js diff --git a/src/datasource-zabbix/datasource.js b/src/datasource-zabbix/datasource.js index 8f9a088..6e8805d 100644 --- a/src/datasource-zabbix/datasource.js +++ b/src/datasource-zabbix/datasource.js @@ -2,6 +2,7 @@ import _ from 'lodash'; import * as dateMath from 'app/core/utils/datemath'; import * as utils from './utils'; +import * as migrations from './migrations'; import * as metricFunctions from './metricFunctions'; import DataProcessor from './DataProcessor'; import './zabbixAPI.service.js'; @@ -110,6 +111,8 @@ export class ZabbixAPIDatasource { var promises = _.map(options.targets, function(target) { if (target.mode !== 1) { + //console.log(migrations.isGrafana2target(target), target); + target = migrations.migrate(target); // Don't request undefined and hidden targets if (target.hide || !target.group || diff --git a/src/datasource-zabbix/migrations.js b/src/datasource-zabbix/migrations.js new file mode 100644 index 0000000..3871782 --- /dev/null +++ b/src/datasource-zabbix/migrations.js @@ -0,0 +1,53 @@ +/** + * Query format migration. + * This module can detect query format version and make migration. + */ + +import * as utils from './utils'; + +export function isGrafana2target(target) { + if ((target.hostFilter || target.itemFilter || target.downsampleFunction || target.host.host) && + (!target.functions && !target.host.filter)) { + return true; + } else { + return false; + } +} + +export function migrateFrom2To3version(target) { + utils.isRegex(target.host.name); + var newTarget = { + group: { + filter: target.group.name === "*" ? "/.*/" : target.group.name, + isRegex: target.group.name === "*" + }, + host: { + filter: target.host.name === "*" ? convertToRegex(target.hostFilter) : target.host.name, + isRegex: target.host.name === "*" + }, + application: { + filter: target.application.name === "*" ? "" : target.application.name, + isRegex: target.application.name === "*" + }, + item: { + filter: target.item.name === "All" ? convertToRegex(target.itemFilter) : target.item.name, + isRegex: target.item.name === "All" + }, + functions: [], + mode: target.mode, + hide: target.hide, + }; + return newTarget; +} + +export function migrate(target) { + if (isGrafana2target(target)) { + return migrateFrom2To3version(target); + } else { + return target; + } +} + +function convertToRegex(str) { + return '/' + str + '/'; +} diff --git a/src/datasource-zabbix/query.controller.js b/src/datasource-zabbix/query.controller.js index c2c2bd5..1a8a64b 100644 --- a/src/datasource-zabbix/query.controller.js +++ b/src/datasource-zabbix/query.controller.js @@ -2,6 +2,7 @@ import {QueryCtrl} from 'app/plugins/sdk'; import _ from 'lodash'; import * as utils from './utils'; import * as metricFunctions from './metricFunctions'; +import * as migrations from './migrations'; import './add-metric-function.directive'; import './metric-function-editor.directive'; @@ -32,6 +33,8 @@ export class ZabbixQueryController extends QueryCtrl { this.init = function() { + this.target = migrations.migrate(this.target); + this.templateSrv = templateSrv; var target = this.target;