Implementing a translation table in InfluxDB

@Matteo.dt Where the localization data isn’t time series data, I don’t know that InfluxDB is the best storage solution for it. Storing in in CSV or an a relational db may be the better way to go. If you’re using Flux, you can query external CSV and SQL data sources.

The following example shows the CSV inline, but you could certainly pull the CSV from an external source or query a SQL database.

import "csv"

localizationCSV = "Variable,Language1,Language2
VariableA,VariableALang1,VariableALang2
VariableB,VariableBLang1,VariableBLang2
"

localizations = csv.from(csv: localizationCSV, mode: "raw")

localize = (variable, lang) => {
    localization = localizations
        |> filter(fn: (r) => r.Variable == variable)
        |> findColumn(fn: (key) => true, column: lang)

    return localization[0]
}

from(bucket: "example-bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r._measurement == "_measurement1")
    |> map(fn: (r) => ({r with _field: localize(variable: r._field, lang: "Language1")}))

For the sake of demonstration, here’s the same example, but with the sample data you provided above in an ad hoc table (you should be able to run this query in InfluxDB 2.0+):

import "array"
import "csv"

localizationCSV = "Variable,Language1,Language2
VariableA,VariableALang1,VariableALang2
VariableB,VariableBLang1,VariableBLang2
"

localizations = csv.from(csv: localizationCSV, mode: "raw")

localize = (variable, lang) => {
    localization = localizations
        |> filter(fn: (r) => r.Variable == variable)
        |> findColumn(fn: (key) => true, column: lang)

    return localization[0]
}

array.from(
    rows: [
        {_time: 2022-01-01, _field: "VariableA", _value: "value1"},
        {_time: 2022-01-01, _field: "VariableB", _value: "value2"},
    ])
    |> map(fn: (r) => ({r with _field: localize(variable: r._field, lang: "Language1")}))

This returns:

_time _field _value
2022-01-01T00:00:00Z VariableALang1 value1
2022-01-01T00:00:00Z VariableBLang1 value2
1 Like