Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Conversation

meverett
Copy link
Contributor

@meverett meverett commented Dec 6, 2016

This is related to issue #30.

Presently Simple JSON Datasource does not work as a proper data source for template variables of type: Query. It only supports an argument object with a property "target". When template variables send their query to the data source, there is only one argument and it is a string, which is just the content of the query the user entered into the template variable text box.

Since metricFindQuery only expects a nested object as the argument, it effectively strips off the query value before sending it on to the data source when options is a string:

var interpolated = {
  target: this.templateSrv.replace(options.target, null, 'regex')
};

We can support both use cases by providing some conditional logic to detect what type of argument has been passed from Grafana:

var target = typeof (options) == "string" ? options : options.target;
var interpolated = {
  target: this.templateSrv.replace(target, null, 'regex')
};

Also right now the mapToTextValue function only supports results that are of a single value. The returned value is used as the text, and the index in the list is used as the value:

function mapToTextValue(result) {
  return _.map(result.data, function (d, i) {
    return { text: d, value: i };
  });
}

By adding some additional conditional logic to detect nested text/value objects, we can support both scenarios:

function mapToTextValue(result) {
  return _.map(result.data, function (d, i) {
    if (d && d.text && d.value) return { text: d.text, value: d.value }
    return { text: d, value: i };
  });
}

The nested { text: "label", value: "rawValue" } is supported by query template variables. Grafana will use the text property as the label in the template variable drop down and the value property as the actual value of the template variable. I believe this also might possibly address issue #27 as well.

Using Simple JSON Datasource as a custom template variable aliasing solution is very desirable since Grafana currently does not support custom aliasing/look up for template variables. This happens when you have a raw value that is not very user-friendly to display that you want to use as a template variable. This setup provides a solution where you can use Simple JSON Datasource to alias template variables and provide a user-friendly label on the dashboard, but preserve the raw value for driving queries using your own HTTP endpoint to drive aliasing.

…ariables with support for both single strings (default) or nested text/value objects: { text: "label", value: "rawValue }.
Copy link
Contributor

@bergquist bergquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. I certainly know other will find this useful.
But the code need unit tests before merging.

@RichiH
Copy link
Member

RichiH commented Dec 7, 2016

I for one will use it once it's in a state to be merged :)

@meverett
Copy link
Contributor Author

meverett commented Dec 7, 2016

Unit tests added. Let me know if you see any issues with them. I tried to ensure the previous expected behavior was intact as well as ensuring the enhancements work as expected.

@bergquist bergquist merged commit 778a91c into grafana:master Dec 7, 2016
@bergquist
Copy link
Contributor

bergquist commented Dec 7, 2016

Great work!

I still have some more work to be done before I make a new release on grafana.net.
But if you want to run this release. Just git clone this repo into the grafana plugins folder. (data/plugins by default)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants