Skip to content

add node links #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hamedkarbasi93-nodegraphapi-datasource",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
45 changes: 41 additions & 4 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
FieldColorModeId,
} from '@grafana/data';

import { getBackendSrv } from '@grafana/runtime';

import { getTemplateSrv } from '@grafana/runtime';
import { getBackendSrv, getTemplateSrv } from '@grafana/runtime';

import { MyQuery, MyDataSourceOptions, defaultQuery } from './types';

Expand Down Expand Up @@ -52,7 +50,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
// fieldType can be either number of string
var fieldType = field['type'] === 'number' ? FieldType.number : FieldType.string;
// add 'name' and 'type' items to the output object
var outputField: FrameFieldType = { name: field['field_name'], type: fieldType, config: {} };
var outputField: FrameFieldType = { name: field['field_name'], type: fieldType, config: { links: [] } };
// add color for 'arc__*' items(only apperas for the nodes)
if ('color' in field) {
outputField.config.color = { fixedColor: field['color'], mode: FieldColorModeId.Fixed };
Expand All @@ -61,6 +59,45 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
if ('displayName' in field) {
outputField.config.displayName = field['displayName'];
}

// add optional links (format: link__name__param)
let links: Record<string, any> = {};
for (let xkey in field) {
if (xkey.startsWith('links__')) {
let set = xkey.split('__');
if (!links[set[1]]) {
links[set[1]] = {};
}
links[set[1]][set[2]] = field[xkey];
}
}
if (Object.keys(links).length > 0) {
// transforms parameters into link objects
// outputField.config.links = [];
for (let ykey in links) {
let link = links[ykey];
// add single external link for items (url, title)
if ('url' in link) {
outputField.config.links.push({
url: link['url'],
title: link['title'] || 'Link',
targetBlank: true,
});
}
// add single internal link for items (expr, uid, name)
if ('expr' in link && 'uid' in link) {
outputField.config.links.push({
url: link['url'] || '',
title: link['title'] || 'Link',
internal: {
query: { expr: link['expr'] },
datasourceUid: link['uid'],
datasourceName: link['name'] || link['uid'],
},
});
}
}
}
outputFields.push(outputField);
});
return outputFields;
Expand Down