|
| 1 | +--- |
| 2 | +title: "How to fetch additional data for a flux query from a json file" |
| 3 | +date: 2024-12-26 |
| 4 | +tags: |
| 5 | +- influx |
| 6 | +- grafana |
| 7 | +- datenzwerg |
| 8 | +--- |
| 9 | + |
| 10 | +My buddy Romses is currently taking care of the [Datenzwerg deployment](https://datagnome.de) at 38c3, and like at every event where we deploy them I'm updating our page and [Grafana dashboard](https://grafana.datagnome.de) with the locations of the gnomes. |
| 11 | + |
| 12 | +So far the latter was always quite annoying: We have only the names of the gnomes in our influx data, and adding the location/deployment status to the graph thus meant having something like this for every single graph: |
| 13 | + |
| 14 | +```flux |
| 15 | +import "strings" |
| 16 | +import "dict" |
| 17 | +
|
| 18 | +locations = [ |
| 19 | + "Bashful": "Uptime Bar", |
| 20 | + "Dopey": "c3cat", |
| 21 | + "Grumpy": "Späti", |
| 22 | + "Happy": "Kidspace", |
| 23 | + "Hefty": "HASS Assembly", |
| 24 | + "Moopsy": "Chaospost", |
| 25 | + "Kinky": "Eventphone", |
| 26 | + "Nerdy": "House of Tea", |
| 27 | + "Sleepy": "DDOS Bar", |
| 28 | + "Sneezy": "Wohnzimmer" |
| 29 | +] |
| 30 | +
|
| 31 | +from(bucket: "datagnome") |
| 32 | + [...] |
| 33 | + |> map(fn: (r) => ({r with device: r.device + " (" + dict.get(dict: locations, key: r.device, default: "?") + ")"})) |
| 34 | +``` |
| 35 | + |
| 36 | +Which of course means that I had to update this `locations` dict for every single panel, on every single deployment, at least twice (start and end of the event). |
| 37 | + |
| 38 | +I finally decided I had to solve this differently and just now figured out how to keep the deployment info in [a JSON file on our git repo](https://github.com/romses/Datenzwerg/blob/main/docs/deployment.json) and then querying *that* from the graphs, instead of manually keeping the lookup data updated in more than one place: |
| 39 | + |
| 40 | +```flux |
| 41 | +import "strings" |
| 42 | +import "dict" |
| 43 | +import "http/requests" |
| 44 | +import "experimental/json" |
| 45 | +
|
| 46 | +response = requests.get(url: "https://raw.githubusercontent.com/romses/Datenzwerg/refs/heads/main/docs/deployment.json") |
| 47 | +data = json.parse(data: response.body) |
| 48 | +locations = dict.fromList(pairs: data) |
| 49 | +
|
| 50 | +from(bucket: "datagnome") |
| 51 | + [...] |
| 52 | + |> map(fn: (r) => ({r with device: r.device + " (" + dict.get(dict: locations, key: r.device, default: "?") + ")"})) |
| 53 | +``` |
| 54 | + |
| 55 | +So far seems to work well, and I'm very happy to be able to do this faster now (and also more easily from my phone, should I need to). |
| 56 | + |
| 57 | +Next step: Figuring out how to use that JSON file to also keep the event box on the home page updated. But that's for another day :) |
0 commit comments