Repository navigation
keep r-r of both records when two share a second #1056
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
Open
abdulsaheel
wants to merge
4
commits into
main
Choose a base branch
from
fix/decoded-rr-same-second-beats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3c172c2
keep both records' r-r when two share a second
abdulsaheel 90c072c
keep decoded_rr read order by beat_index
abdulsaheel 951d3c3
decoded_rr merge keeps beat_index dense
abdulsaheel 70bcb6a
agents.md: the beat-time merge is not whoop 4 only
abdulsaheel 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,186 @@ | ||
| // Two WHOOP 4 v24 records can share one Unix second (they are 0.9614 s apart). | ||
| // decoded_onehz keeps one row per second (newest wins), but the R-R beats of | ||
| // both records are kept: merged by beat time, so a re-offload is idempotent. | ||
| // | ||
| // The pair: counters 32324863 / 32324864, second 1775395280, sub-seconds 816 | ||
| // and 32320; the first carries R-R 569 and 903 ms, the second none. | ||
|
|
||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:openstrap_protocol/openstrap_protocol.dart' as proto; | ||
| import 'package:path/path.dart' as p; | ||
| import 'package:sqflite_common_ffi/sqflite_ffi.dart'; | ||
| import 'package:openstrap_edge/data/db.dart'; | ||
| import 'package:openstrap_edge/data/models.dart'; | ||
|
|
||
| const _base = | ||
| '2f1805f13ced01c261d2698848805454016202d802f5010000000000609f0fff80de233d' | ||
| '3dda19be5c87a9be7b14803f0020dc463dda19be5c87a9be7b14803f16028402bc028602' | ||
| '5e01000b010c020c0000000000370001510c000000000000'; | ||
| const _ts = 1775395280; | ||
|
|
||
| String _frame(int counter, int subsec, List<int> rr) { | ||
| final b = Uint8List.fromList(proto.hexToBytes(_base)); | ||
| final v = b.buffer.asByteData(); | ||
| v.setUint32(3, counter, Endian.little); | ||
| v.setUint32(7, _ts, Endian.little); | ||
| v.setUint16(11, subsec, Endian.little); | ||
| b[18] = rr.length; | ||
| for (var i = 0; i < rr.length; i++) { | ||
| v.setInt16(19 + 2 * i, rr[i], Endian.little); | ||
| } | ||
| return b.map((x) => x.toRadixString(16).padLeft(2, '0')).join(); | ||
| } | ||
|
|
||
| final _first = _frame(32324863, 816, [569, 903]); | ||
| final _second = _frame(32324864, 32320, []); | ||
| final _third = _frame(32324865, 20000, [800]); | ||
|
|
||
| RawRecord _raw(int counter, String hex) => RawRecord( | ||
| counter: counter, | ||
| packetType: 0x2F, | ||
| hex: hex, | ||
| capturedAt: _ts * 1000, | ||
| recTs: _ts, | ||
| ); | ||
|
|
||
| Future<List<Map<String, Object?>>> _beats() async => (await LocalDb.instance) | ||
| .rawQuery('SELECT rr_ms, beat_ts_ms FROM decoded_rr WHERE rec_ts = ? ' | ||
| 'ORDER BY beat_ts_ms', [_ts]); | ||
|
|
||
| Future<void> _reset() async { | ||
| final db = await LocalDb.instance; | ||
| await db.delete('decoded_rr'); | ||
| await db.delete('decoded_onehz'); | ||
| await db.delete(LocalDb.rawArchiveTable); | ||
| } | ||
|
|
||
| void main() { | ||
| setUpAll(() async { | ||
| sqfliteFfiInit(); | ||
| databaseFactory = databaseFactoryFfi; | ||
| LocalDb.dbName = 'openstrap_decoded_rr_same_second_test.db'; | ||
| final dir = await databaseFactory.getDatabasesPath(); | ||
| await databaseFactory.deleteDatabase(p.join(dir, LocalDb.dbName)); | ||
| }); | ||
|
|
||
| tearDownAll(() async { | ||
| await LocalDb.close(); | ||
| final dir = await databaseFactory.getDatabasesPath(); | ||
| await databaseFactory.deleteDatabase(p.join(dir, LocalDb.dbName)); | ||
| }); | ||
|
|
||
| setUp(_reset); | ||
|
|
||
| void expectTwoBeats(List<Map<String, Object?>> rows) { | ||
| expect([for (final r in rows) r['rr_ms']], [569, 903]); | ||
| expect(rows[0]['beat_ts_ms'], lessThan(rows[1]['beat_ts_ms'] as int)); | ||
| } | ||
|
|
||
| Future<List<Object?>> indices() async => [ | ||
| for (final r in await (await LocalDb.instance).rawQuery( | ||
| 'SELECT beat_index FROM decoded_rr WHERE rec_ts = ? ' | ||
| 'ORDER BY beat_index', [_ts])) | ||
| r['beat_index'], | ||
| ]; | ||
|
|
||
| for (final (name, order) in const [ | ||
| ('in order', [0, 1]), | ||
| ('reversed', [1, 0]), | ||
| ]) { | ||
| test('live ingest $name, then again: exactly two beats, one row', () async { | ||
| final recs = [(32324863, _first), (32324864, _second)]; | ||
| for (var pass = 0; pass < 2; pass++) { | ||
| for (final i in order) { | ||
| await LocalDb.insertRecord(_raw(recs[i].$1, recs[i].$2), null); | ||
| } | ||
| expectTwoBeats(await _beats()); | ||
| final db = await LocalDb.instance; | ||
| expect( | ||
| (await db.rawQuery('SELECT COUNT(*) AS n FROM decoded_onehz ' | ||
| 'WHERE rec_ts = ?', [_ts])).single['n'], | ||
| 1, | ||
| reason: 'one 1 Hz row per second'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test('A, C, A again leaves dense beat indices (the restore merge needs them)', | ||
| () async { | ||
| for (final (c, h) in [ | ||
| (32324863, _first), | ||
| (32324865, _third), | ||
| (32324863, _first), | ||
| ]) { | ||
| await LocalDb.insertRecord(_raw(c, h), null); | ||
| } | ||
| expect((await _beats()).length, 3); | ||
| expect(await indices(), [0, 1, 2]); | ||
| }); | ||
|
|
||
| test('a batch ingest of the pair keeps both records beats', () async { | ||
| await LocalDb.insertRecordsBatch( | ||
| [_raw(32324863, _first), _raw(32324864, _second)], | ||
| [null, null], | ||
| ); | ||
| expectTwoBeats(await _beats()); | ||
| }); | ||
|
|
||
| test('a record with no sub-second still replaces its second', () async { | ||
| final db = await LocalDb.instance; | ||
| await db.insert('decoded_rr', { | ||
| 'device_id': LocalDb.kPrimaryDeviceId, | ||
| 'ts_ms': _ts * 1000, | ||
| 'rec_ts': _ts, | ||
| 'beat_index': 0, | ||
| 'rr_ts_ms': _ts * 1000, | ||
| 'rr_ms': 700, | ||
| }); | ||
| // A pre-existing row with no beat time is replaced by the record's beats. | ||
| await LocalDb.insertRecord(_raw(32324863, _first), null); | ||
| expectTwoBeats(await _beats()); | ||
| }); | ||
|
|
||
| test('archive replay of the pair ends with two beats, and twice is the same', | ||
| () async { | ||
| final db = await LocalDb.instance; | ||
| for (final (c, h) in [(32324863, _first), (32324864, _second)]) { | ||
| await LocalDb.archiveRawRecord(ArchiveRecord( | ||
| counter: c, | ||
| hex: h, | ||
| packetType: 0x2F, | ||
| capturedAt: _ts * 1000, | ||
| reason: 'undecodable_rec_v24', | ||
| )); | ||
| } | ||
| await LocalDb.redriveArchivedRecords(db); | ||
| expectTwoBeats(await _beats()); | ||
| await LocalDb.redriveArchivedRecords(db); | ||
| expectTwoBeats(await _beats()); | ||
| expect( | ||
| (await db.rawQuery('SELECT COUNT(*) AS n FROM decoded_onehz ' | ||
| 'WHERE rec_ts = ?', [_ts])).single['n'], | ||
| 1); | ||
| }); | ||
|
|
||
| test('archive replay adds a neighbour beats to a second already decoded', | ||
| () async { | ||
| final db = await LocalDb.instance; | ||
| // The second was decoded live (record two, no beats), the first record | ||
| // only sits in the archive. | ||
| await LocalDb.insertRecord(_raw(32324864, _second), null); | ||
| expect(await _beats(), isEmpty); | ||
| await LocalDb.archiveRawRecord(ArchiveRecord( | ||
| counter: 32324863, | ||
| hex: _first, | ||
| packetType: 0x2F, | ||
| capturedAt: _ts * 1000, | ||
| reason: 'undecodable_rec_v24', | ||
| )); | ||
| await LocalDb.redriveArchivedRecords(db); | ||
| expectTwoBeats(await _beats()); | ||
| await LocalDb.redriveArchivedRecords(db); | ||
| expectTwoBeats(await _beats()); | ||
| }); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.