|
| 1 | +import {cn} from '../../../../utils/cn'; |
| 2 | + |
| 3 | +import './ThreadStatesBar.scss'; |
| 4 | + |
| 5 | +const b = cn('thread-states-bar'); |
| 6 | + |
| 7 | +interface ThreadStatesBarProps { |
| 8 | + states?: Record<string, number>; |
| 9 | + totalThreads?: number; |
| 10 | + className?: string; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Thread state colors based on the state type |
| 15 | + */ |
| 16 | +const getStateColor = (state: string): string => { |
| 17 | + switch (state.toUpperCase()) { |
| 18 | + case 'R': // Running |
| 19 | + return 'var(--g-color-text-positive)'; |
| 20 | + case 'S': // Sleeping |
| 21 | + return 'var(--g-color-text-secondary)'; |
| 22 | + case 'D': // Uninterruptible sleep |
| 23 | + return 'var(--g-color-text-warning)'; |
| 24 | + case 'Z': // Zombie |
| 25 | + case 'T': // Stopped |
| 26 | + case 'X': // Dead |
| 27 | + return 'var(--g-color-text-danger)'; |
| 28 | + default: |
| 29 | + return 'var(--g-color-text-misc)'; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Component to display thread states as a horizontal bar chart |
| 35 | + */ |
| 36 | +export function ThreadStatesBar({states = {}, totalThreads, className}: ThreadStatesBarProps) { |
| 37 | + const total = totalThreads || Object.values(states).reduce((sum, count) => sum + count, 0); |
| 38 | + |
| 39 | + if (total === 0) { |
| 40 | + return <div className={b(null, className)}>No threads</div>; |
| 41 | + } |
| 42 | + |
| 43 | + const stateEntries = Object.entries(states).filter(([, count]) => count > 0); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className={b(null, className)}> |
| 47 | + <div className={b('bar')}> |
| 48 | + {stateEntries.map(([state, count]) => { |
| 49 | + const percentage = (count / total) * 100; |
| 50 | + return ( |
| 51 | + <div |
| 52 | + key={state} |
| 53 | + className={b('segment')} |
| 54 | + style={{ |
| 55 | + width: `${percentage}%`, |
| 56 | + backgroundColor: getStateColor(state), |
| 57 | + }} |
| 58 | + title={`${state}: ${count} threads (${Math.round(percentage)}%)`} |
| 59 | + /> |
| 60 | + ); |
| 61 | + })} |
| 62 | + </div> |
| 63 | + <div className={b('legend')}> |
| 64 | + {stateEntries.map(([state, count]) => ( |
| 65 | + <span key={state} className={b('legend-item')}> |
| 66 | + <span |
| 67 | + className={b('legend-color')} |
| 68 | + style={{backgroundColor: getStateColor(state)}} |
| 69 | + /> |
| 70 | + {state}: {count} |
| 71 | + </span> |
| 72 | + ))} |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
0 commit comments