The given JavaScript code processes data from a Grafana panel...

August 7, 2025 at 06:42 PM

im using business charts and i have js code that holds information about some stores in a set. I need to populate a dashboard variable called Store with this information in grafana. how do i do it within js const storeSet = new Set(); context.panel.data.series.forEach((seriesObj) => { storeSet.add(seriesObj.meta.custom.labels["metric.label.store"]); console.log(seriesObj.meta.custom.labels["metric.label.store"]); }); console.log(storeSet);

The given JavaScript code processes data from a Grafana panel and extracts unique store names into a Set object called storeSet. Here’s a detailed breakdown of what the code does:

What the code does:

  1. Define a Set to store unique store names:

    const storeSet = new Set();
    
    • This initializes an empty Set, which is a collection of values where duplicates are automatically removed.
  2. Iterate over series data in Grafana:

    context.panel.data.series.forEach((seriesObj) => {
    
    • context.panel.data.series refers to the data series from the Grafana panel. Each data series object (seriesObj) contains metadata and other details about the series.
  3. Extract the store name:

    storeSet.add(seriesObj.meta.custom.labels["metric.label.store"]);
    
    • For each seriesObj, the code accesses a property deep in the metadata: seriesObj.meta.custom.labels["metric.label.store"]. This is assumed to be the store name associated with the series.
    • The extracted store name is added to the storeSet. Since Set ensures that only unique values are stored, duplicate store names are automatically handled.
  4. Log each store name:

    console.log(seriesObj.meta.custom.labels["metric.label.store"]);
    
    • This logs each store name fetched from the data series for debugging or informational purposes.
  5. Log the final Set:

    console.log(storeSet);
    
    • After the forEach loop, the final Set containing all unique store names is logged to the console.

How to use this in Grafana:

The code assumes that you are using Grafana and working with JavaScript-based custom panels (e.g., via plugins or custom scripts). After this code runs, you will have a storeSet variable containing all unique stores from your data series. You could then use the storeSet to populate the Store variable in your Grafana dashboard.

Here’s an example of how you might populate the Store variable in Grafana:

const storeArray = Array.from(storeSet);  // Convert the Set to an Array
grafana.variables.set("Store", storeArray);  // Assuming grafana.variables.set() is available or use a suitable API

Summary:

  • This code extracts store information from the metric.label.store field of Grafana series metadata.
  • It stores unique store names in a Set.
  • For integration with Grafana, the store names can be converted to an array and used to update a dashboard variable called Store.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node