Skip to content
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

Auto-Import Dashboards #584

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mccahan
Copy link

@mccahan mccahan commented Feb 15, 2025

Hello! I've had my PW3 for a week, found this pretty quick, and love the project.

My pitch for a slightly easier onboarding process, asking the user whether they would like to import the dashboards into Grafana automatically, and whether they would like to set a dashboard to show when they first log in. Coupled with my other PR for anonymous access (#583), you can effectively skip through the manual "import a JSON" file process, and after setup just go to http://localhost:9000 and have a dashboard ready and waiting for you.

Screenshot 2025-02-15 at 10 31 54@2x

Here they are in the Grafana dashboard list:
Screenshot 2025-02-15 at 11 03 47@2x

Notes:
import-dashboards.sh has a little bit of search-replace nonsense happening

  • I opted to do some search-replacing on the files you have to clean them up for the auto-import so that I didn't modify them for whatever process you've figured out for the manual imports
  • To handle the auto-provisioned data sources, you just pass the name of the source instead of the JSON object with the type and the variable
  • There's a bug in Grafana where it will show a "Invalid dashboard UID in the request" error if you don't disable the built-in Grafana annotation query
  • Swapping out the variables driven from the environment with some values (these are still configurable in the same place inside Grafana)
  • I was having an annoying time getting sed to work right on macOS with all the variable interpolation, so I ended up with Perl here but that might not work on Windows?

I chose to be a bit opinionated and make importing the dashboards the default, since they go into a separate "Imported" folder in Grafana anyway and I think it might be a bit more tedious for users if they have to go back later to try it again, but happy to make the default not automatically importing.

@mccahan mccahan force-pushed the feature/import-dashboards branch from d75777d to da2e71b Compare February 15, 2025 18:23
Comment on lines 23 to 48
# Replace variables in dashboards
for file in ./grafana/dashboards/*.json; do
# Replace data source objects with just the name of the source
perl -0777 -pi -e 's|"datasource":\s*\{[^}]*DS_INFLUXDB[^\n]*\s+\}|"datasource": "InfluxDB (auto provisioned)"|gs' "$file"
perl -0777 -pi -e 's|"datasource":\s*\{[^}]*DS_SUN_AND[^\n]*\s+\}|"datasource": "DS_SUN_AND MOON"|gs' "$file"

# Disable the built-in annotation, see https://github.com/grafana/grafana/issues/54574#issuecomment-1431696899
sed -i '' "s|\"enable\": true|\"enable\": false|g" "$file"

# Timezone variable
sed -i '' "s|\${VAR_TZ}|\${tz}|g" "$file"
CURRENT=`cat tz`
perl -0777 -pi -e "s|\"query\": \"\\\${tz}\"|\"query\": \"$CURRENT\"|gs" "$file"

# Cost variables
sed -i '' "s|\${VAR_AVG_BUY_PER_KWH}|\${avg_buy_per_kwh}|g" "$file"
perl -0777 -pi -e 's|"query": "\$\{avg_buy_per_kwh\}"|"query": "0.19"|gs' "$file"

sed -i '' "s|\${VAR_AVG_SELL_PER_KWH}|\${avg_sell_per_kwh}|g" "$file"
perl -0777 -pi -e 's|"query": "\$\{avg_sell_per_kwh\}"|"query": "0.19"|gs' "$file"

sed -i '' "s|\${VAR_AVG_PER_KWH}|\${avg_per_kwh}|g" "$file"
perl -0777 -pi -e 's|"query": "\$\{avg_per_kwh\}"|"query": "0.19"|gs' "$file"

sed -i '' "s|0.19|${avg_price_per_kwh}|g" "$file"
done
Copy link
Owner

Choose a reason for hiding this comment

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

We have had issues with sed -i across different platforms. Since we know sed -i.bak works for our supported platforms, I would prefer to use that in these cases. I think the right setup would be:

    # Replace variables in dashboards
    for file in ./grafana/dashboards/*.json; do
        # Replace data source objects with just the name of the source
        sed -i.bak -E 's|"datasource":\s*\{[^}]*DS_INFLUXDB[^}]*\}|"datasource": "InfluxDB (auto provisioned)"|g' "$file"
        sed -i.bak -E 's|"datasource":\s*\{[^}]*DS_SUN_AND[^}]*\}|"datasource": "DS_SUN_AND MOON"|g' "$file"

        # Disable the built-in annotation
        sed -i.bak "s|\"enable\": true|\"enable\": false|g" "$file"

        # Timezone variable
        sed -i.bak "s|\${VAR_TZ}|\${tz}|g" "$file"
        CURRENT=$(cat tz)
        sed -i.bak "s|\"query\": \"\${tz}\"|\"query\": \"$CURRENT\"|g" "$file"

        # Cost variables
        sed -i.bak "s|\${VAR_AVG_BUY_PER_KWH}|\${avg_buy_per_kwh}|g" "$file"
        sed -i.bak "s|\"query\": \"\${avg_buy_per_kwh}\"|\"query\": \"0.19\"|g" "$file"

        sed -i.bak "s|\${VAR_AVG_SELL_PER_KWH}|\${avg_sell_per_kwh}|g" "$file"
        sed -i.bak "s|\"query\": \"\${avg_sell_per_kwh}\"|\"query\": \"0.19\"|g" "$file"

        sed -i.bak "s|\${VAR_AVG_PER_KWH}|\${avg_per_kwh}|g" "$file"
        sed -i.bak "s|\"query\": \"\${avg_per_kwh}\"|\"query\": \"0.19\"|g" "$file"

        sed -i.bak "s|0.19|${avg_price_per_kwh}|g" "$file"

        # Remove backup files
        rm -f "$file.bak"
    done

Copy link
Author

Choose a reason for hiding this comment

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

The multiline sed commands for the data sources weren't working for me here, so I took a small liberty and updated them in the dashboards/*.json files themselves: c0e4838

I tested this with both manual and automatic import and it worked fine. Assuming most (all?) users don't bother renaming those data sources, even on a manual import this would skip the step where you have to select the data sources.

Let me know if you have any strong objections 😅

echo ""
./import-dashboards.sh
fi

Copy link
Owner

Choose a reason for hiding this comment

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

Similar to the comment on the other PR, I think we need to assume that people will run setup.sh to change their configuration so import-dashboard.sh should be able to run against existing installations (or even standalone). For this case, I think you could probably just have it check for imported dashboards and prompt the user to see if they want to overwrite the existing versions.

@jasonacox
Copy link
Owner

Thanks @mccahan - very cool. I think this would be a nice addition. I want to think through complications.

I would generally prefer not to keep creating separate helper scripts in the main repo. It adds some confusion. I wonder if this should just be in setup.sh. If not, this particular one could go into the grafana or dashboards directory.

@mccahan
Copy link
Author

mccahan commented Feb 16, 2025

Moving it would make sense! I split it into a separate file just to keep from polluting your setup.sh too much

@mccahan
Copy link
Author

mccahan commented Feb 16, 2025

@jasonacox appreciate the feedback!

Updates:

  • Moved the import script to grafana/import-dashboards.sh
  • Running it every time the user runs setup.sh
  • Checks for existing dashboards and confirms overwriting
  • Remove default dashboard setting when the user updates it
  • Possibly-overstretching update to the original dashboard JSON files (see Auto-Import Dashboards #584 (comment))

Comment on lines -4 to -18
"name": "DS_INFLUXDB_(AUTO PROVISIONED)",
"label": "InfluxDB (auto provisioned)",
"description": "",
"type": "datasource",
"pluginId": "influxdb",
"pluginName": "InfluxDB"
},
{
"name": "DS_SUN_AND MOON (AUTO PROVISIONED)",
"label": "Sun and Moon (auto provisioned)",
"description": "",
"type": "datasource",
"pluginId": "fetzerch-sunandmoon-datasource",
"pluginName": "Sun and Moon"
},
Copy link
Owner

Choose a reason for hiding this comment

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

Thinking through all the use cases that users may have...

There was a specific reason why we added the variables #512 - How will this work for existing users who didn't auto-provision? All my test boxes are auto-provisioned at this point so I need to see if I can recreate what may be in the wild.

@jasonacox
Copy link
Owner

Possibly-overstretching update to the original dashboard JSON files (see

Same concern. If we move from sed to perl for those multi-line changes, could we make it work without adjusting? I spot tested perl on most of the platform we support and it does seem to be installed by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants