Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

Recipe

CLI and standard pipes: Available — v0.0.1 (experimental)no extra runtime

Moving averages and windows

std:window groups the readings of each sensor into sliding windows of three records, and a short Starlark pipe turns every window into its mean. Runs inside the runner with no extra runtime; the R version is linked.

How do I compute a moving average over a stream of records?

Sensor readings arrive in time order, several sensors interleaved. You want the mean of the last three readings of each sensor, updated with every new reading.

A moving average is two separate jobs: deciding which records belong together, and aggregating each group. The first is the standard pipe std:window. The second is yours, and here it is written in Starlark, which runs inside the runner.

A pipe is a data-processing step with declared inputs and outputs; a pipeline connects pipes.

What you need

  • The openflange command, release v0.0.1:
    bash
    go install github.com/openflange/openflange-go/cmd/openflange@v0.0.1
  • An empty directory. Create the files below in it under exactly these names and run every command from that directory.
  • Nothing else: every step of this recipe runs inside the runner. No network, no account, no key.

Steps

1. Save the input file

Eight readings from two sensors, a and b, interleaved as they arrived.

readings.ndjson
{"sensor":"a","t":"2026-12-01T10:00:00Z","value":10}
{"sensor":"b","t":"2026-12-01T10:00:00Z","value":100}
{"sensor":"a","t":"2026-12-01T10:01:00Z","value":20}
{"sensor":"a","t":"2026-12-01T10:02:00Z","value":30}
{"sensor":"b","t":"2026-12-01T10:01:00Z","value":110}
{"sensor":"a","t":"2026-12-01T10:03:00Z","value":40}
{"sensor":"b","t":"2026-12-01T10:02:00Z","value":90}
{"sensor":"a","t":"2026-12-01T10:04:00Z","value":50}

2. Describe the pipeline and its contract

size is the length of the window, step: 1 slides it by one record, partitionBy keeps one window per sensor, and emitPartial: false drops the windows that never filled.

pipeline.yaml
# records ─▶ window (std:window, sliding, per sensor) ─▶ mean (Starlark, one frame at a time) ─▶ means
spec: openflange.org/pipeline/v0.1
nodes:
  window:
    pipe: std:window
    inputs:
      in: {from: inputs.records}
    parameters:
      size: {$param: size}
      step: 1                     # slide by one record
      partitionBy: 'item["sensor"]'
      emitPartial: false          # only full windows: a true moving average
  mean:
    pipe: ./pipes/window-mean
    inputs:
      windows: {from: window.outputs.out}
outputs:
  means: {from: mean.outputs.means}
OPENFLANGE.md
---
spec: openflange.org/pipe/v0.1
id: moving-average
version: 0.1.0
title: Moving average per sensor
description: The mean of the last `size` readings of every sensor, one record per full window.
license: Apache-2.0
keywords: [window, moving-average, sliding, starlark, recipe]
maturity: experimental
scope: record
requires: [windows]
inputs:
  records:
    description: 'Readings in time order: {"sensor", "t", "value"}; `value` must be a number.'
outputs:
  means:
    description: 'One record per full window: {"sensor", "from", "to", "count", "mean"}.'
parameters:
  size:
    type: integer
    default: 3
    minimum: 1
    maximum: 1000000
    description: Number of readings in a window.
implementation:
  type: pipeline
  path: pipeline.yaml
---
# Moving average per sensor

Implements the [OpenFlange](https://openflange.org/) specification.

`std:window` groups the readings of each sensor into sliding windows of
`size` records; `window-mean` turns every window frame into one record.
At most `size` readings per sensor are held in memory. Permissions: none.

3. Write the aggregate

The input port has kind: windows: each item is one frame with its position and its records. The function returns one record per frame.

pipes/window-mean/OPENFLANGE.md
---
spec: openflange.org/pipe/v0.1
id: window-mean
version: 0.1.0
title: Window mean (Starlark)
description: The mean of the `value` member of each window frame, with the time of its first and last item.
license: Apache-2.0
keywords: [statistics, window, mean, starlark, recipe]
maturity: experimental
scope: window
requires: [windows]
cardinality: one-to-one
deterministic: true
inputs:
  windows:
    kind: windows
    description: 'Window frames whose items are records with a numeric `value` and a `t` member.'
outputs:
  means:
    description: 'One record per window: {"sensor", "from", "to", "count", "mean"}; `sensor` is the partition of the window.'
implementation:
  type: script
  language: starlark
  path: mean.star
---
# Window mean (Starlark)

Implements the [OpenFlange](https://openflange.org/) specification.

One window frame in, one record out. Runs in-process; no permissions.
A frame whose items lack a numeric `value` fails the run (`E_RUNTIME`).
pipes/window-mean/mean.star
# SPDX-License-Identifier: Apache-2.0
# window-mean: one record per window frame of std:window.

def process(frame, params):
    items = frame["items"]
    total = 0.0
    for r in items:
        v = r["value"]
        if type(v) not in ("int", "float"):
            fail("value must be a number, got %r" % v)
        total += v
    return {
        "sensor": frame["window"]["partition"],
        "from": items[0]["t"],
        "to": items[-1]["t"],
        "count": len(items),
        "mean": total / len(items),
    }

4. Validate and run

The readings on standard input, the means on standard output.

bash
openflange validate .
Output
valid: . (2 nodes, 3 edges)
bash
openflange run . < readings.ndjson
Output
{"sensor":"a","from":"2026-12-01T10:00:00Z","to":"2026-12-01T10:02:00Z","count":3,"mean":20}
{"sensor":"a","from":"2026-12-01T10:01:00Z","to":"2026-12-01T10:03:00Z","count":3,"mean":30}
{"sensor":"b","from":"2026-12-01T10:00:00Z","to":"2026-12-01T10:02:00Z","count":3,"mean":100}
{"sensor":"a","from":"2026-12-01T10:02:00Z","to":"2026-12-01T10:04:00Z","count":3,"mean":40}
completed in 388µs — means: 4 records

Sensor a has five readings, so three full windows; sensor b has three, so one. A window is emitted as soon as its last reading arrives, which is why the lines of the two sensors interleave.

5. Look at the window frames

Run std:window on its own to see what the aggregate receives: one line per window, with the partition, the position and the items.

bash
openflange run std:window --quiet --param size=3 --param step=1 --param 'partitionBy=item["sensor"]' --param emitPartial=false < readings.ndjson
Output
{"window":{"index":0,"partition":"a","start":0,"end":2,"final":false,"partial":false},"items":[{"sensor":"a","t":"2026-12-01T10:00:00Z","value":10},{"sensor":"a","t":"2026-12-01T10:01:00Z","value":20},{"sensor":"a","t":"2026-12-01T10:02:00Z","value":30}]}
{"window":{"index":1,"partition":"a","start":1,"end":3,"final":false,"partial":false},"items":[{"sensor":"a","t":"2026-12-01T10:01:00Z","value":20},{"sensor":"a","t":"2026-12-01T10:02:00Z","value":30},{"sensor":"a","t":"2026-12-01T10:03:00Z","value":40}]}
{"window":{"index":0,"partition":"b","start":0,"end":2,"final":false,"partial":false},"items":[{"sensor":"b","t":"2026-12-01T10:00:00Z","value":100},{"sensor":"b","t":"2026-12-01T10:01:00Z","value":110},{"sensor":"b","t":"2026-12-01T10:02:00Z","value":90}]}
{"window":{"index":2,"partition":"a","start":2,"end":4,"final":false,"partial":false},"items":[{"sensor":"a","t":"2026-12-01T10:02:00Z","value":30},{"sensor":"a","t":"2026-12-01T10:03:00Z","value":40},{"sensor":"a","t":"2026-12-01T10:04:00Z","value":50}]}

6. Change the window size

The size is a parameter of the pipeline, so a different window needs no edit.

bash
openflange run . --param size=2 < readings.ndjson
Output
{"sensor":"a","from":"2026-12-01T10:00:00Z","to":"2026-12-01T10:01:00Z","count":2,"mean":15}
{"sensor":"a","from":"2026-12-01T10:01:00Z","to":"2026-12-01T10:02:00Z","count":2,"mean":25}
{"sensor":"b","from":"2026-12-01T10:00:00Z","to":"2026-12-01T10:01:00Z","count":2,"mean":105}
{"sensor":"a","from":"2026-12-01T10:02:00Z","to":"2026-12-01T10:03:00Z","count":2,"mean":35}
{"sensor":"b","from":"2026-12-01T10:01:00Z","to":"2026-12-01T10:02:00Z","count":2,"mean":100}
{"sensor":"a","from":"2026-12-01T10:03:00Z","to":"2026-12-01T10:04:00Z","count":2,"mean":45}
completed in 426µs — means: 6 records

7. Test it

One test file for the pipeline and one for the aggregate on a hand-written frame. Means are compared with a tolerance because they are floating-point numbers.

tests/moving-average.test.yaml
spec: openflange.org/test/v0.1
pipe: ..
tests:
  - name: a moving average of three readings, separately for every sensor
    inputs:
      records:
        values:
          - {sensor: a, t: "10:00", value: 10}
          - {sensor: b, t: "10:00", value: 100}
          - {sensor: a, t: "10:01", value: 20}
          - {sensor: a, t: "10:02", value: 30}
          - {sensor: b, t: "10:01", value: 110}
          - {sensor: a, t: "10:03", value: 40}
          - {sensor: b, t: "10:02", value: 90}
    expect:
      outputs:
        means:
          values:
            - {sensor: a, from: "10:00", to: "10:02", count: 3, mean: 20}
            - {sensor: a, from: "10:01", to: "10:03", count: 3, mean: 30}
            - {sensor: b, from: "10:00", to: "10:02", count: 3, mean: 100}
          tolerance: 1e-9

  - name: the window size is a parameter
    parameters: {size: 2}
    inputs:
      records:
        values:
          - {sensor: a, t: "10:00", value: 10}
          - {sensor: a, t: "10:01", value: 20}
          - {sensor: a, t: "10:02", value: 40}
    expect:
      outputs:
        means:
          values:
            - {sensor: a, from: "10:00", to: "10:01", count: 2, mean: 15}
            - {sensor: a, from: "10:01", to: "10:02", count: 2, mean: 30}
          tolerance: 1e-9

  - name: fewer readings than the window size give no output
    inputs:
      records:
        values:
          - {sensor: a, t: "10:00", value: 10}
          - {sensor: a, t: "10:01", value: 20}
    expect:
      outputs:
        means: {count: 0}
pipes/window-mean/tests/window-mean.test.yaml
spec: openflange.org/test/v0.1
tests:
  - name: the mean of one window frame
    inputs:
      windows:
        values:
          - window: {index: 0, partition: a, start: 0, end: 2, final: false, partial: false}
            items: [{t: "10:00", value: 1}, {t: "10:01", value: 2.5}, {t: "10:02", value: 4}]
    expect:
      outputs:
        means:
          values: [{sensor: a, from: "10:00", to: "10:02", count: 3, mean: 2.5}]
          tolerance: 1e-9

  - name: a value that is not a number fails the run
    inputs:
      windows:
        values:
          - window: {index: 0, partition: a, start: 0, end: 0, final: false, partial: false}
            items: [{t: "10:00", value: "n/a"}]
    expect:
      status: failed
      error: {code: E_RUNTIME}
bash
openflange test . ./pipes/window-mean
Output
tests/moving-average.test.yaml
  ok    a moving average of three readings, separately for every sensor
  ok    the window size is a parameter
  ok    fewer readings than the window size give no output
pipes/window-mean/tests/window-mean.test.yaml
  ok    the mean of one window frame
  ok    a value that is not a number fails the run
ok 5 tests passed in 2 files (22ms)

Limits

  • Windows are counted in records, not in time: this is "the last three readings", not "the last three minutes". std 0.1 also has std:window-processing-time, a draft pipe that groups by arrival time and is not deterministic; it has no window by a timestamp inside the record.
  • The input must already be in order. Nothing here sorts.
  • With emitPartial: false the first size − 1 readings of every sensor produce no output.
  • Memory is bounded: at most size records per partition, and at most maxPartitions partitions (default 10,000). One partition more fails the run with E_RESOURCE.
  • The mean is a 64-bit float. For exact sums, add integers.
  • The window framing is marked "proposed" in v0.1 and may change.
  • The R version was not run here (R is not installed on the machine where these pages are verified). The same pipeline with an R aggregate is the R moving window example, with output taken from the repository.

Every command on this page was run on Linux with openflange 0.0.1 (go1.27.1 linux/amd64), built from openflange-go commit 11cf8cc5378d70858724b16aa937d7f3b98c085d. The files and the recorded output are kept in the site repository under sites/dev/test/fixtures/recipes/moving-average/, and a test compares this page with them byte for byte.