65 lines
2.1 KiB
Dart
65 lines
2.1 KiB
Dart
// Pages a session's history exactly as ChatState does, and reports whether
|
|||
|
|
// every message is reached. Run against a live server:
|
||
|
|
// dart run tool/history_probe.dart <sessionId> [directory]
|
||
|
|
import 'dart:io';
|
||
|
|
|
||
|
|
import 'package:openchamber/api/client.dart';
|
||
|
|
|
||
|
|
Future<void> main(List<String> args) async {
|
||
|
|
final id = args.isEmpty ? '' : args[0];
|
||
|
|
final dir = args.length > 1
|
||
|
|
? args[1]
|
||
|
|
: Directory.current.path;
|
||
|
|
if (id.isEmpty) {
|
||
|
|
print('usage: dart run tool/history_probe.dart <sessionId> [directory]');
|
||
|
|
exit(2);
|
||
|
|
}
|
||
|
|
final client = Client(Uri.parse('http://127.0.0.1:3000'));
|
||
|
|
|
||
|
|
// 1. What ChatState loads first, then each "load earlier" page.
|
||
|
|
final first = await client.listMessages(id, limit: 30, directory: dir);
|
||
|
|
final seen = <String>{for (final m in first.data) m.id};
|
||
|
|
print('page 1: ${first.data.length} msgs next=${first.next != null}');
|
||
|
|
var cursor = first.next;
|
||
|
|
var page = 2;
|
||
|
|
var oldest = 0;
|
||
|
|
while (cursor != null && page <= 200) {
|
||
|
|
final p = await client.listMessages(id,
|
||
|
|
limit: 30, cursor: cursor, directory: dir);
|
||
|
|
if (p.data.isEmpty) {
|
||
|
|
print('page $page: empty (cursor exhausted)');
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
final added = p.data.where((m) => seen.add(m.id)).length;
|
||
|
|
oldest = p.data
|
||
|
|
.map((m) => m.created)
|
||
|
|
.reduce((a, b) => a < b ? a : b);
|
||
|
|
print('page $page: ${p.data.length} msgs (+$added new, '
|
||
|
|
'oldest=${DateTime.fromMillisecondsSinceEpoch(oldest)})');
|
||
|
|
cursor = p.next;
|
||
|
|
page++;
|
||
|
|
if (added == 0) {
|
||
|
|
print('no progress at page $page: stopping');
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. The full list, for comparison.
|
||
|
|
final all = <String>{};
|
||
|
|
var c = null as String?;
|
||
|
|
do {
|
||
|
|
final p = await client.listMessages(id,
|
||
|
|
limit: 200, cursor: c, directory: dir);
|
||
|
|
all.addAll(p.data.map((m) => m.id));
|
||
|
|
c = p.next;
|
||
|
|
} while (c != null);
|
||
|
|
|
||
|
|
print('----');
|
||
|
|
print('paged: ${seen.length} unique messages');
|
||
|
|
print('total: ${all.length} messages');
|
||
|
|
print('missing: ${all.difference(seen).length}');
|
||
|
|
final missing = all.difference(seen);
|
||
|
|
if (missing.isNotEmpty) print('first missing: ${missing.take(3).join(', ')}');
|
||
|
|
client.close();
|
||
|
|
exit(0);
|
||
|
|
}
|