RelationalCapacityEstimate.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132
  1. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors;
  2. public sealed record RelationalCapacityBatch(int Rows, long RawBytes, decimal SourceMs,
  3. decimal RawMs, decimal CleanMs, decimal HandoffMs, decimal ReconcileMs)
  4. {
  5. public decimal TotalMs => SourceMs + RawMs + CleanMs + HandoffMs + ReconcileMs;
  6. }
  7. public sealed record RelationalCapacityEstimate(long? SourceRows, string SourceRowsBasis, int SampleRows, int SampleBatches,
  8. long SampleRawBytes, decimal? RawBytesPerRow, long? FullRawBytes, decimal? FullSecondsLow, decimal? FullSecondsHigh)
  9. {
  10. public static RelationalCapacityEstimate FromSamples(long? catalogRows, IReadOnlyList<RelationalCapacityBatch> batches,
  11. bool reachedEnd = false)
  12. {
  13. var valid = batches.Where(batch => batch.Rows > 0 && batch.RawBytes > 0).ToArray();
  14. var rows = valid.Sum(batch => batch.Rows);
  15. var bytes = valid.Sum(batch => batch.RawBytes);
  16. long? sourceRows = reachedEnd ? rows : catalogRows.HasValue ? Math.Max(catalogRows.Value, rows) : null;
  17. var basis = reachedEnd ? "sample" : catalogRows is null ? "unknown" : catalogRows < rows ? "sample-floor" : "catalog";
  18. if (rows == 0)
  19. return new(sourceRows, basis, 0, 0, 0, null, sourceRows == 0 ? 0 : null, null, null);
  20. var bytesPerRow = (decimal)bytes / rows;
  21. var projectedBytes = decimal.Ceiling(bytesPerRow * sourceRows.GetValueOrDefault());
  22. var measuredRates = valid.Where(batch => batch.TotalMs > 0).Select(batch => batch.TotalMs / batch.Rows).ToArray();
  23. decimal? Forecast(decimal rate) => sourceRows.HasValue ? decimal.Round(rate * sourceRows.Value / 1000, 3) : null;
  24. return new(sourceRows, basis, rows, valid.Length, bytes, decimal.Round(bytesPerRow, 2),
  25. sourceRows.HasValue && projectedBytes <= long.MaxValue ? (long)projectedBytes : null,
  26. measuredRates.Length > 0 ? Forecast(measuredRates.Min()) : null,
  27. measuredRates.Length > 0 ? Forecast(measuredRates.Max()) : null);
  28. }
  29. }