Home / Function/ normalizeChartBuckets() — supabase Function Reference

normalizeChartBuckets() — supabase Function Reference

Architecture documentation for the normalizeChartBuckets() function in ChartDataTransform.utils.ts from the supabase codebase.

Entity Profile

Relationship Graph

Source Code

apps/studio/components/interfaces/HomeNew/ChartDataTransform.utils.ts lines 38–88

export function normalizeChartBuckets(
  data: LogsBarChartDatum[],
  interval: IntervalKey,
  endDate: Date = new Date()
): LogsBarChartDatum[] {
  const config = BUCKET_CONFIG[interval]
  const { bucketMinutes, expectedBuckets } = config

  // Calculate start time based on expected buckets
  const end = dayjs(endDate)
  const start = end.subtract(expectedBuckets * bucketMinutes, 'minute')

  // Create empty buckets
  const buckets: LogsBarChartDatum[] = []
  let currentBucketStart = start

  for (let i = 0; i < expectedBuckets; i++) {
    buckets.push({
      timestamp: currentBucketStart.toISOString(),
      ok_count: 0,
      warning_count: 0,
      error_count: 0,
    })
    currentBucketStart = currentBucketStart.add(bucketMinutes, 'minute')
  }

  // If no data, return empty buckets
  if (!data || data.length === 0) {
    return buckets
  }

  // Aggregate data into buckets
  for (const datum of data) {
    const datumTime = dayjs(datum.timestamp)

    // Find which bucket this datum belongs to
    const bucketIndex = Math.floor(datumTime.diff(start, 'minute') / bucketMinutes)

    // Skip data points outside our time range
    if (bucketIndex < 0 || bucketIndex >= expectedBuckets) {
      continue
    }

    // Aggregate counts into the appropriate bucket
    buckets[bucketIndex].ok_count += datum.ok_count || 0
    buckets[bucketIndex].warning_count += datum.warning_count || 0
    buckets[bucketIndex].error_count += datum.error_count || 0
  }

  return buckets
}

Subdomains

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free