-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: main
Are you sure you want to change the base?
Conversation
d75777d
to
da2e71b
Compare
import-dashboards.sh
Outdated
# 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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.
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. |
Moving it would make sense! I split it into a separate file just to keep from polluting your setup.sh too much |
Co-Authored-By: Jason Cox <jason@jasonacox.com>
@jasonacox appreciate the feedback! Updates:
|
"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" | ||
}, |
There was a problem hiding this comment.
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.
Same concern. If we move from |
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.Here they are in the Grafana dashboard list:

Notes:
import-dashboards.sh
has a little bit of search-replace nonsense happeningI 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.