Repository navigation
fix(export): preserve legacy non-object event data in CSV #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ErikBjare
merged 2 commits into
ActivityWatch:master
from
TimeToBuildBob:fix/csv-legacy-nonobject-data
Nov 17, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| """CSV export must remain readable for previously accepted non-object data.""" | ||
|
|
||
| import csv | ||
| import io | ||
| import json | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| import pytest | ||
| from aw_core.models import Event | ||
| from aw_server.csv_export import MAX_CSV_DATA_COLUMNS, events_to_csv | ||
| from aw_server.server import AWFlask | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("legacy_data", [["x"], "legacy", 5, 1.5, True]) | ||
| def test_legacy_nonobject_csv_stream_roundtrip(legacy_data): | ||
| app = AWFlask("127.0.0.1", testing=True) | ||
| client = app.test_client() | ||
| bucket = "test-legacy-csv" | ||
| response = client.post( | ||
| f"/api/0/buckets/{bucket}", | ||
| json={"client": "test", "type": "test", "hostname": "test"}, | ||
| ) | ||
| assert response.status_code == 200 | ||
| payloads = [{"title": "=SUM(A1:A2)", "unicode": 'å\n"'}, legacy_data] | ||
| for index, data in enumerate(payloads): | ||
| response = client.post( | ||
| f"/api/0/buckets/{bucket}/events", | ||
| json={ | ||
| "timestamp": f"2026-01-01T00:00:0{index}Z", | ||
| "duration": 1.25, | ||
| "data": data, | ||
| }, | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
| # Headers alone cannot detect exceptions in the streamed body. | ||
| response = client.get(f"/api/0/buckets/{bucket}/export/csv") | ||
| assert response.status_code == 200 | ||
| rows = list(csv.reader(io.StringIO(response.get_data(as_text=True)))) | ||
| assert rows[0] == ["id", "timestamp", "duration", "data"] | ||
| assert len(rows) == 3 | ||
| assert [json.loads(row[3]) for row in rows[1:]] == payloads[::-1] | ||
| assert [row[2] for row in rows[1:]] == ["1.250000000"] * 2 | ||
| assert [row[1] for row in rows[1:]] == [ | ||
| "2026-01-01T00:00:01+00:00", | ||
| "2026-01-01T00:00:00+00:00", | ||
| ] | ||
| # Exporting must not rewrite the stored payloads. | ||
| stored = client.get(f"/api/0/buckets/{bucket}/events").get_json() | ||
| assert [event["data"] for event in stored] == payloads[::-1] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("data", [[], False, 0, ""]) | ||
| def test_legacy_nonobject_csv_falsey_data_preserved(data): | ||
| # aw-core's constructor normalizes falsey data to {}. Seed historical | ||
| # values explicitly to test the exporter boundary without changing ingest. | ||
| event = Event(timestamp=datetime(2026, 1, 1, tzinfo=timezone.utc)) | ||
| event.data = data | ||
| rows = list(csv.reader(events_to_csv([event]))) | ||
| assert rows[0] == ["id", "timestamp", "duration", "data"] | ||
| assert json.loads(rows[1][3]) == data | ||
|
|
||
|
|
||
| def test_legacy_nonobject_csv_null_uses_model_semantics(): | ||
| event = Event(timestamp=datetime(2026, 1, 1, tzinfo=timezone.utc), data=None) | ||
| assert event.data == {} | ||
| event.data = None | ||
| assert event.data == {} # The property also normalizes an explicitly stored null. | ||
| rows = list(csv.reader(events_to_csv([event]))) | ||
| assert rows[0] == ["id", "timestamp", "duration"] | ||
| assert len(rows[1]) == 3 | ||
|
|
||
|
|
||
| def test_legacy_nonobject_csv_object_only_compatibility(): | ||
| event = Event( | ||
| id=7, | ||
| timestamp=datetime(2026, 1, 1, tzinfo=timezone(timedelta(hours=1))), | ||
| duration=timedelta(microseconds=1234567), | ||
| data={ | ||
| "id": "=1+1", | ||
| "data.id": " @command", | ||
| "title": 'å,"line\nnext', | ||
| "null": None, | ||
| "nested": {"a": [1, True]}, | ||
| }, | ||
| ) | ||
| second = Event(timestamp=event.timestamp, data={"other": "-formula"}) | ||
| rows = list(csv.reader(events_to_csv([event, second]))) | ||
| assert rows[0] == [ | ||
| "id", | ||
| "timestamp", | ||
| "duration", | ||
| "data.id", | ||
| "data.data.id", | ||
| "title", | ||
| "null", | ||
| "nested", | ||
| "other", | ||
| ] | ||
| assert rows[1] == [ | ||
| "7", | ||
| "2025-12-31T23:00:00+00:00", | ||
| "1.234567000", | ||
| "'=1+1", | ||
| "' @command", | ||
| 'å,"line\nnext', | ||
| "null", | ||
| '{"a":[1,true]}', | ||
| "", | ||
| ] | ||
| assert rows[2][3:8] == [""] * 5 | ||
| assert rows[2][8] == "'-formula" | ||
|
|
||
|
|
||
| def test_legacy_nonobject_csv_wide_object_fallback_unchanged(): | ||
| data = {f"key{i}": i for i in range(MAX_CSV_DATA_COLUMNS + 1)} | ||
| rows = list(csv.reader(events_to_csv([Event(data=data)]))) | ||
| assert rows[0] == ["id", "timestamp", "duration", "data"] | ||
| assert json.loads(rows[1][3]) == data | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [-5, -1.5, 0, 7]) | ||
| def test_numeric_json_is_not_formula_neutralized(value): | ||
| event = Event( | ||
| timestamp=datetime(2026, 1, 1, tzinfo=timezone.utc), data={"n": value} | ||
| ) | ||
| legacy = Event(timestamp=datetime(2026, 1, 2, tzinfo=timezone.utc)) | ||
| legacy.data = value | ||
| wide = Event( | ||
| timestamp=datetime(2026, 1, 3, tzinfo=timezone.utc), | ||
| data={f"k{i}": value for i in range(MAX_CSV_DATA_COLUMNS + 1)}, | ||
| ) | ||
| # Per-key column, scalar data column and wide-object JSON column. | ||
| assert list(csv.reader(events_to_csv([event])))[1][3] == json.dumps(value) | ||
| assert json.loads(list(csv.reader(events_to_csv([legacy])))[1][3]) == value | ||
| assert json.loads(list(csv.reader(events_to_csv([wide])))[1][3])["k0"] == value | ||
|
|
||
|
|
||
| def test_formula_text_still_neutralized_next_to_numbers(): | ||
| event = Event( | ||
| timestamp=datetime(2026, 1, 1, tzinfo=timezone.utc), | ||
| data={"title": "=SUM(A1:A2)", "text": "-5", "n": -5}, | ||
| ) | ||
| row = list(csv.reader(events_to_csv([event])))[1] | ||
| assert row[3:] == ["'=SUM(A1:A2)", "'-5", "-5"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")]) | ||
| def test_nonfinite_numbers_are_not_trusted_as_numbers(value): | ||
| event = Event(timestamp=datetime(2026, 1, 1, tzinfo=timezone.utc)) | ||
| event.data = value | ||
| cell = list(csv.reader(events_to_csv([event])))[1][3] | ||
| assert not cell.startswith(("-", "=")) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_jsonnow callsmath.isfiniteon integers, which raisesOverflowErrorfor valid JSON integers such as10**400. An event withdata={"n": 10**400}therefore stops a CSV export after the header, even thoughjson.dumpscan preserve the value.Treat integers as finite directly and call
math.isfiniteonly for floats. Add a large-integer regression.