ServiceStatus.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import { useParams } from 'common'
  2. import dayjs from 'dayjs'
  3. import { ChevronRight, Loader2 } from 'lucide-react'
  4. import Link from 'next/link'
  5. import { cn, HoverCard, HoverCardContent, HoverCardTrigger, InfoIcon } from 'ui'
  6. import {
  7. extractDbSchema,
  8. ProjectServiceStatus,
  9. StatusIcon,
  10. StatusMessage,
  11. } from '../Home/ServiceStatus'
  12. import { InlineLink } from '@/components/ui/InlineLink'
  13. import { SingleStat } from '@/components/ui/SingleStat'
  14. import { useBranchesQuery } from '@/data/branches/branches-query'
  15. import { useEdgeFunctionServiceStatusQuery } from '@/data/service-status/edge-functions-status-query'
  16. import { useProjectServiceStatusQuery } from '@/data/service-status/service-status-query'
  17. import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
  18. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  19. import { DOCS_URL } from '@/lib/constants'
  20. const SERVICE_STATUS_THRESHOLD = 5 // minutes
  21. /**
  22. * [Joshen] JFYI before we go live with this, we need to revisit the migrations section
  23. * as I don't think it should live in the ServiceStatus component since its not indicative
  24. * of a project's "service". ServiceStatus's intention is to be an ongoing health/status check.
  25. *
  26. * For context, migrations are meant to be indicative for only when creating branches or projects
  27. * with an initial SQL, so "healthy" migrations just means that migrations have all been successfully
  28. * ran. So it might be a matter of decoupling "ready" state vs "health checks"
  29. * [Edit] Now that migrations are only showing up if the project is a branch, i think its okay for now
  30. *
  31. * [Joshen] Another issue that requires investigation before we go live with the changes:
  32. * We've removed the isProjectNew check in this component which we had that logic cause new
  33. * projects would show unhealthy as the services are still starting up - but it causes a
  34. * perceived negative impression as new projects were showing unhealthy, hence the 5 minute
  35. * threshold check (we’d show “Coming up” instead of “unhealthy” if the project is within 5
  36. * minutes of when it was created). Might be related to decoupling "ready" state vs "health checks"
  37. */
  38. export const ServiceStatus = () => {
  39. const { ref } = useParams()
  40. const { data: project } = useSelectedProjectQuery()
  41. const {
  42. projectAuthAll: authEnabled,
  43. projectEdgeFunctionAll: edgeFunctionsEnabled,
  44. realtimeAll: realtimeEnabled,
  45. projectStorageAll: storageEnabled,
  46. } = useIsFeatureEnabled([
  47. 'project_auth:all',
  48. 'project_edge_function:all',
  49. 'realtime:all',
  50. 'project_storage:all',
  51. ])
  52. const isBranch = project?.parentRef !== project?.ref
  53. // Get branches data when on a branch
  54. const { data: branches, isPending: isBranchesLoading } = useBranchesQuery(
  55. { projectRef: isBranch ? project?.parentRef : undefined },
  56. {
  57. enabled: isBranch,
  58. }
  59. )
  60. const currentBranch = isBranch
  61. ? branches?.find((branch) => branch.project_ref === ref)
  62. : undefined
  63. // [Joshen] Need pooler service check eventually
  64. const { data: status, isPending: isLoading } = useProjectServiceStatusQuery(
  65. { projectRef: ref },
  66. {
  67. refetchInterval: (query) => {
  68. const data = query.state.data
  69. const isServiceUnhealthy = data?.some((service) => {
  70. // if the postgrest service has an empty schema, postgrest has been disabled
  71. if (service.name === 'rest' && extractDbSchema(service) === '') {
  72. return false
  73. }
  74. if (service.status === 'ACTIVE_HEALTHY') {
  75. return false
  76. }
  77. return true
  78. })
  79. return isServiceUnhealthy ? 5000 : false
  80. },
  81. }
  82. )
  83. const { data: edgeFunctionsStatus } = useEdgeFunctionServiceStatusQuery(
  84. { projectRef: ref },
  85. { refetchInterval: (query) => (!query.state.data?.healthy ? 5000 : false) }
  86. )
  87. const authStatus = status?.find((service) => service.name === 'auth')
  88. const restStatus = status?.find((service) => service.name === 'rest')
  89. const realtimeStatus = status?.find((service) => service.name === 'realtime')
  90. const storageStatus = status?.find((service) => service.name === 'storage')
  91. const dbStatus = status?.find((service) => service.name === 'db')
  92. const isMigrationLoading =
  93. project?.status === 'COMING_UP' ||
  94. (isBranch &&
  95. (isBranchesLoading ||
  96. currentBranch?.status === 'CREATING_PROJECT' ||
  97. currentBranch?.status === 'RUNNING_MIGRATIONS'))
  98. // [Joshen] Need individual troubleshooting docs for each service eventually for users to self serve
  99. const services: {
  100. name: string
  101. error?: string
  102. docsUrl?: string
  103. isLoading: boolean
  104. status: ProjectServiceStatus
  105. logsUrl: string
  106. }[] = [
  107. {
  108. name: 'Database',
  109. error: undefined,
  110. docsUrl: undefined,
  111. isLoading: isLoading,
  112. status: dbStatus?.status ?? 'UNHEALTHY',
  113. logsUrl: '/logs/postgres-logs',
  114. },
  115. {
  116. name: 'PostgREST',
  117. error: restStatus?.error,
  118. docsUrl: undefined,
  119. isLoading,
  120. // If PostgREST has an empty schema, it means it's been disabled
  121. status: extractDbSchema(restStatus) === '' ? 'DISABLED' : (restStatus?.status ?? 'UNHEALTHY'),
  122. logsUrl: '/logs/postgrest-logs',
  123. },
  124. ...(authEnabled
  125. ? [
  126. {
  127. name: 'Auth',
  128. error: authStatus?.error,
  129. docsUrl: undefined,
  130. isLoading,
  131. status: authStatus?.status ?? 'UNHEALTHY',
  132. logsUrl: '/logs/auth-logs',
  133. },
  134. ]
  135. : []),
  136. ...(realtimeEnabled
  137. ? [
  138. {
  139. name: 'Realtime',
  140. error: realtimeStatus?.error,
  141. docsUrl: undefined,
  142. isLoading,
  143. status: realtimeStatus?.status ?? 'UNHEALTHY',
  144. logsUrl: '/logs/realtime-logs',
  145. },
  146. ]
  147. : []),
  148. ...(storageEnabled
  149. ? [
  150. {
  151. name: 'Storage',
  152. error: storageStatus?.error,
  153. docsUrl: undefined,
  154. isLoading,
  155. status: storageStatus?.status ?? 'UNHEALTHY',
  156. logsUrl: '/logs/storage-logs',
  157. },
  158. ]
  159. : []),
  160. ...(edgeFunctionsEnabled
  161. ? [
  162. {
  163. name: 'Edge Functions',
  164. error: undefined,
  165. docsUrl: `${DOCS_URL}/guides/functions/troubleshooting`,
  166. isLoading,
  167. status: edgeFunctionsStatus?.healthy
  168. ? ('ACTIVE_HEALTHY' as const)
  169. : isLoading
  170. ? ('COMING_UP' as const)
  171. : ('UNHEALTHY' as const),
  172. logsUrl: '/logs/edge-functions-logs',
  173. },
  174. ]
  175. : []),
  176. ...(isBranch
  177. ? [
  178. {
  179. name: 'Migrations',
  180. error: undefined,
  181. docsUrl: undefined,
  182. isLoading: isBranchesLoading,
  183. status: isBranch
  184. ? currentBranch?.status === 'FUNCTIONS_DEPLOYED'
  185. ? ('ACTIVE_HEALTHY' as const)
  186. : currentBranch?.status === 'FUNCTIONS_FAILED' ||
  187. currentBranch?.status === 'MIGRATIONS_FAILED'
  188. ? ('UNHEALTHY' as const)
  189. : ('COMING_UP' as const)
  190. : isMigrationLoading
  191. ? 'COMING_UP'
  192. : 'ACTIVE_HEALTHY',
  193. logsUrl: isBranch ? '/branches' : '/logs/database-logs',
  194. },
  195. ]
  196. : []),
  197. ]
  198. const isLoadingChecks = services.some((service) => service.isLoading)
  199. // We consider a service operational if it's healthy or intentionally disabled
  200. const allServicesOperational = services.every(
  201. (service) => service.status === 'ACTIVE_HEALTHY' || service.status === 'DISABLED'
  202. )
  203. // Check if project or branch is in a startup state
  204. const isProjectNew =
  205. dayjs.utc().diff(dayjs.utc(project?.inserted_at), 'minute') < SERVICE_STATUS_THRESHOLD ||
  206. project?.status === 'COMING_UP' ||
  207. (isBranch &&
  208. (currentBranch?.status === 'CREATING_PROJECT' ||
  209. currentBranch?.status === 'RUNNING_MIGRATIONS' ||
  210. isMigrationLoading))
  211. const isProjectComingUp = ['COMING_UP', 'UNKNOWN'].includes(project?.status ?? '')
  212. const anyUnhealthy = services.some((service) => service.status === 'UNHEALTHY')
  213. const anyComingUp =
  214. isProjectComingUp || services.some((service) => service.status === 'COMING_UP')
  215. const getOverallStatusLabel = (): string => {
  216. if (isLoadingChecks) return 'Checking...'
  217. if (anyComingUp) return 'Coming up...'
  218. if (anyUnhealthy) return 'Unhealthy'
  219. return 'Healthy'
  220. }
  221. const overallStatusLabel = getOverallStatusLabel()
  222. return (
  223. <HoverCard openDelay={200} closeDelay={100}>
  224. <HoverCardTrigger>
  225. <SingleStat
  226. icon={
  227. // Spinner only while the overall project is in COMING_UP; otherwise show 6-dot grid
  228. isProjectComingUp ? (
  229. <Loader2 className="animate-spin" size={18} />
  230. ) : (
  231. <div className="grid grid-cols-3 gap-1">
  232. {services.map((service, index) => (
  233. <div
  234. key={`${service.name}-${index}`}
  235. className={cn(
  236. 'w-1.5 h-1.5 rounded-full',
  237. service.isLoading ||
  238. service.status === 'COMING_UP' ||
  239. (isProjectNew && service.status !== 'ACTIVE_HEALTHY')
  240. ? 'bg-foreground-lighter animate-pulse'
  241. : service.status === 'ACTIVE_HEALTHY'
  242. ? 'bg-brand'
  243. : 'bg-selection'
  244. )}
  245. />
  246. ))}
  247. </div>
  248. )
  249. }
  250. label={<span>Status</span>}
  251. value={<span>{overallStatusLabel}</span>}
  252. />
  253. </HoverCardTrigger>
  254. <HoverCardContent className="p-0 w-60" side="bottom" align="start">
  255. {services.map((service) => (
  256. <Link
  257. href={`/project/${ref}${service.logsUrl}`}
  258. key={service.name}
  259. className="transition px-3 py-2 text-xs flex items-center justify-between border-b last:border-none group relative hover:bg-surface-300"
  260. >
  261. <div className="flex gap-x-2">
  262. <StatusIcon
  263. isLoading={service.isLoading}
  264. isProjectNew={isProjectNew}
  265. projectStatus={service.status}
  266. />
  267. <div className="flex-1">
  268. <p>{service.name}</p>
  269. <p className="text-foreground-light flex items-center gap-1">
  270. <StatusMessage
  271. isLoading={service.isLoading}
  272. isProjectNew={isProjectNew}
  273. status={
  274. isProjectComingUp && service.status === 'UNHEALTHY'
  275. ? 'COMING_UP'
  276. : service.status
  277. }
  278. />
  279. </p>
  280. </div>
  281. </div>
  282. <div className="flex items-center gap-x-1 transition opacity-0 group-hover:opacity-100">
  283. <span className="text-xs text-foreground">View logs</span>
  284. <ChevronRight size={14} className="text-foreground" />
  285. </div>
  286. </Link>
  287. ))}
  288. {!allServicesOperational && (
  289. <div className="flex gap-2 text-xs text-foreground-light px-3 py-2">
  290. <div className="mt-0.5">
  291. <InfoIcon />
  292. </div>
  293. <div className="flex flex-col gap-y-1">
  294. <p>
  295. {isProjectNew ? 'New' : 'Recently restored'} projects can take up to{' '}
  296. {SERVICE_STATUS_THRESHOLD} minutes to become fully operational.
  297. </p>
  298. <p>
  299. If services stay unhealthy, refer to our{' '}
  300. <InlineLink
  301. href={`${DOCS_URL}/guides/troubleshooting/project-status-reports-unhealthy-services`}
  302. >
  303. docs
  304. </InlineLink>{' '}
  305. for more information.
  306. </p>
  307. </div>
  308. </div>
  309. )}
  310. </HoverCardContent>
  311. </HoverCard>
  312. )
  313. }