Skip to content

Commit

Permalink
Enhance clock panel migration to set grafana datasource as default wh…
Browse files Browse the repository at this point in the history
…en on queries are in use (#209)

* Enhance clock panel migration to set grafana datasource as default when on queries are in use

* v2.1.8

* ignore all ts files

* fix spell

* Restore file

* add datasource

* improve code order
  • Loading branch information
academo authored Sep 26, 2024
1 parent 5704b15 commit 1bfd690
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clock-panel",
"version": "2.1.7",
"version": "2.1.8",
"description": "Clock Panel Plugin for Grafana",
"scripts": {
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
Expand Down
31 changes: 31 additions & 0 deletions src/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PanelModel } from '@grafana/data';
import { ClockOptions, ClockRefresh } from './types';
import { config } from '@grafana/runtime';

export const clockMigrationHandler = (panel: PanelModel<ClockOptions>): Partial<ClockOptions> => {
const options: any = panel.options || {};
Expand Down Expand Up @@ -79,6 +80,36 @@ const migrateInputOnlyPluginConfig = (panel: PanelModel<ClockOptions>) => {
delete panel.datasource;
// remove the targets
panel.targets = [];

// find the grafana datasource and set it if available
const datasources = config.datasources || [];
let grafanaDs: (typeof datasources)[number] | undefined = undefined;

for (let datasourceKey of Object.keys(datasources)) {
const ds = datasources[datasourceKey];
if (ds.uid === 'grafana' || (ds.name === '-- Grafana --' && ds.type === 'datasource')) {
grafanaDs = ds;
break;
}
}

// set a default random walk
if (grafanaDs !== undefined) {
panel.targets = [
{
refId: 'A',
datasource: {
type: grafanaDs.type,
uid: grafanaDs.uid,
},
queryType: 'randomWalk',
},
];
panel.datasource = {
type: grafanaDs.type,
uid: grafanaDs.uid,
};
}
};

const cleanupConfig = (panel: PanelModel<ClockOptions>) => {
Expand Down

0 comments on commit 1bfd690

Please sign in to comment.