Skip to content

Instantly share code, notes, and snippets.

@bodsch
Last active February 21, 2020 11:09
Show Gist options
  • Save bodsch/6f612c30c0e935bc0c0d5db2f05c27b2 to your computer and use it in GitHub Desktop.
Save bodsch/6f612c30c0e935bc0c0d5db2f05c27b2 to your computer and use it in GitHub Desktop.
reorder IDs in a (handmady) grafana dashboard
#!/usr/bin/env python3
from __future__ import print_function
import os
import sys
import json
arguments = sys.argv
if( len(arguments) != 2):
print("ERROR: missing argument")
sys.exit(1)
directory = arguments[1]
isdir = os.path.isdir(directory)
if(not isdir):
print("ERROR: '{}' is no directory".format(directory))
sys.exit(1)
for filename in os.listdir(directory):
if filename.endswith(".json"):
# print(directory + '/' + filename)
id_counter = 10
with open(directory + '/' + filename, 'r') as f:
dashboard = json.load(f)
panels = dashboard.get('panels', {})
if(len(panels) == 0):
continue
new_y_pos = 0
for row in panels:
new_grid = { "h": 0, "w": 0, "x": 0, "y": 0 }
grid_pos = row.get('gridPos')
w = grid_pos.get('w')
h = grid_pos.get('h')
x = grid_pos.get('x')
y = grid_pos.get('y')
# new line ... maybe
# {'h': 3, 'w': 2, 'x': 0, 'y': 0}
if(h == 3 and w == 2 and x == 0):
new_y_pos += h
new_grid["w"] = w
new_grid["h"] = h
new_grid["x"] = x
new_grid["y"] = new_y_pos
row['id'] = id_counter
row['gridPos'] = new_grid
id_counter += 1
with open(directory + '/' + filename, 'w', encoding='utf-8') as f:
json.dump(dashboard, f, ensure_ascii=False, indent=2)
else:
continue
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment