QueuesRows.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import dayjs from 'dayjs'
  2. import { includes, sortBy } from 'lodash'
  3. import { Check, ChevronRight, Loader2, X } from 'lucide-react'
  4. import { useRouter } from 'next/router'
  5. import { pgmqQueueTable } from './Queues.utils'
  6. import Table from '@/components/to-be-cleaned/Table'
  7. import { useQueuesMetricsQuery } from '@/data/database-queues/database-queues-metrics-query'
  8. import { PostgresQueue } from '@/data/database-queues/database-queues-query'
  9. import { useTablesQuery } from '@/data/tables/tables-query'
  10. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  11. import { DATETIME_FORMAT } from '@/lib/constants'
  12. interface QueuesRowsProps {
  13. queues: PostgresQueue[]
  14. filterString: string
  15. }
  16. const QueueRow = ({ queue }: { queue: PostgresQueue }) => {
  17. const router = useRouter()
  18. const { data: selectedProject } = useSelectedProjectQuery()
  19. const { data: queueTables } = useTablesQuery({
  20. projectRef: selectedProject?.ref,
  21. connectionString: selectedProject?.connectionString,
  22. schema: 'pgmq',
  23. })
  24. const queueTable = queueTables?.find((x) => x.name === pgmqQueueTable(queue.queue_name))
  25. const isRlsEnabled = !!queueTable?.rls_enabled
  26. const { data: metrics, isPending: isLoading } = useQueuesMetricsQuery(
  27. {
  28. queueName: queue.queue_name,
  29. projectRef: selectedProject?.ref,
  30. connectionString: selectedProject?.connectionString,
  31. },
  32. {
  33. staleTime: 30 * 1000, // 30 seconds, talk with Oli whether this is ok to call every minute
  34. }
  35. )
  36. const type = queue.is_partitioned ? 'Partitioned' : queue.is_unlogged ? 'Unlogged' : 'Basic'
  37. return (
  38. <Table.tr
  39. key={queue.queue_name}
  40. onClick={() => {
  41. router.push(
  42. `/project/${selectedProject?.ref}/integrations/queues/queues/${queue.queue_name}`
  43. )
  44. }}
  45. >
  46. <Table.td className="truncate">
  47. <p title={queue.queue_name}>{queue.queue_name}</p>
  48. </Table.td>
  49. <Table.td className="table-cell overflow-auto">
  50. <p title={type.toLocaleLowerCase()} className="truncate">
  51. {type}
  52. </p>
  53. </Table.td>
  54. <Table.td className="table-cell">
  55. <div className="flex justify-center">
  56. {isRlsEnabled ? <Check size={14} className="text-brand" /> : <X size={14} />}
  57. </div>
  58. </Table.td>
  59. <Table.td className="table-cell">
  60. <p title={queue.created_at}>{dayjs(queue.created_at).format(DATETIME_FORMAT)}</p>
  61. </Table.td>
  62. <Table.td className="table-cell">
  63. <div className="flex justify-center">
  64. {isLoading ? (
  65. <Loader2 className="animate-spin" size={16} />
  66. ) : (
  67. <p>
  68. {metrics?.queue_length} {metrics?.method === 'estimated' ? '(Approximate)' : null}
  69. </p>
  70. )}
  71. </div>
  72. </Table.td>
  73. <Table.td>
  74. <div className="flex items-center justify-end">
  75. <ChevronRight size="18" />
  76. </div>
  77. </Table.td>
  78. </Table.tr>
  79. )
  80. }
  81. export const QueuesRows = ({ queues: fetchedQueues, filterString }: QueuesRowsProps) => {
  82. const filteredQueues = fetchedQueues.filter((x) =>
  83. includes(x.queue_name.toLowerCase(), filterString.toLowerCase())
  84. )
  85. const queues = sortBy(filteredQueues, (func) => func.queue_name.toLocaleLowerCase())
  86. if (queues.length === 0 && filterString.length > 0) {
  87. return (
  88. <Table.tr>
  89. <Table.td colSpan={5}>
  90. <p className="text-sm text-foreground">No results found</p>
  91. <p className="text-sm text-foreground-light">
  92. Your search for "{filterString}" did not return any results
  93. </p>
  94. </Table.td>
  95. </Table.tr>
  96. )
  97. }
  98. return (
  99. <>
  100. {queues.map((q) => (
  101. <QueueRow key={q.queue_name} queue={q} />
  102. ))}
  103. </>
  104. )
  105. }