Repository navigation
perf: append measurements to CSV and SQLite instead of rewriting - #85
Merged
Merged
Conversation
Every measurement read the whole data file, added one row and wrote the whole file again, so storing became slower as data grew (e.g. 184 ms per measurement with 100,000 CSV rows), all data was kept in memory, and concurrent requests could lose measurements. - CSV: only the header is read and the row is appended (about 3 ms per measurement with 1,000,000 rows). If the header lacks a configured column (e.g. a column was added, or timestamps enabled later), the file is rewritten once with the new column - SQLite: rows are inserted; missing columns are added with ALTER TABLE. New tables have untyped columns, so values keep their type (a first measurement with a missing value no longer makes a column text). Lists and objects are stored as JSON - JSON and XML still rewrite the file (to be changed separately) - Lock the stored data while writing and reading (lock file next to the data file, works between threads and processes on Linux, macOS and Windows), so concurrent measurements, also from several gunicorn workers, are all stored, and /data, /export and /explore never read a half-written row - Values are stored as sent (e.g. 21 no longer becomes 21.0), and earlier rows are no longer changed by later measurements - Fix a 500 error when several processes create the data folder at the same time - Keep only the last stored row in memory after a measurement - Document the storage and the lock file, and add tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCvw8haRnmyrVKrLkz5Yf8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every
POST /measureread the whole data file, added one row withpd.concatand wrote the whole file again (SQLite:to_sql(if_exists='replace'), i.e. the table was recreated). Consequences:Storing gets slower as data grows. Measured per measurement:
100,000 rows is about a year of 5-minute measurements; a Raspberry Pi is several times slower than the machine used here.
All data stayed in memory (
Processing.df).Concurrent measurements were lost. With 6 processes (like gunicorn workers) sending 150 measurements, only 43 (CSV), 37 (SQLite) and 36 (JSON) were stored, and many requests failed.
Changes
nrows=0), then the row is appended. If the header lacks a configured column (column added to the configuration, timestamps enabled later), the file is rewritten once with the new column; then appending continues. Stored columns that are no longer configured stay and are empty for new rows. A file without a trailing newline is handled.INSERTof the new row; missing columns are added withALTER TABLE. New tables are created with untyped columns, so values keep the type they were sent with (with pandas' type inference, a first measurement with a missing value made a columnTEXTpermanently). Lists/objects are stored as JSON instead of failing. Existing databases created by older versions keep working.Processing.storage_lock()uses a lock file next to the data file (data/.data.csv.lock):fcntl.flockon Linux/macOS,msvcrt.lockingon Windows. It works between threads and processes./data,/exportand/exploretake the same lock, so they never read a half-written row.dtype=object, so e.g.21is no longer written as21.0, and earlier rows are no longer re-formatted by later measurements (e.g."007"→7).os.makedirs(..., exist_ok=True); before, several processes creating it at the same time got a 500 error.Processing.dfholds only the last stored row after a measurement (all data after/dataor/export, as before).Processing.load()is shared by/data,/exportand/explore.File formats are unchanged, so existing data files keep working without conversion.
Tests
tests/test_storage.py(20 tests): CSV append and file content, only the header is read, earlier values unchanged, new/removed columns, timestamps enabled later, missing trailing newline, empty file, quoting; SQLite insert, no full read,ALTER TABLEwith a quoted column name, JSON for lists/objects, first value missing, databases from older versions; concurrent writes from threads and from 4 processes for all four formats; reading while writing.ruff checkpasses.Generated by Claude Code