Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

2.7 KB
Raw Blame
import { type ComparisonOperator } from '@/components/interfaces/Reports/v2/ReportsNumericFilter'
import { AnalyticsInterval } from '@/data/analytics/constants'
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
import { logsAllEndpointUrl } from '@/data/logs/logs-endpoint'
import { safeSql, type SafeLogSqlFragment } from '@/data/logs/safe-analytics-sql'

export type Granularity = 'minute' | 'hour' | 'day'

/**
 * Pre-branded SQL fragments for the closed set of granularity tokens that
 * `analyticsIntervalToGranularity` may return. Use to splice a granularity into
 * a `safeSql` template without re-validating at the call site.
 */
export const SAFE_GRANULARITY_SQL: Record<Granularity, SafeLogSqlFragment> = {
  minute: safeSql`minute`,
  hour: safeSql`hour`,
  day: safeSql`day`,
}

/**
 * Pre-branded SQL fragments for the closed set of numeric comparison operators
 * accepted by `ReportsNumericFilter`. Use to splice an operator into a
 * `safeSql` template without re-validating at the call site.
 */
export const SAFE_COMPARISON_OPERATOR_SQL: Record<ComparisonOperator, SafeLogSqlFragment> = {
  '=': safeSql`=`,
  '>=': safeSql`>=`,
  '<=': safeSql`<=`,
  '>': safeSql`>`,
  '<': safeSql`<`,
  '!=': safeSql`!=`,
}

export function analyticsIntervalToGranularity(interval: AnalyticsInterval): Granularity {
  switch (interval) {
    case '1m':
      return 'minute'
    case '2m':
      return 'minute'
    case '5m':
      return 'minute'
    case '10m':
      return 'minute'
    case '30m':
      return 'minute'
    case '1h':
      return 'hour'
    case '1d':
      return 'day'
    default:
      return 'hour'
  }
}

export const REPORT_STATUS_CODE_COLORS: { [key: string]: { light: string; dark: string } } = {
  '400': { light: '#FFD54F', dark: '#FFF176' },
  '401': { light: '#FF8A65', dark: '#FFAB91' },
  '403': { light: '#FFB74D', dark: '#FFCC80' },
  '404': { light: '#90A4AE', dark: '#B0BEC5' },
  '409': { light: '#BA68C8', dark: '#CE93D8' },
  '410': { light: '#A1887F', dark: '#BCAAA4' },
  '422': { light: '#FF9800', dark: '#FFB74D' },
  '429': { light: '#E65100', dark: '#F57C00' },
  '500': { light: '#B71C1C', dark: '#D32F2F' },
  '502': { light: '#9575CD', dark: '#B39DDB' },
  '503': { light: '#0097A7', dark: '#4DD0E1' },
  '504': { light: '#C0CA33', dark: '#D4E157' },
  default: { light: '#757575', dark: '#9E9E9E' },
}

export async function fetchLogs(
  projectRef: string,
  sql: SafeLogSqlFragment,
  startDate: string,
  endDate: string,
  useOtel = false
) {
  return await executeAnalyticsSql({
    projectRef,
    endpoint: logsAllEndpointUrl(useOtel),
    sql,
    iso_timestamp_start: startDate,
    iso_timestamp_end: endDate,
    method: 'get',
  })
}