|
| 1 | +import { useState } from 'react'; |
| 2 | +import { MetricsDataType, TSource } from '@hyperdx/common-utils/dist/types'; |
| 3 | +import { Modal, Paper, Tabs, Text, TextProps, Tooltip } from '@mantine/core'; |
| 4 | + |
| 5 | +import { useTableMetadata } from '@/hooks/useMetadata'; |
| 6 | + |
| 7 | +import { SQLPreview } from './ChartSQLPreview'; |
| 8 | + |
| 9 | +interface SourceSchemaInfoIconProps { |
| 10 | + onClick: () => void; |
| 11 | + isEnabled: boolean; |
| 12 | + tableCount: number; |
| 13 | + iconStyles?: Pick<TextProps, 'size' | 'color'>; |
| 14 | +} |
| 15 | + |
| 16 | +const SourceSchemaInfoIcon = ({ |
| 17 | + onClick, |
| 18 | + isEnabled, |
| 19 | + tableCount, |
| 20 | + iconStyles, |
| 21 | +}: SourceSchemaInfoIconProps) => { |
| 22 | + const tooltipText = isEnabled |
| 23 | + ? tableCount > 1 |
| 24 | + ? `Show Table Schemas` |
| 25 | + : 'Show Table Schema' |
| 26 | + : 'Select a table to view its schema'; |
| 27 | + |
| 28 | + return ( |
| 29 | + <Tooltip |
| 30 | + label={tooltipText} |
| 31 | + color="dark" |
| 32 | + c="white" |
| 33 | + position="right" |
| 34 | + onClick={() => isEnabled && onClick()} |
| 35 | + > |
| 36 | + <Text {...iconStyles}> |
| 37 | + <i |
| 38 | + className={`bi bi-info-circle ${isEnabled ? 'cursor-pointer' : ''}`} |
| 39 | + /> |
| 40 | + </Text> |
| 41 | + </Tooltip> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +interface TableSchemaPreviewProps { |
| 46 | + databaseName: string; |
| 47 | + tableName: string; |
| 48 | + connectionId: string; |
| 49 | +} |
| 50 | + |
| 51 | +const TableSchemaPreview = ({ |
| 52 | + databaseName, |
| 53 | + tableName, |
| 54 | + connectionId, |
| 55 | +}: TableSchemaPreviewProps) => { |
| 56 | + const { data, isLoading } = useTableMetadata({ |
| 57 | + databaseName, |
| 58 | + tableName, |
| 59 | + connectionId, |
| 60 | + }); |
| 61 | + |
| 62 | + return ( |
| 63 | + <Paper flex="auto" shadow="none" radius="sm" style={{ overflow: 'hidden' }}> |
| 64 | + {isLoading ? ( |
| 65 | + <div className="spin-animate d-inline-block"> |
| 66 | + <i className="bi bi-arrow-repeat" /> |
| 67 | + </div> |
| 68 | + ) : ( |
| 69 | + <SQLPreview |
| 70 | + data={data?.create_table_query ?? 'Schema is not available'} |
| 71 | + enableCopy={!!data?.create_table_query} |
| 72 | + /> |
| 73 | + )} |
| 74 | + </Paper> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +export interface SourceSchemaPreviewProps { |
| 79 | + source?: Pick<TSource, 'connection' | 'from' | 'metricTables'> & |
| 80 | + Partial<Pick<TSource, 'kind' | 'name'>>; |
| 81 | + iconStyles?: Pick<TextProps, 'size' | 'color'>; |
| 82 | +} |
| 83 | + |
| 84 | +const METRIC_TYPE_NAMES: Record<MetricsDataType, string> = { |
| 85 | + [MetricsDataType.Sum]: 'Sum', |
| 86 | + [MetricsDataType.Gauge]: 'Gauge', |
| 87 | + [MetricsDataType.Histogram]: 'Histogram', |
| 88 | + [MetricsDataType.Summary]: 'Summary', |
| 89 | + [MetricsDataType.ExponentialHistogram]: 'Exponential Histogram', |
| 90 | +}; |
| 91 | + |
| 92 | +const SourceSchemaPreview = ({ |
| 93 | + source, |
| 94 | + iconStyles, |
| 95 | +}: SourceSchemaPreviewProps) => { |
| 96 | + const [isModalOpen, setIsModalOpen] = useState(false); |
| 97 | + |
| 98 | + const isMetricSource = source?.kind === 'metric'; |
| 99 | + const tables: (TableSchemaPreviewProps & { title: string })[] = []; |
| 100 | + if (source && isMetricSource) { |
| 101 | + tables.push( |
| 102 | + ...Object.values(MetricsDataType) |
| 103 | + .map(metricType => ({ |
| 104 | + metricType, |
| 105 | + tableName: source.metricTables?.[metricType], |
| 106 | + })) |
| 107 | + .filter(({ tableName }) => !!tableName) |
| 108 | + .map(({ metricType, tableName }) => ({ |
| 109 | + databaseName: source.from.databaseName, |
| 110 | + tableName: tableName!, |
| 111 | + connectionId: source.connection, |
| 112 | + title: METRIC_TYPE_NAMES[metricType], |
| 113 | + })), |
| 114 | + ); |
| 115 | + } else if (source && source.from.tableName) { |
| 116 | + tables.push({ |
| 117 | + databaseName: source.from.databaseName, |
| 118 | + tableName: source.from.tableName, |
| 119 | + connectionId: source.connection, |
| 120 | + title: source.name ?? source.from.tableName, |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + const isEnabled = !!source && tables.length > 0; |
| 125 | + |
| 126 | + return ( |
| 127 | + <> |
| 128 | + <SourceSchemaInfoIcon |
| 129 | + isEnabled={isEnabled} |
| 130 | + onClick={() => setIsModalOpen(true)} |
| 131 | + iconStyles={iconStyles} |
| 132 | + tableCount={tables.length} |
| 133 | + /> |
| 134 | + {isEnabled && ( |
| 135 | + <Modal |
| 136 | + opened={isModalOpen} |
| 137 | + onClose={() => setIsModalOpen(false)} |
| 138 | + size="auto" |
| 139 | + title={tables.length > 1 ? `Table Schemas` : `Table Schema`} |
| 140 | + > |
| 141 | + <Tabs |
| 142 | + defaultValue={`${tables[0]?.databaseName}.${tables[0]?.tableName}.${tables[0]?.title}`} |
| 143 | + > |
| 144 | + <Tabs.List> |
| 145 | + {tables.map(table => ( |
| 146 | + <Tabs.Tab |
| 147 | + key={`${table.databaseName}.${table.tableName}.${table.title}`} |
| 148 | + value={`${table.databaseName}.${table.tableName}.${table.title}`} |
| 149 | + > |
| 150 | + {table.title} |
| 151 | + </Tabs.Tab> |
| 152 | + ))} |
| 153 | + </Tabs.List> |
| 154 | + {tables.map(table => ( |
| 155 | + <Tabs.Panel |
| 156 | + key={`${table.databaseName}.${table.tableName}.${table.title}`} |
| 157 | + value={`${table.databaseName}.${table.tableName}.${table.title}`} |
| 158 | + pt="sm" |
| 159 | + > |
| 160 | + <TableSchemaPreview {...table} /> |
| 161 | + </Tabs.Panel> |
| 162 | + ))} |
| 163 | + </Tabs> |
| 164 | + </Modal> |
| 165 | + )} |
| 166 | + </> |
| 167 | + ); |
| 168 | +}; |
| 169 | + |
| 170 | +export default SourceSchemaPreview; |
0 commit comments