|
| 1 | +import * as React from 'react'; |
| 2 | +import { consoleFetchJSON } from '@openshift-console/dynamic-plugin-sdk'; |
| 3 | +import { Spinner, TextArea } from '@patternfly/react-core'; |
| 4 | +import { debounce } from 'lodash'; |
| 5 | + |
| 6 | +const PROMETHEUS_BASE_PATH = '/api/prometheus'; |
| 7 | +const PROMETHEUS_TENANCY_BASE_PATH = '/api/prometheus-tenancy'; |
| 8 | +const POLL_INTERVAL = 15 * 1000; |
| 9 | + |
| 10 | +type PrometheusResponse = { |
| 11 | + status: string; |
| 12 | + data: { |
| 13 | + resultType: 'vector' | 'scalar' | 'matrix' | 'string'; |
| 14 | + result: Array<{ |
| 15 | + metric: Record<string, string>; |
| 16 | + value: [number, string]; |
| 17 | + }>; |
| 18 | + }; |
| 19 | +}; |
| 20 | + |
| 21 | +const formatValue = (value: string): string => { |
| 22 | + const num = parseFloat(value); |
| 23 | + if (isNaN(num)) { |
| 24 | + return value; |
| 25 | + } |
| 26 | + // Format large numbers with appropriate units |
| 27 | + if (Math.abs(num) >= 1e9) { |
| 28 | + return `${(num / 1e9).toFixed(2)}B`; |
| 29 | + } |
| 30 | + if (Math.abs(num) >= 1e6) { |
| 31 | + return `${(num / 1e6).toFixed(2)}M`; |
| 32 | + } |
| 33 | + if (Math.abs(num) >= 1e3) { |
| 34 | + return `${(num / 1e3).toFixed(2)}K`; |
| 35 | + } |
| 36 | + // Format decimals nicely |
| 37 | + if (num % 1 !== 0) { |
| 38 | + return num.toFixed(4).replace(/\.?0+$/, ''); |
| 39 | + } |
| 40 | + return num.toString(); |
| 41 | +}; |
| 42 | + |
| 43 | +const PromQLValue: React.FC<{ query: string }> = ({ query: initialQuery }) => { |
| 44 | + const [inputValue, setInputValue] = React.useState(''); |
| 45 | + const [query, setQuery] = React.useState(''); |
| 46 | + const [loading, setLoading] = React.useState(true); |
| 47 | + const [error, setError] = React.useState<string | null>(null); |
| 48 | + const [results, setResults] = React.useState< |
| 49 | + Array<{ metric: Record<string, string>; value: string }> |
| 50 | + >([]); |
| 51 | + const [updateKey, setUpdateKey] = React.useState(0); |
| 52 | + |
| 53 | + const debouncedSetQuery = React.useMemo( |
| 54 | + () => |
| 55 | + debounce((value: string) => { |
| 56 | + setQuery(value); |
| 57 | + }, 500), |
| 58 | + [], |
| 59 | + ); |
| 60 | + |
| 61 | + const effectiveQuery = inputValue || initialQuery; |
| 62 | + |
| 63 | + React.useEffect(() => { |
| 64 | + debouncedSetQuery(effectiveQuery); |
| 65 | + }, [debouncedSetQuery, effectiveQuery]); |
| 66 | + |
| 67 | + const fetchData = React.useCallback( |
| 68 | + async (isInitialLoad: boolean) => { |
| 69 | + if (isInitialLoad) { |
| 70 | + setLoading(true); |
| 71 | + } |
| 72 | + setError(null); |
| 73 | + |
| 74 | + const encodedQuery = encodeURIComponent(query); |
| 75 | + const url = `${PROMETHEUS_BASE_PATH}/api/v1/query?query=${encodedQuery}`; |
| 76 | + |
| 77 | + try { |
| 78 | + const response: PrometheusResponse = await consoleFetchJSON(url); |
| 79 | + |
| 80 | + if (response.status !== 'success') { |
| 81 | + setError('Query failed'); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const data = response.data; |
| 86 | + if (data.resultType === 'scalar') { |
| 87 | + // Scalar result is [timestamp, value] |
| 88 | + const scalarResult = data.result as unknown as [number, string]; |
| 89 | + setResults([{ metric: {}, value: scalarResult[1] }]); |
| 90 | + setUpdateKey((k) => k + 1); |
| 91 | + } else if (data.resultType === 'vector') { |
| 92 | + setResults( |
| 93 | + data.result.map((r) => ({ |
| 94 | + metric: r.metric, |
| 95 | + value: r.value[1], |
| 96 | + })), |
| 97 | + ); |
| 98 | + setUpdateKey((k) => k + 1); |
| 99 | + } else { |
| 100 | + setError(`Unexpected result type: ${data.resultType}`); |
| 101 | + } |
| 102 | + } catch (err) { |
| 103 | + // Try tenancy endpoint as fallback |
| 104 | + try { |
| 105 | + const tenancyUrl = `${PROMETHEUS_TENANCY_BASE_PATH}/api/v1/query?query=${encodedQuery}`; |
| 106 | + const response: PrometheusResponse = await consoleFetchJSON(tenancyUrl); |
| 107 | + |
| 108 | + if (response.status !== 'success') { |
| 109 | + setError('Query failed'); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + const data = response.data; |
| 114 | + if (data.resultType === 'scalar') { |
| 115 | + const scalarResult = data.result as unknown as [number, string]; |
| 116 | + setResults([{ metric: {}, value: scalarResult[1] }]); |
| 117 | + setUpdateKey((k) => k + 1); |
| 118 | + } else if (data.resultType === 'vector') { |
| 119 | + setResults( |
| 120 | + data.result.map((r) => ({ |
| 121 | + metric: r.metric, |
| 122 | + value: r.value[1], |
| 123 | + })), |
| 124 | + ); |
| 125 | + setUpdateKey((k) => k + 1); |
| 126 | + } else { |
| 127 | + setError(`Unexpected result type: ${data.resultType}`); |
| 128 | + } |
| 129 | + } catch { |
| 130 | + setError(err instanceof Error ? err.message : 'Failed to fetch data'); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + if (isInitialLoad) { |
| 134 | + setLoading(false); |
| 135 | + } |
| 136 | + } |
| 137 | + }, |
| 138 | + [query], |
| 139 | + ); |
| 140 | + |
| 141 | + React.useEffect(() => { |
| 142 | + if (!query) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + fetchData(true); |
| 147 | + |
| 148 | + const intervalId = setInterval(() => { |
| 149 | + fetchData(false); |
| 150 | + }, POLL_INTERVAL); |
| 151 | + |
| 152 | + return () => clearInterval(intervalId); |
| 153 | + }, [fetchData, query]); |
| 154 | + |
| 155 | + const onChange = React.useCallback( |
| 156 | + (_event: React.ChangeEvent<HTMLTextAreaElement>, value: string) => { |
| 157 | + setInputValue(value); |
| 158 | + }, |
| 159 | + [], |
| 160 | + ); |
| 161 | + |
| 162 | + const renderResults = () => { |
| 163 | + if (loading) { |
| 164 | + return <Spinner size="md" />; |
| 165 | + } |
| 166 | + |
| 167 | + if (error) { |
| 168 | + return <span className="ols-plugin__promql-value-error">{error}</span>; |
| 169 | + } |
| 170 | + |
| 171 | + if (results.length === 0) { |
| 172 | + return <span className="ols-plugin__promql-value-empty">No data</span>; |
| 173 | + } |
| 174 | + |
| 175 | + if (results.length === 1 && Object.keys(results[0].metric).length === 0) { |
| 176 | + // Single scalar value |
| 177 | + return ( |
| 178 | + <span className="ols-plugin__promql-value-badge" key={updateKey}> |
| 179 | + {formatValue(results[0].value)} |
| 180 | + </span> |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + // Multiple results with labels |
| 185 | + return ( |
| 186 | + <div className="ols-plugin__promql-value-list" key={updateKey}> |
| 187 | + {results.map((result, index) => { |
| 188 | + const labelStr = Object.entries(result.metric) |
| 189 | + .map(([k, v]) => `${k}="${v}"`) |
| 190 | + .join(', '); |
| 191 | + return ( |
| 192 | + <div className="ols-plugin__promql-value-item" key={index}> |
| 193 | + <span className="ols-plugin__promql-value-badge">{formatValue(result.value)}</span> |
| 194 | + {labelStr && <span className="ols-plugin__promql-value-labels">{labelStr}</span>} |
| 195 | + </div> |
| 196 | + ); |
| 197 | + })} |
| 198 | + </div> |
| 199 | + ); |
| 200 | + }; |
| 201 | + |
| 202 | + return ( |
| 203 | + <div className="ols-plugin__promql-value"> |
| 204 | + {renderResults()} |
| 205 | + <TextArea |
| 206 | + aria-label="PromQL query" |
| 207 | + autoResize |
| 208 | + className="ols-plugin__promql-value-input" |
| 209 | + onChange={onChange} |
| 210 | + resizeOrientation="vertical" |
| 211 | + rows={2} |
| 212 | + value={inputValue || initialQuery} |
| 213 | + /> |
| 214 | + </div> |
| 215 | + ); |
| 216 | +}; |
| 217 | + |
| 218 | +export default PromQLValue; |
0 commit comments