import dayjs from 'dayjs'
import { Check, Loader2, X } from 'lucide-react'
import { pgmqQueueTable } from './Queues.utils'
import { useQueuesMetricsQuery } from '@/data/database-queues/database-queues-metrics-query'
import { PostgresQueue } from '@/data/database-queues/database-queues-query'
import { useTablesQuery } from '@/data/tables/tables-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { DATETIME_FORMAT } from '@/lib/constants'
export interface QueueWithMetrics extends PostgresQueue {
id: string // Add unique id for DataGrid
}
interface QueueCellProps {
queue: QueueWithMetrics
}
export const QueueNameCell = ({ queue }: QueueCellProps) => (
{queue.queue_name}
)
export const QueueTypeCell = ({ queue }: QueueCellProps) => {
const type = queue.is_partitioned ? 'Partitioned' : queue.is_unlogged ? 'Unlogged' : 'Basic'
return (
{type}
)
}
export const QueueRLSCell = ({ queue }: QueueCellProps) => {
const { data: selectedProject } = useSelectedProjectQuery()
const { data: queueTables } = useTablesQuery({
projectRef: selectedProject?.ref,
connectionString: selectedProject?.connectionString,
schema: 'pgmq',
})
const queueTable = queueTables?.find((x) => x.name === pgmqQueueTable(queue.queue_name))
const isRlsEnabled = !!queueTable?.rls_enabled
return (
{isRlsEnabled ? : }
)
}
export const QueueCreatedAtCell = ({ queue }: QueueCellProps) => (
{dayjs(queue.created_at).format(DATETIME_FORMAT)}
)
export const QueueSizeCell = ({ queue }: QueueCellProps) => {
const { data: selectedProject } = useSelectedProjectQuery()
const { data: metrics, isPending: isLoading } = useQueuesMetricsQuery(
{
queueName: queue.queue_name,
projectRef: selectedProject?.ref,
connectionString: selectedProject?.connectionString,
},
{
staleTime: 30 * 1000, // 30 seconds
}
)
return (
{isLoading ? (
) : (
{metrics?.queue_length} {metrics?.method === 'estimated' ? '(Approximate)' : null}
)}
)
}