save static method

Future<void> save(
  1. StatsRegistry registry,
  2. String path, {
  3. required VfsAdapter vfs,
})

Implementation

static Future<void> save(StatsRegistry registry, String path,
    {required VfsAdapter vfs}) async {
  final tables = <String, dynamic>{};
  for (final entry in registry.allStats.entries) {
    final ts   = entry.value;
    final cols = <String, dynamic>{};
    for (final ce in ts.columnStats.entries) {
      final cs = ce.value;
      cols[ce.key] = {
        'ndv':          cs.ndv,
        'minValue':     _serializeValue(cs.minValue),
        'maxValue':     _serializeValue(cs.maxValue),
        'nullFraction': cs.nullFraction,
      };
    }
    tables[entry.key] = {
      'rowCount':    ts.rowCount,
      'pageCount':   ts.pageCount,
      'collectedAt': ts.collectedAt.toIso8601String(),
      'columns':     cols,
    };
  }

  final doc  = {
    'version': _version,
    'savedAt': DateTime.now().toIso8601String(),
    'tables':  tables,
  };

  final json  = const JsonEncoder.withIndent('  ').convert(doc);
  final bytes = Uint8List.fromList(utf8.encode(json));
  await vfs.writeAll(path, bytes);
  print('[Stats] Saved statistics for ${tables.length} tables to $path');
}