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

Updated colourmapper to map colour in the new way for Grafana #175

Merged
merged 3 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Will-Cross1 marked this conversation as resolved.
Show resolved Hide resolved
try:
data['panels'][i]['fieldConfig']['overrides'] = override_list
except KeyError:
Will-Cross1 marked this conversation as resolved.
Show resolved Hide resolved
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