load static method

Future<StatsRegistry> load(
  1. String path, {
  2. required VfsAdapter vfs,
})

Implementation

static Future<StatsRegistry> load(String path,
    {required VfsAdapter vfs}) async {
  final registry = StatsRegistry();
  if (!await vfs.exists(path)) return registry;

  try {
    final bytes = await vfs.readAll(path);
    if (bytes.isEmpty) return registry;
    final raw  = utf8.decode(bytes);
    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;
}