Home / Function/ aggregateLogsByQuery() — supabase Function Reference

aggregateLogsByQuery() — supabase Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  463d9b1b_4c86_b763_848a_1c62d2508795["aggregateLogsByQuery()"]
  8afea200_94fd_d6c2_5605_0880960a2b8b["WithMonitor()"]
  8afea200_94fd_d6c2_5605_0880960a2b8b -->|calls| 463d9b1b_4c86_b763_848a_1c62d2508795
  a6d684c3_e3ea_9b03_57b6_1761cc468109["normalizeQuery()"]
  463d9b1b_4c86_b763_848a_1c62d2508795 -->|calls| a6d684c3_e3ea_9b03_57b6_1761cc468109
  style 463d9b1b_4c86_b763_848a_1c62d2508795 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/QueryPerformance/WithMonitor/WithMonitor.utils.ts lines 130–230

export const aggregateLogsByQuery = (parsedLogs: ParsedLogEntry[]): QueryPerformanceRow[] => {
  if (!parsedLogs || parsedLogs.length === 0) return []

  const queryGroups = new Map<string, ParsedLogEntry[]>()

  parsedLogs.forEach((log) => {
    const query = normalizeQuery(log.query || '')
    if (!query) return

    if (!queryGroups.has(query)) {
      queryGroups.set(query, [])
    }
    queryGroups.get(query)!.push(log)
  })

  const aggregatedData: QueryPerformanceRow[] = []
  let totalExecutionTime = 0

  const queryStats = Array.from(queryGroups.entries()).map(([query, logs]) => {
    const count = logs.length
    let totalCalls = 0
    let totalRowsRead = 0
    let totalCacheHits = 0
    let totalCacheMisses = 0
    let rolname = logs[0].username
    let minTime = Infinity
    let maxTime = -Infinity
    let totalExecutionTimeForQuery = 0

    logs.forEach((log) => {
      const logMeanTime = parseFloat(
        String(log.mean_time ?? log.mean_exec_time ?? log.mean_query_time ?? 0)
      )
      const logMinTime = parseFloat(
        String(log.min_time ?? log.min_exec_time ?? log.min_query_time ?? 0)
      )
      const logMaxTime = parseFloat(
        String(log.max_time ?? log.max_exec_time ?? log.max_query_time ?? 0)
      )
      const logCalls = parseInt(String(log.calls ?? 0), 10)
      const logRows = parseInt(String(log.rows ?? 0), 10)
      const logCacheHits = parseFloat(String(log.shared_blks_hit ?? 0))
      const logCacheMisses = parseFloat(String(log.shared_blks_read ?? 0))

      minTime = Math.min(minTime, logMinTime)
      maxTime = Math.max(maxTime, logMaxTime)
      totalCalls += logCalls
      totalRowsRead += logRows
      totalCacheHits += logCacheHits
      totalCacheMisses += logCacheMisses
      totalExecutionTimeForQuery += logMeanTime * logCalls
    })

    // Overall mean time is the weighted average
    const avgMeanTime = totalCalls > 0 ? totalExecutionTimeForQuery / totalCalls : 0
    const finalMinTime = minTime === Infinity ? 0 : minTime
    const finalMaxTime = maxTime === -Infinity ? 0 : maxTime

    totalExecutionTime += totalExecutionTimeForQuery

    return {
      query,
      rolname,
      count,
      avgMeanTime,
      minTime: finalMinTime,
      maxTime: finalMaxTime,
      totalCalls,
      totalRowsRead,
      totalTime: totalExecutionTimeForQuery,
      totalCacheHits,
      totalCacheMisses,
    }
  })

  queryStats.forEach((stats) => {
    const totalCacheAccess = stats.totalCacheHits + stats.totalCacheMisses
    const cacheHitRate = totalCacheAccess > 0 ? (stats.totalCacheHits / totalCacheAccess) * 100 : 0

    const propTotalTime = totalExecutionTime > 0 ? (stats.totalTime / totalExecutionTime) * 100 : 0

    aggregatedData.push({
      query: stats.query,
      rolname: stats.rolname,
      calls: stats.totalCalls,
      mean_time: stats.avgMeanTime,
      min_time: stats.minTime,
      max_time: stats.maxTime,
      total_time: stats.totalTime,
      rows_read: stats.totalRowsRead,
      cache_hit_rate: cacheHitRate,
      prop_total_time: propTotalTime,
      index_advisor_result: null,
      _total_cache_hits: stats.totalCacheHits,
      _total_cache_misses: stats.totalCacheMisses,
      _count: stats.count,
    })
  })

  return aggregatedData.sort((a, b) => b.total_time - a.total_time)
}

Subdomains

Called By

Frequently Asked Questions

What does aggregateLogsByQuery() do?
aggregateLogsByQuery() is a function in the supabase codebase.
What does aggregateLogsByQuery() call?
aggregateLogsByQuery() calls 1 function(s): normalizeQuery.
What calls aggregateLogsByQuery()?
aggregateLogsByQuery() is called by 1 function(s): WithMonitor.

Analyze Your Own Codebase

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

Try Supermodel Free