load static method
Implementation
static Future<StatsRegistry> load(String path) async {
final registry = StatsRegistry();
final f = File(path);
if (!await f.exists()) return registry;
try {
final raw = await f.readAsString();
final doc = jsonDecode(raw) as Map<String, dynamic>;
final ver = doc['version'] as int? ?? 1;
if (ver < 2) {
print('[Stats] Old stats format (v$ver) — ignoring, will re-analyze.');
return registry;
}
final tables = doc['tables'] as Map<String, dynamic>? ?? {};
for (final te in tables.entries) {
final td = te.value as Map<String, dynamic>;
final colData = td['columns'] as Map<String, dynamic>? ?? {};
final cols = <String, ColumnStats>{};
for (final ce in colData.entries) {
final cd = ce.value as Map<String, dynamic>;
cols[ce.key] = ColumnStats(
columnName: ce.key,
ndv: (cd['ndv'] as num).toInt(),
minValue: _deserializeValue(cd['minValue']),
maxValue: _deserializeValue(cd['maxValue']),
nullFraction: (cd['nullFraction'] as num).toDouble(),
);
}
final collectedAt = DateTime.tryParse(
td['collectedAt'] as String? ?? '') ?? DateTime(2000);
final ts = TableStats(
tableName: te.key,
rowCount: (td['rowCount'] as num).toInt(),
pageCount: (td['pageCount'] as num).toInt(),
columns: cols,
collectedAt: collectedAt,
);
registry.update(ts);
}
print('[Stats] Loaded statistics for ${tables.length} tables from $path');
} catch (e) {
print('[Stats] Warning: failed to load stats from $path: $e');
}
return registry;
}