EdgeFunctionUsageSection.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { useMemo } from 'react'
  2. import { ChartMetric } from 'ui-patterns/Chart'
  3. import { PageContainer } from 'ui-patterns/PageContainer'
  4. import {
  5. PageSection,
  6. PageSectionContent,
  7. PageSectionMeta,
  8. PageSectionSummary,
  9. PageSectionTitle,
  10. } from 'ui-patterns/PageSection'
  11. import { getCpuTooltipDetails, getMemoryTooltipDetails } from './EdgeFunctionMetricTooltipDetails'
  12. import {
  13. CPU_TIME_CHART_CONFIG,
  14. formatMetric,
  15. formatRate,
  16. getChartEmptyStateCopy,
  17. MEMORY_CHART_CONFIG,
  18. } from './EdgeFunctionOverview.utils'
  19. import type { EdgeFunctionChartDatum } from './EdgeFunctionOverview.utils'
  20. import { EdgeFunctionTimeSeriesChartCard } from './EdgeFunctionTimeSeriesChartCard'
  21. interface EdgeFunctionUsageSectionProps {
  22. data: EdgeFunctionChartDatum[]
  23. dateTimeFormat: string
  24. isLoading: boolean
  25. isError: boolean
  26. errorMessage?: string
  27. averageCpuTime: number
  28. maxCpuTime: number
  29. averageMemoryUsage: number
  30. totalHeapMemory: number
  31. totalExternalMemory: number
  32. totalMemoryByType: number
  33. }
  34. export const EdgeFunctionUsageSection = ({
  35. data,
  36. dateTimeFormat,
  37. isLoading,
  38. isError,
  39. errorMessage,
  40. averageCpuTime,
  41. maxCpuTime,
  42. averageMemoryUsage,
  43. totalHeapMemory,
  44. totalExternalMemory,
  45. totalMemoryByType,
  46. }: EdgeFunctionUsageSectionProps) => {
  47. const cpuEmptyStateCopy = getChartEmptyStateCopy('CPU time', isError, errorMessage)
  48. const memoryEmptyStateCopy = getChartEmptyStateCopy('memory usage', isError, errorMessage)
  49. const cpuTooltipDetails = useMemo(() => getCpuTooltipDetails(averageCpuTime), [averageCpuTime])
  50. const memoryTooltipDetails = useMemo(
  51. () => getMemoryTooltipDetails(averageMemoryUsage),
  52. [averageMemoryUsage]
  53. )
  54. const cpuMetrics = (
  55. <div className="flex flex-wrap gap-x-8 gap-y-4">
  56. <ChartMetric
  57. label="Average CPU Time"
  58. value={formatMetric(averageCpuTime, 'ms')}
  59. tooltip="Average CPU time usage for the function"
  60. />
  61. <ChartMetric
  62. label="Max CPU Time"
  63. value={formatMetric(maxCpuTime, 'ms')}
  64. tooltip="Maximum CPU time usage for the function"
  65. />
  66. </div>
  67. )
  68. const memoryMetrics = (
  69. <div className="flex flex-wrap gap-x-8 gap-y-4">
  70. <ChartMetric
  71. label="Average Memory Usage"
  72. value={formatMetric(averageMemoryUsage, 'MB')}
  73. tooltip="Average memory usage for the function"
  74. />
  75. <ChartMetric
  76. label="Heap"
  77. value={formatRate(totalHeapMemory, totalMemoryByType)}
  78. tooltip="Share of memory attributed to heap usage over the selected interval"
  79. />
  80. <ChartMetric
  81. label="External"
  82. value={formatRate(totalExternalMemory, totalMemoryByType)}
  83. tooltip="Share of memory attributed to external usage over the selected interval"
  84. />
  85. </div>
  86. )
  87. return (
  88. <PageSection>
  89. <PageSectionContent>
  90. <PageContainer size="full">
  91. <div className="flex flex-col gap-6">
  92. <PageSectionMeta>
  93. <PageSectionSummary>
  94. <PageSectionTitle>Usage</PageSectionTitle>
  95. </PageSectionSummary>
  96. </PageSectionMeta>
  97. <div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
  98. <EdgeFunctionTimeSeriesChartCard
  99. data={data}
  100. dateTimeFormat={dateTimeFormat}
  101. isLoading={isLoading}
  102. isError={isError}
  103. emptyTitle={cpuEmptyStateCopy.title}
  104. emptyDescription={cpuEmptyStateCopy.description}
  105. metrics={cpuMetrics}
  106. dataKey="max_cpu_time_used"
  107. config={CPU_TIME_CHART_CONFIG}
  108. tooltipDetails={cpuTooltipDetails}
  109. referenceLines={[
  110. {
  111. y: averageCpuTime,
  112. label: 'average',
  113. stroke: 'hsl(var(--foreground-default))',
  114. strokeWidth: 1.5,
  115. },
  116. ]}
  117. yAxisProps={{
  118. width: 64,
  119. tickFormatter: (value: number) => `${Math.round(value)}ms`,
  120. }}
  121. />
  122. <EdgeFunctionTimeSeriesChartCard
  123. data={data}
  124. dateTimeFormat={dateTimeFormat}
  125. isLoading={isLoading}
  126. isError={isError}
  127. emptyTitle={memoryEmptyStateCopy.title}
  128. emptyDescription={memoryEmptyStateCopy.description}
  129. metrics={memoryMetrics}
  130. dataKey="avg_memory_used"
  131. config={MEMORY_CHART_CONFIG}
  132. tooltipDetails={memoryTooltipDetails}
  133. referenceLines={[
  134. {
  135. y: averageMemoryUsage,
  136. label: 'average',
  137. stroke: 'hsl(var(--foreground-default))',
  138. strokeWidth: 1.5,
  139. },
  140. ]}
  141. yAxisProps={{
  142. width: 64,
  143. tickFormatter: (value: number) => `${Number(value).toFixed(1)}MB`,
  144. }}
  145. />
  146. </div>
  147. </div>
  148. </PageContainer>
  149. </PageSectionContent>
  150. </PageSection>
  151. )
  152. }