save static method

Future<void> save(
  1. StatsRegistry registry,
  2. String path
)

Implementation

static Future<void> save(StatsRegistry registry, String path) 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 f    = File(path);
  await f.writeAsString(json, flush: false); // best-effort, no fsync
  print('[Stats] Saved statistics for ${tables.length} tables to $path');
}