Home / Function/ calculatePercentilesFromHistogram() — supabase Function Reference

calculatePercentilesFromHistogram() — supabase Function Reference

Architecture documentation for the calculatePercentilesFromHistogram() function in WithMonitor.utils.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  b1259752_04bc_1519_0b13_de719e165b16["calculatePercentilesFromHistogram()"]
  0f4e0758_d08d_4514_7f21_93d850ccdbf9["QueryPerformanceChart()"]
  0f4e0758_d08d_4514_7f21_93d850ccdbf9 -->|calls| b1259752_04bc_1519_0b13_de719e165b16
  feb777f2_18e6_9259_8223_be4c3687a513["transformLogsToChartData()"]
  feb777f2_18e6_9259_8223_be4c3687a513 -->|calls| b1259752_04bc_1519_0b13_de719e165b16
  style b1259752_04bc_1519_0b13_de719e165b16 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/QueryPerformance/WithMonitor/WithMonitor.utils.ts lines 232–301

export const calculatePercentilesFromHistogram = (
  respCalls: number[]
): {
  p50: number
  p95: number
} => {
  const bucketBoundaries = [
    { min: 0, max: 1 },
    { min: 1, max: 10 },
    { min: 10, max: 100 },
    { min: 100, max: 1000 },
    { min: 1000, max: 10000 },
    { min: 10000, max: 100000 },
  ]

  const totalCalls = respCalls.reduce((sum, count) => sum + count, 0)

  if (totalCalls === 0) {
    return { p50: 0, p95: 0 }
  }

  const distribution: {
    minValue: number
    maxValue: number
    cumulativeCount: number
    count: number
  }[] = []
  let cumulativeCount = 0

  respCalls.forEach((count, index) => {
    if (count > 0 && index < bucketBoundaries.length) {
      const bucket = bucketBoundaries[index]
      cumulativeCount += count
      distribution.push({
        minValue: bucket.min,
        maxValue: bucket.max,
        cumulativeCount,
        count,
      })
    }
  })

  const getPercentile = (percentile: number): number => {
    const targetCount = totalCalls * percentile

    for (let i = 0; i < distribution.length; i++) {
      const prevCumulativeCount = i > 0 ? distribution[i - 1].cumulativeCount : 0

      if (distribution[i].cumulativeCount >= targetCount) {
        const positionInBucket = (targetCount - prevCumulativeCount) / distribution[i].count
        const bucketMin = distribution[i].minValue
        const bucketMax = distribution[i].maxValue
        const logMin = Math.log10(Math.max(bucketMin, 0.1))
        const logMax = Math.log10(bucketMax)
        const logValue = logMin + positionInBucket * (logMax - logMin)

        return Math.pow(10, logValue)
      }
    }

    return distribution[distribution.length - 1]?.maxValue || 0
  }

  const result = {
    p50: getPercentile(0.5),
    p95: getPercentile(0.95),
  }

  return result
}

Subdomains

Frequently Asked Questions

What does calculatePercentilesFromHistogram() do?
calculatePercentilesFromHistogram() is a function in the supabase codebase.
What calls calculatePercentilesFromHistogram()?
calculatePercentilesFromHistogram() is called by 2 function(s): QueryPerformanceChart, transformLogsToChartData.

Analyze Your Own Codebase

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

Try Supermodel Free