Code in Grafana Variables

Hi Grafana Community,

I am using variables in my Grafana Dashboard, but I need to perform some logic within these variables. Specifically, I need to take another defined variable, $cluster, parse it, and based on the parsed values, perform some conditional logic using if and else.

For instance, I am looking for functionality similar to the following:

function parseAndEvaluate($cluster) {
// Parse the variable to an integer
// $cluster is another Grafana variable
let parsedNumber = parseInt($cluster, 10);
let result;

// Use the result in an if-else statement
if (!isNaN(parsedNumber)) {
    if (parsedNumber > 50) {
        result = "The number is greater than 50.";
    } else {
        result = "The number is 50 or less.";
    }
} else {
    result = "The input is not a valid number.";
}
return result;

}

Is the above possible in Grafana? If not, is there any ongoing work to provide such variable functionality? How can we request Grafana to support such functions?

Thank you for your assistance.

you can could try expressions?

@joemar Check out Business Input Data Source which includes JavaScript to create such logic.

1 Like

Thanks for replying, this won’t work because it is just for queries, I don’t want to execute a query: “Server-side expressions allow you to manipulate data returned from queries”.

I wanted to have a variable that allows me to add arbitrary logic, for instance a java script that will do some custom validations and then the format the result of the variable.

I only see this type of variables:

This is awesome I will take a look, but I see this is for logic inside the panel, right?, and I want something like this but at variables level, I only see this type of variables: Add variables | Grafana Cloud documentation if you check variable types, i don’t see one that allows me have custom/arbitrary logic in variables.