blackstork-io / fabric

An open-source command-line tool for reporting workflow automation and a configuration language for reusable templates. Reporting-as-Code
https://blackstork.io/fabric/
Apache License 2.0
10 stars 0 forks source link

`queries` block #166

Open traut opened 3 weeks ago

traut commented 3 weeks ago

Background

Currently, the query attribute for content blocks allows users to query / transform the data from the context and store it under .query_result for convenience. Using query is not required but simplifies data requests in Go templates.

This approach has 2 limitations:

Design

queries block

Introduce queries block that accepts string-to-string mappings of variable names and queries:

content text {
  queries {
    foo = ".data.inline.hosts[0]",
    bar = ".data.elasticsearch.alerts.hits.hits[0]._id"
  }

  value = "The first host is {{ .vars.foo.name }} and the first alert id is {{ .vars.bar }}"
}

The variable names used as keys in queries block are used in the context, under .vars root key, to store the corresponding query results.

queries block can be specified in document, section and content blocks. The queries are executed after the data blocks and for_each_query (so after dynamic unpacking - https://github.com/blackstork-io/fabric/issues/142).

If queries block is specified in a grouping block, like document or section, all nested children have .vars.<var> values in their contexts.

For example,

section {
  queries {
    foo = ".data.inline.hosts[0]",
    bar = ".data.elasticsearch.alerts.hits.hits[0]._id"
  }
  content text {
    value = "The first host is {{ .vars.foo.name }}"
  }
  content text {
    value = "The first alert id is {{ .vars.bar }}"
  }
}

query attribute

With the new queries block, an old query attribute becomes a syntax sugar that can be unpacked into:

queries {
    query_result = "<query-attribute-value>"
}

[!WARNING] This introduces a breaking change -- the value of the query will be accessible under .vars.query_result and not .query_result

Scope

To match the flexibility of the queries block, the query attribute should be allowed in document/ section / content blocks and behave exactly like the queries block with a single query_result value.

It is not allowed to use the 'queryattribute andqueries` block simultaneously.