stats method
Diagnostic snapshot of the keystore's state — total counts, TTL/TTB key counts, approximate size, oldest/newest timestamps. Intended for operator dashboards, not for runtime decisions on a hot path.
Implementation
@override
Future<KeyStoreStats> stats() async {
final row = _db.raw
.select('SELECT '
'COUNT(*) total, '
'SUM(CASE WHEN expires_at IS NOT NULL THEN 1 ELSE 0 END) ttl, '
'SUM(CASE WHEN available_at IS NOT NULL THEN 1 ELSE 0 END) ttb, '
"MIN(json_extract(metadata, '\$.createdAt')) oldest, "
"MAX(json_extract(metadata, '\$.createdAt')) newest "
'FROM at_data;')
.first;
DateTime? parse(Object? v) =>
v == null ? null : DateTime.tryParse(v as String);
return KeyStoreStats(
totalKeys: (row['total'] as int?) ?? 0,
ttlKeys: (row['ttl'] as int?) ?? 0,
ttbKeys: (row['ttb'] as int?) ?? 0,
sizeBytes:
_db.raw.select('PRAGMA page_count;').first.values.first as int? ?? 0,
oldestCreatedAt: parse(row['oldest']),
newestCreatedAt: parse(row['newest']),
);
}