collect static method

TableStats collect(
  1. String tableName,
  2. List<Map<String, dynamic>> rows
)

Build TableStats from a list of row maps.

Implementation

static TableStats collect(
    String tableName, List<Map<String, dynamic>> rows) {
  final rowCount = rows.length;
  final pageCount = (rowCount / _rowsPerPage).ceil().clamp(1, 999999);

  // Gather per-column stats
  final colNames = rows.isNotEmpty ? rows.first.keys.toSet() : <String>{};
  final colStats = <String, ColumnStats>{};

  for (final col in colNames) {
    final vals = rows.map((r) => r[col]).toList();
    final nonNull = vals.where((v) => v != null).toList();
    final nullFrac = rows.isEmpty ? 0.0 : (vals.length - nonNull.length) / vals.length;

    final distinct = nonNull.toSet();
    final ndv = distinct.length;

    dynamic minV, maxV;
    for (final v in nonNull) {
      if (v is num) {
        if (minV == null || v < (minV as num)) minV = v;
        if (maxV == null || v > (maxV as num)) maxV = v;
      } else if (v is String) {
        if (minV == null || v.compareTo(minV as String) < 0) minV = v;
        if (maxV == null || v.compareTo(maxV as String) > 0) maxV = v;
      }
    }

    colStats[col] = ColumnStats(
      columnName: col,
      ndv: ndv,
      minValue: minV,
      maxValue: maxV,
      nullFraction: nullFrac,
    );
  }

  return TableStats(
    tableName: tableName,
    rowCount: rowCount,
    pageCount: pageCount,
    columns: colStats,
  );
}