InfrastructureActivity.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. import { useParams } from 'common'
  2. import dayjs from 'dayjs'
  3. import { capitalize } from 'lodash'
  4. import { BarChart2, ChartLine, ExternalLink } from 'lucide-react'
  5. import Link from 'next/link'
  6. import { Fragment, useMemo, useState } from 'react'
  7. import { Button } from 'ui'
  8. import { Admonition } from 'ui-patterns/admonition'
  9. import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
  10. import { INFRA_ACTIVITY_METRICS } from './Infrastructure.constants'
  11. import { getAddons } from '@/components/interfaces/Billing/Subscription/Subscription.utils'
  12. import { CPUWarnings } from '@/components/interfaces/Billing/Usage/UsageWarningAlerts/CPUWarnings'
  13. import { DiskIOBandwidthWarnings } from '@/components/interfaces/Billing/Usage/UsageWarningAlerts/DiskIOBandwidthWarnings'
  14. import { RAMWarnings } from '@/components/interfaces/Billing/Usage/UsageWarningAlerts/RAMWarnings'
  15. import UsageBarChart from '@/components/interfaces/Organization/Usage/UsageBarChart'
  16. import {
  17. ScaffoldContainer,
  18. ScaffoldDivider,
  19. ScaffoldSection,
  20. ScaffoldSectionContent,
  21. ScaffoldSectionDetail,
  22. } from '@/components/layouts/Scaffold'
  23. import { DatabaseSelector } from '@/components/ui/DatabaseSelector'
  24. import { DateRangePicker } from '@/components/ui/DateRangePicker'
  25. import { DocsButton } from '@/components/ui/DocsButton'
  26. import Panel from '@/components/ui/Panel'
  27. import { DataPoint } from '@/data/analytics/constants'
  28. import { mapMultiResponseToAnalyticsData } from '@/data/analytics/infra-monitoring-queries'
  29. import {
  30. InfraMonitoringAttribute,
  31. useInfraMonitoringAttributesQuery,
  32. } from '@/data/analytics/infra-monitoring-query'
  33. import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query'
  34. import { useProjectAddonsQuery } from '@/data/subscriptions/project-addons-query'
  35. import { useResourceWarningsQuery } from '@/data/usage/resource-warnings-query'
  36. import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
  37. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  38. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  39. import { DOCS_URL, INSTANCE_MICRO_SPECS, INSTANCE_NANO_SPECS, InstanceSpecs } from '@/lib/constants'
  40. import { TIME_PERIODS_BILLING, TIME_PERIODS_REPORTS } from '@/lib/constants/metrics'
  41. import { useDatabaseSelectorStateSnapshot } from '@/state/database-selector'
  42. const NON_DEDICATED_IO_RESOURCES = [
  43. 'ci_micro',
  44. 'ci_small',
  45. 'ci_medium',
  46. 'ci_large',
  47. 'ci_xlarge',
  48. 'ci_2xlarge',
  49. ]
  50. const INFRA_ATTRIBUTES: InfraMonitoringAttribute[] = [
  51. 'max_cpu_usage',
  52. 'ram_usage',
  53. 'disk_io_consumption',
  54. ]
  55. export const InfrastructureActivity = () => {
  56. const { ref: projectRef } = useParams()
  57. const { data: project } = useSelectedProjectQuery()
  58. const { data: organization } = useSelectedOrganizationQuery()
  59. const state = useDatabaseSelectorStateSnapshot()
  60. const [dateRange, setDateRange] = useState<any>()
  61. const { data: subscription, isPending: isLoadingSubscription } = useOrgSubscriptionQuery({
  62. orgSlug: organization?.slug,
  63. })
  64. const { hasAccess: hasAccessToComputeSizes } = useCheckEntitlements(
  65. 'instances.compute_update_available_sizes'
  66. )
  67. const { data: resourceWarnings } = useResourceWarningsQuery({ ref: projectRef })
  68. // [Joshen Cleanup] JFYI this client side filtering can be cleaned up once BE changes are live which will only return the warnings based on the provided ref
  69. const projectResourceWarnings = resourceWarnings?.find((x) => x.project === projectRef)
  70. const { data: addons } = useProjectAddonsQuery({ projectRef })
  71. const selectedAddons = addons?.selected_addons ?? []
  72. const { computeInstance } = getAddons(selectedAddons)
  73. const hasDedicatedIOResources =
  74. computeInstance !== undefined &&
  75. !NON_DEDICATED_IO_RESOURCES.includes(computeInstance.variant.identifier)
  76. function getCurrentComputeInstanceSpecs() {
  77. if (computeInstance?.variant.meta) {
  78. // If user has a compute instance (called addons) return that
  79. return computeInstance?.variant.meta as InstanceSpecs
  80. } else {
  81. // Otherwise, return the default specs
  82. return project?.infra_compute_size === 'nano' ? INSTANCE_NANO_SPECS : INSTANCE_MICRO_SPECS
  83. }
  84. }
  85. const currentComputeInstanceSpecs = getCurrentComputeInstanceSpecs()
  86. const currentBillingCycleSelected = useMemo(() => {
  87. // Selected by default
  88. if (!dateRange?.period_start || !dateRange?.period_end || !subscription) return true
  89. const { current_period_start, current_period_end } = subscription
  90. return (
  91. dayjs(dateRange.period_start.date).isSame(new Date(current_period_start * 1000)) &&
  92. dayjs(dateRange.period_end.date).isSame(new Date(current_period_end * 1000))
  93. )
  94. }, [dateRange, subscription])
  95. const upgradeUrl =
  96. organization === undefined
  97. ? `/`
  98. : hasAccessToComputeSizes
  99. ? `/org/${organization?.slug ?? '[slug]'}/billing#subscription`
  100. : `/project/${projectRef}/settings/addons`
  101. const categoryMeta = INFRA_ACTIVITY_METRICS.find((category) => category.key === 'infra')
  102. const startDate = useMemo(() => {
  103. if (dateRange?.period_start?.date === 'Invalid Date') return undefined
  104. // If end date is in future, set end date to now
  105. if (!dateRange?.period_start?.date) {
  106. return undefined
  107. } else {
  108. // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
  109. return new Date(dateRange?.period_start?.date ?? 0).toISOString().slice(0, -5) + 'Z'
  110. }
  111. }, [dateRange])
  112. const endDate = useMemo(() => {
  113. if (dateRange?.period_end?.date === 'Invalid Date') return undefined
  114. // If end date is in future, set end date to end of current day
  115. if (dateRange?.period_end?.date && dayjs(dateRange.period_end.date).isAfter(dayjs())) {
  116. // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
  117. // In order to have full days from Prometheus metrics when using 1d interval,
  118. // the time needs to be greater or equal than the time of the start date
  119. return dayjs().endOf('day').toISOString().slice(0, -5) + 'Z'
  120. } else if (dateRange?.period_end?.date) {
  121. // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
  122. return new Date(dateRange.period_end.date ?? 0).toISOString().slice(0, -5) + 'Z'
  123. }
  124. }, [dateRange])
  125. // Switch to hourly interval, if the timeframe is <48 hours
  126. let interval: '1d' | '1h' = '1d'
  127. let dateFormat = 'DD MMM'
  128. if (startDate && endDate) {
  129. const diffInHours = dayjs(endDate).diff(startDate, 'hours')
  130. if (diffInHours <= 48) {
  131. interval = '1h'
  132. dateFormat = 'h a'
  133. }
  134. }
  135. const { data: infraMonitoringData, isPending: isLoadingInfraData } =
  136. useInfraMonitoringAttributesQuery({
  137. projectRef,
  138. attributes: INFRA_ATTRIBUTES,
  139. interval,
  140. startDate,
  141. endDate,
  142. databaseIdentifier: state.selectedDatabaseId,
  143. })
  144. const transformedData = useMemo(() => {
  145. if (!infraMonitoringData) return undefined
  146. return mapMultiResponseToAnalyticsData(infraMonitoringData, INFRA_ATTRIBUTES, dateFormat)
  147. }, [infraMonitoringData, dateFormat])
  148. const cpuUsageData = transformedData?.max_cpu_usage
  149. const memoryUsageData = transformedData?.ram_usage
  150. const ioBudgetData = transformedData?.disk_io_consumption
  151. const hasLatest = dayjs(endDate!).isAfter(dayjs().startOf('day'))
  152. const latestIoBudgetConsumption =
  153. hasLatest && ioBudgetData?.data?.slice(-1)?.[0]
  154. ? Number(ioBudgetData.data.slice(-1)[0].disk_io_consumption)
  155. : 0
  156. const highestIoBudgetConsumption = (ioBudgetData?.data || [])
  157. .map((x) => Number(x.disk_io_consumption) ?? 0)
  158. .reduce((a, b) => Math.max(a, b), 0)
  159. const chartMeta: { [key: string]: { data: DataPoint[]; isLoading: boolean } } = {
  160. max_cpu_usage: {
  161. isLoading: isLoadingInfraData,
  162. data: cpuUsageData?.data ?? [],
  163. },
  164. ram_usage: {
  165. isLoading: isLoadingInfraData,
  166. data: memoryUsageData?.data ?? [],
  167. },
  168. disk_io_consumption: {
  169. isLoading: isLoadingInfraData,
  170. data: ioBudgetData?.data ?? [],
  171. },
  172. }
  173. return (
  174. <>
  175. <ScaffoldContainer id="infrastructure-activity">
  176. <div className="mx-auto flex flex-col gap-10 pt-6">
  177. <div>
  178. <p className="text-xl">Infrastructure Activity</p>
  179. <p className="text-sm text-foreground-light">
  180. Activity statistics related to your server instance
  181. </p>
  182. </div>
  183. </div>
  184. </ScaffoldContainer>
  185. <ScaffoldContainer className="sticky top-0 py-6 border-b bg-studio z-10">
  186. <div className="flex items-center gap-x-4">
  187. <DatabaseSelector />
  188. {!isLoadingSubscription && (
  189. <>
  190. <DateRangePicker
  191. onChange={setDateRange}
  192. value={TIME_PERIODS_REPORTS[0].key}
  193. options={[...TIME_PERIODS_BILLING, ...TIME_PERIODS_REPORTS]}
  194. loading={isLoadingSubscription}
  195. currentBillingPeriodStart={subscription?.current_period_start}
  196. currentBillingPeriodEnd={subscription?.current_period_end}
  197. />
  198. <p className="text-sm text-foreground-light">
  199. {dayjs(startDate).format('DD MMM YYYY')} - {dayjs(endDate).format('DD MMM YYYY')}
  200. </p>
  201. </>
  202. )}
  203. </div>
  204. </ScaffoldContainer>
  205. {categoryMeta?.attributes.map((attribute) => {
  206. const chartData = chartMeta[attribute.key]?.data ?? []
  207. return (
  208. <Fragment key={attribute.key}>
  209. <ScaffoldContainer id={attribute.anchor}>
  210. <ScaffoldSection>
  211. <ScaffoldSectionDetail>
  212. <div className="sticky top-32 space-y-6">
  213. <div className="space-y-1">
  214. <div className="flex items-center space-x-2">
  215. <h4 className="text-base capitalize m-0">{attribute.name}</h4>
  216. </div>
  217. <div className="grid gap-4">
  218. {attribute.description.split('\n').map((value, idx) => (
  219. <p key={`desc-${idx}`} className="text-sm text-foreground-light pr-8 m-0">
  220. {value}
  221. </p>
  222. ))}
  223. </div>
  224. </div>
  225. <div className="space-y-2">
  226. <p className="text-sm text-foreground mb-2">More information</p>
  227. {attribute.links.map((link) => (
  228. <div key={link.url}>
  229. <Link href={link.url} target="_blank" rel="noreferrer">
  230. <div className="flex items-center space-x-2 opacity-50 hover:opacity-100 transition">
  231. <p className="text-sm m-0">{link.name}</p>
  232. <ExternalLink size={16} strokeWidth={1.5} />
  233. </div>
  234. </Link>
  235. </div>
  236. ))}
  237. </div>
  238. </div>
  239. </ScaffoldSectionDetail>
  240. <ScaffoldSectionContent>
  241. {attribute.key === 'disk_io_consumption' && (
  242. <>
  243. <DiskIOBandwidthWarnings
  244. upgradeUrl={upgradeUrl}
  245. hasAccessToComputeSizes={hasAccessToComputeSizes}
  246. hasLatest={hasLatest}
  247. currentBillingCycleSelected={currentBillingCycleSelected}
  248. latestIoBudgetConsumption={latestIoBudgetConsumption}
  249. highestIoBudgetConsumption={highestIoBudgetConsumption}
  250. />
  251. <div className="space-y-1">
  252. <p>Disk IO Bandwidth</p>
  253. {currentComputeInstanceSpecs.baseline_disk_io_mbs ===
  254. currentComputeInstanceSpecs.max_disk_io_mbs ? (
  255. <p className="text-sm text-foreground-light">
  256. Your current compute has a baseline and maximum disk throughput of{' '}
  257. {currentComputeInstanceSpecs.max_disk_io_mbs?.toLocaleString()} Mbps.
  258. </p>
  259. ) : (
  260. <p className="text-sm text-foreground-light">
  261. Your current compute can burst above the baseline disk throughput of{' '}
  262. {currentComputeInstanceSpecs.baseline_disk_io_mbs?.toLocaleString()}{' '}
  263. Mbps for short periods of time.
  264. </p>
  265. )}
  266. </div>
  267. <div>
  268. <p className="text-sm mb-2">Overview</p>
  269. <div className="flex items-center justify-between border-b py-1">
  270. <p className="text-xs text-foreground-light">Current compute instance</p>
  271. <p className="text-xs">
  272. {computeInstance?.variant?.name ??
  273. capitalize(project?.infra_compute_size) ??
  274. 'Micro'}
  275. </p>
  276. </div>
  277. <div className="flex items-center justify-between border-b py-1">
  278. <p className="text-xs text-foreground-light">Baseline IO Bandwidth</p>
  279. <p className="text-xs">
  280. {currentComputeInstanceSpecs.baseline_disk_io_mbs?.toLocaleString()}{' '}
  281. Mbps
  282. </p>
  283. </div>
  284. <div className="flex items-center justify-between border-b py-1">
  285. <p className="text-xs text-foreground-light">
  286. Maximum IO Bandwidth (burst limit)
  287. </p>
  288. <p className="text-xs">
  289. {currentComputeInstanceSpecs.max_disk_io_mbs?.toLocaleString()} Mbps
  290. </p>
  291. </div>
  292. {currentComputeInstanceSpecs.max_disk_io_mbs !==
  293. currentComputeInstanceSpecs?.baseline_disk_io_mbs && (
  294. <div className="flex items-center justify-between py-1">
  295. <p className="text-xs text-foreground-light">Daily burst time limit</p>
  296. <p className="text-xs">30 mins</p>
  297. </div>
  298. )}
  299. </div>
  300. </>
  301. )}
  302. {attribute.key === 'max_cpu_usage' && (
  303. <CPUWarnings
  304. upgradeUrl={upgradeUrl}
  305. hasAccessToComputeSizes={hasAccessToComputeSizes}
  306. severity={projectResourceWarnings?.cpu_exhaustion}
  307. />
  308. )}
  309. {attribute.key === 'ram_usage' && (
  310. <RAMWarnings
  311. upgradeUrl={upgradeUrl}
  312. hasAccessToComputeSizes={hasAccessToComputeSizes}
  313. severity={projectResourceWarnings?.memory_and_swap_exhaustion}
  314. />
  315. )}
  316. <div className="space-y-1">
  317. <div className="flex flex-row justify-between">
  318. {attribute.key === 'disk_io_consumption' ? (
  319. <p>Disk IO consumed per {interval === '1d' ? 'day' : 'hour'}</p>
  320. ) : (
  321. <p>
  322. Max{' '}
  323. <span className={attribute.key === 'ram_usage' ? 'lowercase' : ''}>
  324. {attribute.name}
  325. </span>{' '}
  326. utilization per {interval === '1d' ? 'day' : 'hour'}
  327. </p>
  328. )}
  329. </div>
  330. {attribute.key === 'ram_usage' && (
  331. <div className="text-sm text-foreground-light">
  332. <p>
  333. Your compute instance has {currentComputeInstanceSpecs.memory_gb} GB of
  334. memory.
  335. </p>
  336. {currentComputeInstanceSpecs.memory_gb === 1 && (
  337. <p>
  338. As your project is running on the smallest compute instance, it is not
  339. unusual for your project to have a base memory usage of ~50%.
  340. </p>
  341. )}
  342. </div>
  343. )}
  344. {attribute.key === 'max_cpu_usage' && (
  345. <p className="text-sm text-foreground-light">
  346. Your compute instance has {currentComputeInstanceSpecs.cpu_cores} CPU cores.
  347. </p>
  348. )}
  349. {attribute.chartDescription.split('\n').map((paragraph, idx) => (
  350. <p key={`para-${idx}`} className="text-sm text-foreground-light">
  351. {paragraph}
  352. </p>
  353. ))}
  354. </div>
  355. {attribute.key === 'disk_io_consumption' && hasDedicatedIOResources ? (
  356. <>
  357. <Admonition
  358. type="note"
  359. title={`Your compute instance of ${computeInstance.variant.name} comes with dedicated I/O resources`}
  360. description="Your project thus does not rely on I/O balance or burst capacity as larger
  361. add-ons are designed for sustained, high performance with specific IOPS and
  362. throughput limits without needing to burst."
  363. >
  364. <DocsButton
  365. abbrev={false}
  366. href={`${DOCS_URL}/guides/platform/compute-add-ons#disk-throughput-and-iops`}
  367. />
  368. </Admonition>
  369. </>
  370. ) : chartMeta[attribute.key].isLoading ? (
  371. <div className="space-y-2">
  372. <ShimmeringLoader />
  373. <ShimmeringLoader className="w-3/4" />
  374. <ShimmeringLoader className="w-1/2" />
  375. </div>
  376. ) : chartData.length ? (
  377. <UsageBarChart
  378. name={`${attribute.chartPrefix || ''} ${attribute.name}`}
  379. unit={attribute.unit}
  380. attributes={attribute.attributes}
  381. data={chartData}
  382. yFormatter={(value) => `${Math.round(Number(value))}%`}
  383. tooltipFormatter={(value) => `${value}%`}
  384. yLimit={100}
  385. />
  386. ) : (
  387. <Panel>
  388. <Panel.Content>
  389. <div className="flex flex-col items-center justify-center space-y-2">
  390. <BarChart2 size={18} className="text-foreground-light mb-2" />
  391. <p className="text-sm">No data in period</p>
  392. <p className="text-sm text-foreground-light">
  393. May take a few minutes to show
  394. </p>
  395. </div>
  396. </Panel.Content>
  397. </Panel>
  398. )}
  399. {attribute.key === 'disk_io_consumption' && !hasDedicatedIOResources && (
  400. <Admonition
  401. type="default"
  402. title="Looking for actual disk activity?"
  403. description="The chart above shows your remaining burst budget, not real disk throughput. For detailed read/write IOPS and throughput charts, head to the Database Observability page."
  404. >
  405. <Button asChild type="default" icon={<ChartLine size={14} />}>
  406. <Link href={`/project/${projectRef}/observability/database`}>
  407. View detailed IOPS and throughput
  408. </Link>
  409. </Button>
  410. </Admonition>
  411. )}
  412. </ScaffoldSectionContent>
  413. </ScaffoldSection>
  414. </ScaffoldContainer>
  415. <ScaffoldDivider />
  416. </Fragment>
  417. )
  418. })}
  419. </>
  420. )
  421. }