diff --git a/src/datasource-zabbix/constants.ts b/src/datasource-zabbix/constants.ts index 6917eff..a30aa4c 100644 --- a/src/datasource-zabbix/constants.ts +++ b/src/datasource-zabbix/constants.ts @@ -1,3 +1,7 @@ +// Plugin IDs +export const ZABBIX_PROBLEMS_PANEL_ID = 'alexanderzobnin-zabbix-triggers-panel'; +export const ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource'; + // Data point export const DATAPOINT_VALUE = 0; export const DATAPOINT_TS = 1; diff --git a/src/datasource-zabbix/query.controller.js b/src/datasource-zabbix/query.controller.js index 32f4003..a3c71a3 100644 --- a/src/datasource-zabbix/query.controller.js +++ b/src/datasource-zabbix/query.controller.js @@ -5,6 +5,37 @@ import * as utils from './utils'; import * as metricFunctions from './metricFunctions'; import * as migrations from './migrations'; +function getTargetDefaults() { + return { + queryType: c.MODE_METRICS, + group: { 'filter': "" }, + host: { 'filter': "" }, + application: { 'filter': "" }, + item: { 'filter': "" }, + functions: [], + triggers: { + 'count': true, + 'minSeverity': 3, + 'acknowledged': 2 + }, + trigger: {filter: ""}, + tags: {filter: ""}, + proxy: {filter: ""}, + options: { + showDisabledItems: false, + skipEmptyValues: false, + // Problems + hostsInMaintenance: false, + showTriggers: null, + sortTriggersBy: null, + showEvents: null, + }, + table: { + 'skipEmptyValues': false + }, + }; +} + export class ZabbixQueryController extends QueryCtrl { /** @ngInject */ @@ -86,6 +117,7 @@ export class ZabbixQueryController extends QueryCtrl { }); this.init = function() { + console.log(this); var target = this.target; // Migrate old targets @@ -99,27 +131,12 @@ export class ZabbixQueryController extends QueryCtrl { _.defaults(this, scopeDefaults); // Load default values - var targetDefaults = { - 'queryType': c.MODE_METRICS, - 'group': { 'filter': "" }, - 'host': { 'filter': "" }, - 'application': { 'filter': "" }, - 'item': { 'filter': "" }, - 'functions': [], - 'triggers': { - 'count': true, - 'minSeverity': 3, - 'acknowledged': 2 - }, - 'options': { - 'showDisabledItems': false, - 'skipEmptyValues': false - }, - 'table': { - 'skipEmptyValues': false - } - }; - _.defaults(target, targetDefaults); + const targetDefaults = getTargetDefaults(); + _.defaultsDeep(target, targetDefaults); + + if (this.panel.type === c.ZABBIX_PROBLEMS_PANEL_ID) { + target.queryType = c.MODE_PROBLEMS; + } // Create function instances from saved JSON target.functions = _.map(target.functions, function(func) { diff --git a/src/panel-triggers/migrations.ts b/src/panel-triggers/migrations.ts index f929160..fc34323 100644 --- a/src/panel-triggers/migrations.ts +++ b/src/panel-triggers/migrations.ts @@ -3,7 +3,16 @@ import { getNextRefIdChar } from './utils'; import { getDefaultTarget } from './triggers_panel_ctrl'; // Actual schema version -export const CURRENT_SCHEMA_VERSION = 7; +export const CURRENT_SCHEMA_VERSION = 8; + +export function getDefaultTargetOptions() { + return { + hostsInMaintenance: true, + showTriggers: 'all triggers', + sortTriggersBy: { text: 'last change', value: 'lastchange' }, + showEvents: { text: 'Problems', value: 1 }, + }; +} export function migratePanelSchema(panel) { if (isEmptyPanel(panel)) { @@ -12,7 +21,7 @@ export function migratePanelSchema(panel) { } const schemaVersion = getSchemaVersion(panel); - panel.schemaVersion = CURRENT_SCHEMA_VERSION; + // panel.schemaVersion = CURRENT_SCHEMA_VERSION; if (schemaVersion < 2) { panel.datasources = [panel.datasource]; @@ -66,6 +75,23 @@ export function migratePanelSchema(panel) { delete panel.datasources; } + if (schemaVersion < 8) { + if (panel.targets.length === 1) { + if (panel.targets[0].datasource) { + panel.datasource = panel.targets[0].datasource; + delete panel.targets[0].datasource; + } + } else if (panel.targets.length > 1) { + // Mixed data sources + panel.datasource = '-- Mixed --'; + } + for (const target of panel.targets) { + // set queryType to PROBLEMS + target.queryType = 5; + target.options = getDefaultTargetOptions(); + } + } + return panel; } diff --git a/src/panel-triggers/triggers_panel_ctrl.js b/src/panel-triggers/triggers_panel_ctrl.js index 1fdeaf1..a91f0f3 100644 --- a/src/panel-triggers/triggers_panel_ctrl.js +++ b/src/panel-triggers/triggers_panel_ctrl.js @@ -10,9 +10,7 @@ import { triggerPanelOptionsTab } from './options_tab'; import { migratePanelSchema, CURRENT_SCHEMA_VERSION } from './migrations'; import ProblemList from './components/Problems/Problems'; import AlertList from './components/AlertList/AlertList'; -import { getNextRefIdChar } from './utils'; -const ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource'; const PROBLEM_EVENTS_LIMIT = 100; export const DEFAULT_TARGET = { @@ -24,18 +22,6 @@ export const DEFAULT_TARGET = { proxy: {filter: ""}, }; -export const getDefaultTarget = (targets) => { - return { - group: {filter: ""}, - host: {filter: ""}, - application: {filter: ""}, - trigger: {filter: ""}, - tags: {filter: ""}, - proxy: {filter: ""}, - refId: getNextRefIdChar(targets), - }; -}; - export const DEFAULT_SEVERITY = [ { priority: 0, severity: 'Not classified', color: 'rgb(108, 108, 108)', show: true}, { priority: 1, severity: 'Information', color: 'rgb(120, 158, 183)', show: true}, @@ -51,7 +37,6 @@ const DEFAULT_TIME_FORMAT = "DD MMM YYYY HH:mm:ss"; export const PANEL_DEFAULTS = { schemaVersion: CURRENT_SCHEMA_VERSION, - // targets: [getDefaultTarget([])], // Fields hostField: true, hostTechNameField: false, @@ -115,7 +100,7 @@ export class TriggerPanelCtrl extends MetricsPanelCtrl { this.range = {}; this.renderData = []; - // this.panel = migratePanelSchema(this.panel); + this.panel = migratePanelSchema(this.panel); _.defaultsDeep(this.panel, _.cloneDeep(PANEL_DEFAULTS)); this.events.on(PanelEvents.render, this.onRender.bind(this)); @@ -125,6 +110,8 @@ export class TriggerPanelCtrl extends MetricsPanelCtrl { } onInitEditMode() { + // Update schema version to prevent migration on up-to-date targets + this.panel.schemaVersion = CURRENT_SCHEMA_VERSION; this.addEditorTab('Options', triggerPanelOptionsTab); }