Data: datasets and bound widgets
lib/state/tui_data.sh, lib/markup/tui_data_tags.sh - named lists of rows that code appends to, changes and removes, and widgets that follow them. Guide: Data. Tutorial: A live dashboard.
A row is its cells separated by |. An ID is the key of the row for a keyed dataset (--key), else the 0-based index. A COL is a 0-based index or a name from --cols. Functions that take a variable or array name (VAR, ARRAY) keep their own locals under a _ prefix, so any caller name is safe.
Declare
| Function | Summary |
|---|---|
tui.data.new |
Declares a dataset: a named list of rows that code appends to, changes and removes, and that widgets can show. |
tui.data.drop |
Forgets a dataset, its rows and the bindings of the widgets that showed it. |
tui.data.new
tui.data.new NAME [--cols "A|B"] [--key COL] [--max N] [--scope page|session|disk]
Declares a dataset: a named list of rows that code appends to, changes and removes, and that widgets can show.
Parameters
NAME: letters, digits and_, not starting with a digit.--cols: column names separated by|. They name columns in the other calls and become a table’s header.--key COL: the column whose value identifies a row. A keyed dataset has at most one row per key, sotui.data.appendwith an existing key replaces that row.--max N: keep only the newestNrows; the oldest row goes when one is added pastN.--scope:page(dropped when the page is left),session(kept for the run, the default) ordisk(also written to$TUI_HOME/state/data/NAME, read here).
Returns: 1 for a bad name, option or key column. Declaring a dataset that exists changes nothing, so a page may declare it on every visit.
Example
tui.data.new latency --cols "ms" --max 60
tui.data.new hosts --cols "host|state" --key host
tui.data.drop
tui.data.drop NAME
Forgets a dataset, its rows and the bindings of the widgets that showed it.
Returns: 1 for an unknown dataset.
Change
| Function | Summary |
|---|---|
tui.data.append |
Adds rows at the end. A row is its cells separated by \|. |
tui.data.set |
Changes one cell of a row. |
tui.data.replace |
Changes a whole row. In a keyed dataset the new row may carry another key; the row keeps its position. |
tui.data.remove |
Deletes rows. Ids that do not exist are skipped. |
tui.data.clear |
Deletes every row. The dataset stays declared and bound. |
tui.data.append
tui.data.append NAME ROW...
Adds rows at the end. A row is its cells separated by |.
Notes
- In a keyed dataset a row whose key exists replaces that row in place.
- With
--max Nthe oldest rows go when there are more thanN; the others are not moved. - A cell cannot contain
|.
Returns: 1 for an unknown dataset.
Example
tui.data.append latency 12 15 9
tui.data.append hosts "web1|up" "db1|down"
tui.data.set
tui.data.set NAME ID COL VALUE
Changes one cell of a row.
Parameters
ID: the key of the row for a keyed dataset, else its 0-based index.COL: a 0-based column index or a name from--cols.
Returns: 1 for an unknown dataset, row or column.
Example
tui.data.set hosts db1 state up
tui.data.replace
tui.data.replace NAME ID ROW
Changes a whole row. In a keyed dataset the new row may carry another key; the row keeps its position.
Returns: 1 for an unknown dataset or row.
tui.data.remove
tui.data.remove NAME ID...
Deletes rows. Ids that do not exist are skipped.
Notes
- The row array is rebuilt once for all ids, so removing many rows in one call costs the same as removing one.
Returns: 1 for an unknown dataset.
Example
tui.data.remove hosts db1 cache1
tui.data.clear
tui.data.clear NAME
Deletes every row. The dataset stays declared and bound.
Returns: 1 for an unknown dataset.
Read
| Function | Summary |
|---|---|
tui.data.count |
Prints the number of rows, or stores it in VAR. |
tui.data.has |
Succeeds when a row with that id exists (the key for a keyed dataset, else the 0-based index). |
tui.data.get |
Stores a row, its cells joined by \|, in VAR. |
tui.data.cell |
Stores one cell in VAR. |
tui.data.rows |
Fills the array ARRAY with the rows, oldest first. With --last N only the newest N rows. |
tui.data.col |
Fills the array ARRAY with one column, oldest first. With --last N only the newest N values. |
tui.data.find |
Stores the id of the first row whose cell COL equals VALUE in VAR: the key for a keyed dataset, else the 0-based index. |
tui.data.stat |
Computes sum, min, max, avg or count over the numeric cells of a column and prints it, or stores it in VAR. |
tui.data.sort |
Sorts the rows in place. The sort is stable. |
tui.data.gen |
Stores the change counter of a dataset in VAR. It grows with every change, so code can tell whether something changed since it last looked. |
tui.data.count
tui.data.count NAME [VAR]
Prints the number of rows, or stores it in VAR.
Returns: 1 for an unknown dataset.
Example
tui.data.count hosts n
tui.data.has
tui.data.has NAME ID
Succeeds when a row with that id exists (the key for a keyed dataset, else the 0-based index).
Example
tui.data.has hosts web1 || tui.data.append hosts "web1|up"
tui.data.get
tui.data.get NAME ID VAR
Stores a row, its cells joined by |, in VAR.
Returns: 1 for an unknown dataset or row.
Example
tui.data.get hosts web1 row
IFS='|' read -ra cells <<<"$row"
tui.data.cell
tui.data.cell NAME ID COL VAR
Stores one cell in VAR.
Returns: 1 for an unknown dataset, row or column.
Example
tui.data.cell hosts web1 state state
tui.data.rows
tui.data.rows NAME ARRAY [--last N]
Fills the array ARRAY with the rows, oldest first. With --last N only the newest N rows.
Returns: 1 for an unknown dataset.
Example
tui.data.rows log recent --last 10
tui.table.set tbl "Time|Text" "${recent[@]}"
tui.data.col
tui.data.col NAME COL ARRAY [--last N]
Fills the array ARRAY with one column, oldest first. With --last N only the newest N values.
Returns: 1 for an unknown dataset or column.
Example
tui.data.col latency ms values --last 24
tui.chart.spark line "${values[@]}"
tui.data.find
tui.data.find NAME COL VALUE VAR
Stores the id of the first row whose cell COL equals VALUE in VAR: the key for a keyed dataset, else the 0-based index.
Returns: 1 when no row matches, or for an unknown dataset or column.
Example
tui.data.find hosts state down first_down && tui.notify "$first_down is down" warn
tui.data.stat
tui.data.stat NAME COL FN [VAR]
Computes sum, min, max, avg or count over the numeric cells of a column and prints it, or stores it in VAR.
Notes
- Numbers may have decimals (
12.5); they are summed in thousandths, so there is no fork and no floating point.avgkeeps three decimals. - Cells that are not numbers are skipped.
Returns: 1 for an unknown dataset, column or function, and for min, max and avg of no numbers (the result is then empty). sum and count of nothing are 0.
Example
tui.data.stat latency ms avg avg_ms
tui.data.sort
tui.data.sort NAME COL [--desc] [--num]
Sorts the rows in place. The sort is stable.
Parameters
--desc: largest first.--num: compare the cells as numbers (decimals allowed) instead of as text.
Notes
- Text is compared with the current locale, as
[[ a < b ]]does.
Returns: 1 for an unknown dataset, column or option.
Example
tui.data.sort procs cpu --num --desc
tui.data.gen
tui.data.gen NAME VAR
Stores the change counter of a dataset in VAR. It grows with every change, so code can tell whether something changed since it last looked.
Returns: 1 for an unknown dataset.
Show
Widgets follow a dataset through a binding. Markup writes the same bindings as attributes; see Data.
| Function | Summary |
|---|---|
tui.data.bind |
Keeps a widget showing a dataset. Whenever the dataset changes the widget is updated, once, after the callback that changed it, and only when the text it would show is different. |
tui.data.unbind |
Removes the binding of a widget. The widget keeps what it shows. |
tui.data.flush |
Updates the widgets bound to datasets that changed since the last flush. |
tui.data.bind
tui.data.bind NAME WIDGET KIND [OPTION=VALUE...]
Keeps a widget showing a dataset. Whenever the dataset changes the widget is updated, once, after the callback that changed it, and only when the text it would show is different.
Parameters
KIND: what the widget shows.
KIND |
Widget | Shows |
|---|---|---|
table |
<table> |
the --cols names as header and every row (show=name,cpu picks columns); a keyed dataset keeps the selected row |
list |
<list> |
one item per row, the cells of col |
label |
<label> |
the cell col of the newest row, through format (a printf format with one %s) |
spark |
<label> |
one block glyph per value of col, the newest last (default 24), width cells wide |
gauge |
<label> |
the newest value of col as a bar width cells wide (default 20) out of max (default 100) |
chart |
<pane> |
tui.chart of type (default linechart) over col (several, comma separated, for one series each), label and last, with the renderer flags in args |
OPTION=VALUE:colis a 0-based column index or a name (default 0). Binding a widget again replaces its binding.width=fit(spark, gauge) and-w fit/-h fitin a chart’sargstake the size of the pane the widget is in, less what the widget draws around itself. The size is read when the widget is drawn, so a resized terminal gets it at the next change of the data.key=ID(label, gauge): show the cellcolof that row of a keyed dataset instead of the newest row, so a block per node can show its own numbers.levels=WARN,CRIT(gauge): the widget is restyled with the theme classgauge_ok,gauge_warnorgauge_critby the percent ofmax.heat=col:WARN:HOT,...(table):tui.table.heaton those columns of the shown header;show=a,bpicks the columns.class=NAME(chart): the colours of that class are put back after every reset in the chart, so it keeps the background of its pane.
Notes
- In markup the same bindings are written as attributes:
<spark id="s" from="latency" col="ms" last="30"/>.
Returns: 1 for an unknown dataset or kind.
Example
tui.data.bind latency lbl_now label col=ms 'format=now %s ms'
tui.data.bind latency lbl_spark spark col=ms last=30
tui.data.unbind
tui.data.unbind WIDGET
Removes the binding of a widget. The widget keeps what it shows.
tui.data.flush
tui.data.flush
Updates the widgets bound to datasets that changed since the last flush.
Notes
- It runs by itself after every callback and on every tick, so most code never calls it. Call it inside a long callback to draw before the callback returns.
- A widget is only redrawn when the text it would show changed.
Keep
| Function | Summary |
|---|---|
tui.data.save |
Writes the rows of a dataset to $TUI_HOME/state/data/NAME: one row per line, with \ and newlines escaped, through a temporary file that is moved into place. |
tui.data.load |
Reads the disk copy of a dataset, replacing its rows. tui.data.new with --scope disk does this itself. |
tui.data.save
tui.data.save NAME
Writes the rows of a dataset to $TUI_HOME/state/data/NAME: one row per line, with \ and newlines escaped, through a temporary file that is moved into place.
Notes
- Datasets of scope
diskare written by themselves when the page is left and when the app exits.
Returns: 1 for an unknown dataset or without a TUI_HOME.
tui.data.load
tui.data.load NAME
Reads the disk copy of a dataset, replacing its rows. tui.data.new with --scope disk does this itself.
Returns: 1 for an unknown dataset, without a TUI_HOME, or when there is no copy (the rows stay as they are).
