#!/usr/bin/env nu # Derived variables for grafana bundle rendering. # Reads the component JSON, outputs extra Tera context vars as JSON. def datasource_block [ds: record]: nothing -> string { let ds_type = ($ds | get type) # uid is pinned to the datasource name so it survives Grafana pod restarts. # Without a stable uid, each restart generates a new random one; panels that # were migrated from string→object format in the DB reference the stale uid # and show "No data" until Grafana re-reconciles. let uid = ($ds | get -o uid | default $ds.name) let lines = [ $" - name: ($ds.name)", $" uid: ($uid)", $" type: ($ds_type)", $" url: ($ds.url)", " access: proxy", " isDefault: false", " editable: false", ] $lines | str join "\n" } def main [component_json: string]: nothing -> nothing { let d = ($component_json | from json) let datasources = ($d | get -o params.datasources | default []) let ds_blocks = ($datasources | each {|ds| datasource_block $ds }) let datasources_yaml = $"apiVersion: 1 datasources: ($ds_blocks | str join "\n")" let contact_email = ($d | get -o params.alerting.contact_email | default "") let alerting_yaml = if ($contact_email | is-empty) { "# no alerting contact configured" } else { $"apiVersion: 1 contactPoints: - orgId: 1 name: email-ops receivers: - uid: email-ops-email type: email settings: addresses: ($contact_email) singleEmail: true disableResolveMessage: false policies: - orgId: 1 receiver: email-ops group_by: [alertname, severity, volume] group_wait: 1m group_interval: 5m repeat_interval: 6h" } { datasources_yaml: $datasources_yaml, alerting_yaml: $alerting_yaml, } | to json --raw | print }