Race condition resolving dependent variables on Target panel

  • What Grafana version and what operating system are you using?
    10.2.0

  • What are you trying to achieve?
    I have a panel with two tables and some template variables.
    The variables are for group and category.
    The first table shows aggregate data by group and category -

FRUIT/APPLES, 500
FRUIT/MELONS, 50
VEGES/POTATOES, 100
VEGES/ONIONS, 100

The template variables are for group and category as well, with the category being dependent on the group. FRUIT -> APPLES, MELONS, VEGES-> POTATOES, ONIONS etc…

The second tables, shows a breakdown of the values in the category selected in the template variables:

(FRUIT, APPLES,) 'Granny Smith', 200
(FRUIT, APPLES,) 'Pink Lady', 300
(FRUIT, MELONS,) 'Watermelon', 30
(FRUIT, MELONS,) 'Rock', 20
(VEGES, POTATOES,) 'Red', 60
(VEGES, POTATOES,) 'Blue', 40
(VEGES, ONIONS,) 'Brown', 70
(VEGES, ONIONS,) 'Red', 30 

The second table only shows data for the selected group and category.

I’m trying to set things up such that if I click on a row in the first table, the template variables and the second table will switch to showing data for the matching group/category. If I click on FRUIT/APPLES in the first table, the second should show data for the different types of apples, if I click on the row for VEGES/ONIONS the second table should show data for different types of onions.

  • How are you trying to achieve it?
    I’ve set up a datalink for the first table that redisplays the same panel, specify field values for the template variables:

    d/stores/stores?orgid=1&${__url_time_range}&var-group=${__data.fields.GROUP}&var-category=${__data.fields.CATEGORY}

    This gives me a clickable hyperlink that changes the template variables and thus the contents of the second table.

  • What happened?
    Most of the time it works, but sometimes it doesn’t.

What I think I’m getting is that changing the group value is causing a refresh of the valid category values - and sometimes this is overwriting the category value from the data link that is passed in with the first category value that’s in the group. (or, maybe, the category from the data link is being ignored because it’s not valid in the previous group). The query to get the valid categories for the group isn’t instantaneous - it takes, maybe, 0.2 of a second. I’d like to keep the query so users can manually change the category (and the group).

  1. The data link is called with var-group=FRUIT,var-category=MELONS.
  2. The query to find the valid categories for FRUIT is run.
  3. Sometimes the category ends up as MELONS, Sometimes it ends up as APPLES

I sometimes end up on a page where the URL and the variables are mismatched:

localhost:3000/d/store/store/var-group=FRUIT,var-category=MELONS

But the variables are set to FRUIT and APPLES.
When this happens, clicking on the data link again, produces no action - presumably because it thinks we’re already there.

  • What did you expect to happen?

The template variables should always end up with the values passed on the data link.

  • Can you copy/paste the configuration(s) that you are having problems with?
    Not easily.

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    Nope, no errors.

  • Did you follow any online instructions? If so, what is the URL?
    Nope, flying solo here.

Could you screenshot dashboard and variable definitions, please?

chrome-capture-2024-6-12

Your category naming convention differs from parent to child.

VEGES/ONIONS => (VEGES, ONIONS,)

Yeah, first table has four columns:

SELECT
    TRIM(GROUP)||'/'||TRIM(CATEGORY) AS DISPLAY_NAME,
    COUNT AS METRIC1,
    GROUP,
    CATEGORY

The GROUP and CATEGORY columns are hidden, with the data link being attached to the DISPLAY_NAME. When clicked it uses the values from the GROUP and CATEGORY columns to build the URL. That part is working fine, it’s when the panel is redisplayed that the variable values specified in the URL are, sometimes, being over written.

In practice, there are 20+ groups and 4 or 5 categories inside each one, so a combined selection list isn’t going to be practical in a single drop down (and we need two separate values for the Table 2 query). Table 1 is sorted by one of three metrics to help customers identify the groups and categories they need to look at in the second table. I’m trying to add the DataLinks they don’t have to manually select the group and category through the template variables at the top of the page…

Table 2 doesn’t show the group and category, just the things that match the template variable values (potentially thousands of them) along with their individual metrics and can be sorted to locate problem ones (whith outlying metric values).

Just ran another quick test. If I paste the URL into a new tab, it always seems to work correctly (10 out of 10 tests). Problems only comes when it tries to redisplay the panel in the same tab with the selected variable values from the data link, where it seems to be redriving the whole template variable cascade and, sometimes, ends up overwriting the passed values (usually the CATEGORY value, but occasionally the GROUP value).

I’ll see what I can, but this is a simplified scenario to reproduce the problem because it’s part of a commercial development.

why are you using links to same dashboard?

Because I want to change the contents of the second table, based upon what the user has picked from the first table. Like your example, but using template variables the user can manually change or set via the data link.

what i posted does use template variables without data links. it uses the value of the variable from table 1 to filter table 2

That sounds promising. How are you setting the value of the variable in Table 1?

If I take the queries behind the variables out and hide them, then it works reliably. It seems the variable values were being set from the data link and then overwritten when the cascading variables behind them were redriven. Providing the variable already had one of the valid values, redriving the query shouldn’t, really, be changing the variables value.

parent child relationship

–parent

select 'FRUIT/APPLES' as Category union
select 'FRUIT/MELONS' union
select 'VEGES/POTATOES' union
select 'VEGES/ONIONS' 

–Child

select * from (
select 'FRUIT/APPLES' as Category, 'Granny Smith' as Type, 200 as Inventory union
select 'FRUIT/APPLES','Pink Lady', 300 union
select 'FRUIT/MELONS','Watermelon', 30 union
select 'FRUIT/MELONS','Rock', 20 union
select 'VEGES/POTATOES','Red', 60 union
select 'VEGES/POTATOES','Blue', 40 union
select 'VEGES/ONIONS','Brown', 70 union
select 'VEGES/ONIONS', 'Red', 30 
) a
where Category = '$categories'

since I do not have your db/tables, I mocked it.

Summation nicely done by Table Panel