Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 10x 25x 25x 25x 25x 25x 23x 29x 3x 26x 23x 23x 23x 23x 23x 23x 23x 11x 11x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 12x 21x 21x 21x 21x 21x 21x 12x 16x 16x 16x 16x 16x 16x 16x 16x 16x 21x 21x 21x 21x 21x 23x 25x 9x 25x 23x 25x 3x 22x 1x 21x 21x | // ─────────────────────────────────────────────────────────────
// src/TangoExplorerWidget.tsx
// ─────────────────────────────────────────────────────────────
import { useMemo } from 'react';
import { useThemeMode, DataLoadFailed, RefreshingBadge } from '@ska-octopus-widget-sdk/widget-sdk';
import Plot from 'react-plotly.js';
import { getStateColor } from './constants/stateColors';
import { getLegacyStateColor } from './constants/legacyStateColors';
import styles from './TangoExplorerWidget.module.css';
import {
grey,
red,
orange,
lightBlue,
lightGreen,
pink,
teal,
cyan,
deepPurple,
blue
} from '@mui/material/colors';
import { useTangoExplorerLogic } from './TangoExplorerLogic';
export interface TangoExplorerWidgetProps {
instanceId?: string;
tangoDB?: string | string[];
useEnumLabels?: boolean;
groupByServer?: boolean;
stateColorMode?: 'state' | 'tango';
}
/** Keep hover template stable across renders (prevents plot updates). */
const HOVER_TEMPLATE = '%{label}<br>Value: %{value}<br>%{percentParent:.2%}<extra></extra>';
export default function TangoExplorerWidget(config: TangoExplorerWidgetProps) {
const { groupByServer = true, stateColorMode = 'state' } = config;
const logic = useTangoExplorerLogic(config);
const { isDark } = useThemeMode();
const { deviceNames, byServer, deviceStates, devicesLoading, devicesError, refetchDevices } =
logic;
// 1. Build sunburst data
const sunburst = useMemo(() => {
const stateColor = (label: string): string => {
if (stateColorMode === 'tango') {
return getLegacyStateColor(label) ?? getStateColor('UNKNOWN');
}
return getStateColor(label);
};
const rootId = 'root';
const ids: string[] = [rootId];
const labels: string[] = ['Tango Devices'];
const parents: string[] = [''];
const values: number[] = [deviceNames.length];
const colors: string[] = [isDark ? grey[900] : grey[200]];
if (groupByServer) {
/* ------- ORIGINAL BEHAVIOUR ------- */
const palette = [
red[300],
orange[300],
lightGreen[300],
lightBlue[300],
pink[300],
cyan[300],
deepPurple[300],
teal[300],
blue[300]
];
for (const [i, [sid, devs]] of Array.from(byServer.entries()).entries()) {
const serverId = `server:${sid}`;
ids.push(serverId);
labels.push(`Server ${sid}`);
parents.push(rootId);
values.push(devs.length);
colors.push(palette[i % palette.length]);
const counts = new Map<string, number>();
const mapping = new Map<string, { label: string; names: string[] }>();
for (const d of devs) {
const raw = (deviceStates[d.name] ?? 'unknown') as string;
const norm = raw.trim().toLowerCase();
const entry = mapping.get(norm) ?? { label: String(raw), names: [] };
entry.names.push(d.name);
mapping.set(norm, entry);
counts.set(norm, (counts.get(norm) ?? 0) + 1);
}
for (const [normKey, count] of counts.entries()) {
const displayLabel = mapping.get(normKey)!.label;
const stateId = `server:${sid}|state:${normKey}`;
ids.push(stateId);
labels.push(displayLabel);
parents.push(serverId);
values.push(count);
const col = stateColor(normKey);
colors.push(col);
for (const name of mapping.get(normKey)!.names) {
ids.push(`device:${name}`);
labels.push(name);
parents.push(stateId);
values.push(1);
colors.push(col);
}
}
}
} else {
/* ------- NEW GLOBAL-STATE GROUPING ------- */
const counts = new Map<string, number>();
const mapping = new Map<string, { label: string; names: string[] }>();
for (const name of deviceNames) {
const raw = (deviceStates[name] ?? 'unknown') as string;
const norm = raw.trim().toLowerCase();
const entry = mapping.get(norm) ?? { label: String(raw), names: [] };
entry.names.push(name);
mapping.set(norm, entry);
counts.set(norm, (counts.get(norm) ?? 0) + 1);
}
for (const [normKey, count] of counts.entries()) {
const displayLabel = mapping.get(normKey)!.label;
const stateId = `state:${normKey}`;
ids.push(stateId);
labels.push(displayLabel);
parents.push(rootId);
values.push(count);
const col = stateColor(normKey);
colors.push(col);
for (const name of mapping.get(normKey)!.names) {
ids.push(`device:${name}`);
labels.push(name);
parents.push(stateId);
values.push(1);
colors.push(col);
}
}
}
return { ids, labels, parents, values, colors } as const;
}, [byServer, deviceStates, deviceNames, isDark, groupByServer, stateColorMode]);
// 2. Layout & config for Plotly
const layout = useMemo(
() => ({
template: isDark ? 'plotly_dark' : undefined,
margin: { t: 0, l: 0, r: 0, b: 0 },
autosize: true as const,
paper_bgcolor: isDark ? '#1e1e1e' : 'transparent',
plot_bgcolor: isDark ? '#1e1e1e' : 'transparent',
font: { color: isDark ? '#e0e0e0' : '#000' },
uirevision: config.instanceId ?? 'tangoExplorer'
}),
[isDark, config.instanceId]
);
const plotData = useMemo(
() => [
{
type: 'sunburst' as const,
ids: sunburst.ids,
labels: sunburst.labels,
parents: sunburst.parents,
values: sunburst.values,
branchvalues: 'total' as const,
marker: { colors: sunburst.colors as string[] },
hovertemplate: HOVER_TEMPLATE,
uirevision: config.instanceId ?? 'tangoExplorer'
}
],
[sunburst, config.instanceId]
);
// 3. Render
if (devicesLoading && deviceNames.length === 0) {
return <div className={styles.loading}>🔄 Querying devices…</div>;
}
if (devicesError) {
return (
<DataLoadFailed
message="Failed to load devices"
error={String(devicesError)}
handleRetry={refetchDevices}
/>
);
}
Iif (sunburst.labels.length === 0) return null;
return (
<div className={styles.container} style={{ position: 'relative' }}>
<div className={styles.inner}>
<Plot
data={plotData}
layout={layout}
useResizeHandler
className={styles.plot}
config={{ responsive: true, displaylogo: false }}
/>
</div>
{/* Keep data visible; show non-blocking refresh indicator during polls */}
<RefreshingBadge active={devicesLoading} text="Refreshing…" position="top-right" />
</div>
);
}
|