Functional Weave
Code in Python

monitor.series-window

The samples of a time series from one moment up to (not including) another, checking they are in time order.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 12 tests, run in TypeScript, Python and Rust.

What it does

Picks the samples of a stored metric series that fall in a window, and owns the `MetricSample` type the other `monitor.*` metric capabilities share: `{ at, value }`, `at` in Unix seconds and `value` a whole number in whatever unit the metric uses (milliseconds, bytes, a request counter). Scale decimals to integers first (a CPU percentage in basis points), so every language agrees on every answer.

## Why half-open

For example

  • series_window(samples ×4, 150, 250) → ×2 keeps the samples inside the window
  • series_window(samples ×2, 100, 200) → ×2 a sample exactly at from is included
  • series_window(samples ×2, 100, 200) → ×1 a sample exactly at to is excluded, so back-to-back windows never share one

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def series_window(samples: Sequence[MetricSample], from_: int, to: int) -> List[MetricSample]
samplesMetricSample[]in strictly ascending time order, as a monitor stores them
from_intthe first second included, Unix seconds
tointthe first second excluded; equal to from is an empty window
returnsMetricSample[]

The type it declares, generated into your project

@dataclass(frozen=True)
class MetricSample:
    """One metric reading at a moment, in whole units (ms, bytes, requests); scale decimals first."""

    #: Unix seconds
    at: int
    value: int

Your code names it in one line, in the file that uses it

from fune.monitor.series_window import series_window  # monitor.series-window@^1
impl/python.py · 22 lines · open · raw
from typing import List, Sequence

from .monitor_series_window_types import MetricSample


def series_window(samples: Sequence[MetricSample], from_: int, to: int) -> List[MetricSample]:
    """The samples with from <= at < to. Half-open, so consecutive windows
    (the last hour, the hour before) never count a sample twice."""
    for v in (from_, to):
        if not isinstance(v, int) or isinstance(v, bool):
            raise ValueError("from and to must be whole seconds")
    if from_ > to:
        raise ValueError("from must not be after to: %d > %d" % (from_, to))
    out: List[MetricSample] = []
    for i, s in enumerate(samples):
        # Checked over the whole series, not just the window: a misordered
        # store gives wrong answers everywhere, so it should fail everywhere.
        if i > 0 and s.at <= samples[i - 1].at:
            raise ValueError("samples must be in strictly ascending time order: %d follows %d" % (s.at, samples[i - 1].at))
        if from_ <= s.at < to:
            out.append(MetricSample(at=s.at, value=s.value))
    return out

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add monitor.series-window
Download for Python monitor.series-window-1.0.0-python.fune · 6,476 bytes sha256 b36ea87763e9bc13eb345b4b980c8400e693154ae149448c94d33d0e4d0d1c77

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./monitor.series-window-1.0.0-python.fune, or fetch it from a terminal with fune pull monitor.series-window@1.0.0:python.

The whole function, every language, is one file too: monitor.series-window-1.0.0.fune, 9,491 bytes, sha256 b34498a4d87e3c9ae6c5bb56c9a194952205a5cd37f465b3e3485541c6053dca. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before monitor.series-window

after — your function gets the result and the arguments, and returns the final result.

# fune: after monitor.series-window

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show monitor.series-window --steps.

# fune: step monitor.series-window after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
keeps the samples inside the window samples ×4, 150, 250 → ×2
a sample exactly at from is included samples ×2, 100, 200 → ×2
a sample exactly at to is excluded, so back-to-back windows never share one samples ×2, 100, 200 → ×1
an empty window when from equals to samples ×1, 100, 100 →
an empty series is an empty window , 0, 1,000 →
a window before every sample is empty samples ×1, 0, 500 →
negative values and times before 1970 are kept as they are samples ×3, -60, 1 → ×2
a window covering everything returns every sample samples ×3, 0, 4 → ×3
from after to is an error , 200, 100 → error: from must not be after to: 200 > 100
two samples at the same second are an error samples ×2, 0, 1,000 → error: samples must be in strictly ascending time order: 100 follows 100
Show the other 2 tests
CaseArgumentsExpected
misordered samples outside the window are still an error samples ×2, 0, 50 → error: samples must be in strictly ascending time order: 100 follows 300
a fractional bound is an error , 0.5, 10 → error: from and to must be whole seconds

More from the author

The window is `from <= at < to`. Back-to-back windows (this hour, the hour before) then never count the same sample twice, which a closed window would.

## Order is checked

Samples must be strictly ascending in time. The whole series is checked, not only the part inside the window: a store that returns rows out of order gives wrong rates, alerts and uptimes everywhere, so it is refused everywhere. Two samples at the same second are refused too: which one is "the" value then is a guess.

## Errors

- `from must not be after to` - `samples must be in strictly ascending time order` - `from and to must be whole seconds`

Files

PathBytes
README.md1,069
impl/python.py1,076
impl/rust.rs1,814
impl/typescript.ts1,083
vectors.json2,082