|
| 1 | +import { useState } from 'react'; |
| 2 | +import dynamic from 'next/dynamic'; |
| 3 | +import { parseAsInteger, useQueryState } from 'nuqs'; |
| 4 | +import { useForm } from 'react-hook-form'; |
| 5 | +import { SourceKind } from '@hyperdx/common-utils/dist/types'; |
| 6 | +import { Box, Group, Slider, Text } from '@mantine/core'; |
| 7 | + |
| 8 | +import { withAppNav } from '@/layout'; |
| 9 | + |
| 10 | +import ServiceMap from './components/ServiceMap/ServiceMap'; |
| 11 | +import SourceSchemaPreview from './components/SourceSchemaPreview'; |
| 12 | +import { SourceSelectControlled } from './components/SourceSelect'; |
| 13 | +import { TimePicker } from './components/TimePicker'; |
| 14 | +import { useSources } from './source'; |
| 15 | +import { parseTimeQuery, useNewTimeQuery } from './timeQuery'; |
| 16 | + |
| 17 | +// The % of requests sampled is 1 / sampling factor |
| 18 | +export const SAMPLING_FACTORS = [ |
| 19 | + { |
| 20 | + value: 100, |
| 21 | + label: '1%', |
| 22 | + }, |
| 23 | + { |
| 24 | + value: 20, |
| 25 | + label: '5%', |
| 26 | + }, |
| 27 | + { |
| 28 | + value: 10, |
| 29 | + label: '10%', |
| 30 | + }, |
| 31 | + { |
| 32 | + value: 2, |
| 33 | + label: '50%', |
| 34 | + }, |
| 35 | + { |
| 36 | + value: 1, |
| 37 | + label: '100%', |
| 38 | + }, |
| 39 | +]; |
| 40 | + |
| 41 | +const DEFAULT_INTERVAL = 'Past 1h'; |
| 42 | +const defaultTimeRange = parseTimeQuery(DEFAULT_INTERVAL, false) as [ |
| 43 | + Date, |
| 44 | + Date, |
| 45 | +]; |
| 46 | + |
| 47 | +function DBServiceMapPage() { |
| 48 | + const { data: sources } = useSources(); |
| 49 | + const [sourceId, setSourceId] = useQueryState('source'); |
| 50 | + |
| 51 | + const [displayedTimeInputValue, setDisplayedTimeInputValue] = |
| 52 | + useState(DEFAULT_INTERVAL); |
| 53 | + |
| 54 | + const { searchedTimeRange, onSearch } = useNewTimeQuery({ |
| 55 | + initialDisplayValue: DEFAULT_INTERVAL, |
| 56 | + initialTimeRange: defaultTimeRange, |
| 57 | + setDisplayedTimeInputValue, |
| 58 | + }); |
| 59 | + |
| 60 | + const defaultSource = sources?.find( |
| 61 | + source => source.kind === SourceKind.Trace, |
| 62 | + ); |
| 63 | + const source = |
| 64 | + sourceId && sources |
| 65 | + ? (sources.find( |
| 66 | + source => source.id === sourceId && source.kind === SourceKind.Trace, |
| 67 | + ) ?? defaultSource) |
| 68 | + : defaultSource; |
| 69 | + |
| 70 | + const { control, watch } = useForm({ |
| 71 | + values: { |
| 72 | + source: source?.id, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + watch((data, { name, type }) => { |
| 77 | + if (name === 'source' && type === 'change') { |
| 78 | + setSourceId(data.source ?? null); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + const [samplingFactor, setSamplingFactor] = useQueryState( |
| 83 | + 'samplingFactor', |
| 84 | + parseAsInteger.withDefault(10), |
| 85 | + ); |
| 86 | + const { label: samplingLabel = '' } = |
| 87 | + SAMPLING_FACTORS.find(factor => factor.value === samplingFactor) ?? {}; |
| 88 | + |
| 89 | + return source ? ( |
| 90 | + <Box |
| 91 | + data-testid="service-map-page" |
| 92 | + p="sm" |
| 93 | + className="bg-hdx-dark" |
| 94 | + style={{ display: 'flex', flexDirection: 'column', height: '100vh' }} |
| 95 | + > |
| 96 | + <Group mb="md" justify="space-between"> |
| 97 | + <Group> |
| 98 | + <Text c="gray.4" size="xl"> |
| 99 | + Service Map |
| 100 | + </Text> |
| 101 | + <SourceSelectControlled |
| 102 | + control={control} |
| 103 | + name="source" |
| 104 | + size="xs" |
| 105 | + allowedSourceKinds={[SourceKind.Trace]} |
| 106 | + sourceSchemaPreview={ |
| 107 | + <SourceSchemaPreview source={source} variant="text" /> |
| 108 | + } |
| 109 | + /> |
| 110 | + </Group> |
| 111 | + <Group justify="flex-end"> |
| 112 | + <Text c="gray.4" bg="inherit" size="sm"> |
| 113 | + Sampling {samplingLabel} |
| 114 | + </Text> |
| 115 | + <div style={{ minWidth: '200px' }}> |
| 116 | + <Slider |
| 117 | + label={null} |
| 118 | + color="green" |
| 119 | + min={0} |
| 120 | + max={SAMPLING_FACTORS.length - 1} |
| 121 | + value={SAMPLING_FACTORS.findIndex( |
| 122 | + factor => factor.value === samplingFactor, |
| 123 | + )} |
| 124 | + onChange={v => setSamplingFactor(SAMPLING_FACTORS[v].value)} |
| 125 | + showLabelOnHover={false} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + <TimePicker |
| 129 | + inputValue={displayedTimeInputValue} |
| 130 | + setInputValue={setDisplayedTimeInputValue} |
| 131 | + onSearch={onSearch} |
| 132 | + /> |
| 133 | + </Group> |
| 134 | + </Group> |
| 135 | + <ServiceMap |
| 136 | + traceTableSource={source} |
| 137 | + dateRange={searchedTimeRange} |
| 138 | + samplingFactor={samplingFactor} |
| 139 | + /> |
| 140 | + </Box> |
| 141 | + ) : null; |
| 142 | +} |
| 143 | + |
| 144 | +const DBServiceMapPageDynamic = dynamic(async () => DBServiceMapPage, { |
| 145 | + ssr: false, |
| 146 | +}); |
| 147 | + |
| 148 | +// @ts-ignore |
| 149 | +DBServiceMapPageDynamic.getLayout = withAppNav; |
| 150 | + |
| 151 | +export default DBServiceMapPageDynamic; |
0 commit comments