Skip to content

Commit

Permalink
Merge pull request #175 from Will-Cross1/131-update-colour-mapper
Browse files Browse the repository at this point in the history
Updated colourmapper to map colour in the new way for Grafana
  • Loading branch information
tofu-rocketry authored May 8, 2024
2 parents 2c4db1d + 9c61511 commit 62fd9b9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions scripts/colourmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@
args = parser.parse_args()


def create_override_dict(name, code):
"""
This function is called during a loop over all mappings in colourmapper.ini
This creates a long dictionary that will go into the "overrides" parts of the JSON files
"""
return {
"matcher": {
"id": "byName",
"options": name
},
"properties": [
{
"id": "color",
"value": {
"fixedColor": code,
"mode": "fixed"
}
}
]
}

override_list = []
for name, code in alias_colors.items():
override_list.append(create_override_dict(name, code))


def main():
"""Update the desired JSON file with predefined colors."""
filename = args.filepath[:-5] # get filename
Expand All @@ -35,7 +61,13 @@ def main():
data = json.load(reader) # load file into data variable

for i in range(len(data['panels'])): # cycle through panels in JSON file
# uses two different methods of colour mapping (aliasColours and overrides)
# as grafana needs both on the JSON to display all colours
data['panels'][i]['aliasColors'] = alias_colors
try:
data['panels'][i]['fieldConfig']['overrides'] = override_list
except KeyError:
pass

json.dump(data, writer, indent=2, sort_keys=True) # write to new file
os.replace(new_path, args.filepath) # replace original file with new file
Expand Down

0 comments on commit 62fd9b9

Please sign in to comment.