Programming · free software · the open webSubscribe via RSS

How Can You Build a Sensor Gap Reporter?

From the earlier reference collection. Read the current software blog.

Compare ordered observation times with an expected cadence and report intervals that exceed a configurable threshold.

Set the cadence and threshold

A sensor gap reporter looks at observation timestamps and identifies intervals longer than expected. It needs a cadence, such as one reading every five minutes, and a threshold that defines a noteworthy gap. Those are separate choices: a system might expect five-minute readings but alert only when an interval exceeds fifteen minutes.

Accept timestamps in a documented format and parse their offsets. If the source supplies local times without a zone, make that assumption visible or reject the input as ambiguous. A gap calculation is only as reliable as the time interpretation behind it.

Sort and compare observations

Parse each valid timestamp, then order observations by instant. For each consecutive pair, subtract the earlier time from the later time. If the interval exceeds the configured threshold, report both endpoints and the duration. Sorting matters because input files may not arrive in chronological order; comparing file order can invent gaps or miss real ones.

For example, readings at 10:00, 10:05, and 10:20 with a five-minute cadence produce intervals of five and fifteen minutes. With a threshold of ten minutes, only the second interval is reported. State whether the threshold is inclusive: “greater than ten” differs from “ten or more.”

Keep duplicate and invalid times visible

Two records with the same timestamp have a zero-length interval, but may still indicate duplicate data. Count or report duplicates separately so they do not disappear inside the gap summary. Invalid timestamps should be tied to row numbers and either excluded with an explicit count or treated as a fatal input error.

The reporter also cannot determine why a gap occurred. It can show an interval with no observations in the file, but the cause might be communications, device downtime, a planned maintenance period, or an export omission. Label the output as an observed data gap, not proof of a sensor failure.

Report boundaries and limitations

Include the earliest and latest observation, number of valid timestamps, expected cadence, gap threshold, and each flagged interval. If there are fewer than two valid readings, say that there are not enough points to calculate intervals. A result with no long gaps should still include the threshold used.

Test an ordinary sequence, an out-of-order sequence, a duplicate, and a malformed timestamp. These examples exercise the main decisions without requiring a live sensor. The useful result is a concise list someone can compare with collection logs, plus enough context to reproduce the gap rule.

Suggest a correction · All posts