|
| 1 | +import { CommandParser } from '../client/parser'; |
| 2 | +import { Command, ReplyUnion, UnwrapReply, ArrayReply, BlobStringReply, NumberReply } from '../RESP/types'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Hotkey entry with key name and metric value |
| 6 | + */ |
| 7 | +export interface HotkeyEntry { |
| 8 | + key: string; |
| 9 | + value: number; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * HOTKEYS GET response structure |
| 14 | + */ |
| 15 | +export interface HotkeysGetReply { |
| 16 | + trackingActive: number; |
| 17 | + sampleRatio: number; |
| 18 | + selectedSlots: Array<number>; |
| 19 | + sampledCommandSelectedSlotsMs?: number; |
| 20 | + allCommandsSelectedSlotsMs?: number; |
| 21 | + allCommandsAllSlotsMs: number; |
| 22 | + netBytesSampledCommandsSelectedSlots?: number; |
| 23 | + netBytesAllCommandsSelectedSlots?: number; |
| 24 | + netBytesAllCommandsAllSlots: number; |
| 25 | + collectionStartTimeUnixMs: number; |
| 26 | + collectionDurationMs: number; |
| 27 | + usedCpuSysMs: number; |
| 28 | + usedCpuUserMs: number; |
| 29 | + totalNetBytes: number; |
| 30 | + byCpuTime: Array<HotkeyEntry>; |
| 31 | + byNetBytes: Array<HotkeyEntry>; |
| 32 | +} |
| 33 | + |
| 34 | +type HotkeysGetRawReply = ArrayReply<BlobStringReply | NumberReply | ArrayReply<BlobStringReply | NumberReply>>; |
| 35 | + |
| 36 | +/** |
| 37 | + * Parse the hotkeys array into HotkeyEntry objects |
| 38 | + */ |
| 39 | +function parseHotkeysList(arr: Array<BlobStringReply | NumberReply>): Array<HotkeyEntry> { |
| 40 | + const result: Array<HotkeyEntry> = []; |
| 41 | + for (let i = 0; i < arr.length; i += 2) { |
| 42 | + result.push({ |
| 43 | + key: arr[i].toString(), |
| 44 | + value: Number(arr[i + 1]) |
| 45 | + }); |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Transform the raw reply into a structured object |
| 52 | + */ |
| 53 | +function transformHotkeysGetReply(reply: UnwrapReply<HotkeysGetRawReply>): HotkeysGetReply { |
| 54 | + const result: Partial<HotkeysGetReply> = {}; |
| 55 | + |
| 56 | + for (let i = 0; i < reply.length; i += 2) { |
| 57 | + const key = reply[i].toString(); |
| 58 | + const value = reply[i + 1]; |
| 59 | + |
| 60 | + switch (key) { |
| 61 | + case 'tracking-active': |
| 62 | + result.trackingActive = Number(value); |
| 63 | + break; |
| 64 | + case 'sample-ratio': |
| 65 | + result.sampleRatio = Number(value); |
| 66 | + break; |
| 67 | + case 'selected-slots': |
| 68 | + result.selectedSlots = (value as unknown as Array<NumberReply>).map(Number); |
| 69 | + break; |
| 70 | + case 'sampled-command-selected-slots-ms': |
| 71 | + result.sampledCommandSelectedSlotsMs = Number(value); |
| 72 | + break; |
| 73 | + case 'all-commands-selected-slots-ms': |
| 74 | + result.allCommandsSelectedSlotsMs = Number(value); |
| 75 | + break; |
| 76 | + case 'all-commands-all-slots-ms': |
| 77 | + result.allCommandsAllSlotsMs = Number(value); |
| 78 | + break; |
| 79 | + case 'net-bytes-sampled-commands-selected-slots': |
| 80 | + result.netBytesSampledCommandsSelectedSlots = Number(value); |
| 81 | + break; |
| 82 | + case 'net-bytes-all-commands-selected-slots': |
| 83 | + result.netBytesAllCommandsSelectedSlots = Number(value); |
| 84 | + break; |
| 85 | + case 'net-bytes-all-commands-all-slots': |
| 86 | + result.netBytesAllCommandsAllSlots = Number(value); |
| 87 | + break; |
| 88 | + case 'collection-start-time-unix-ms': |
| 89 | + result.collectionStartTimeUnixMs = Number(value); |
| 90 | + break; |
| 91 | + case 'collection-duration-ms': |
| 92 | + result.collectionDurationMs = Number(value); |
| 93 | + break; |
| 94 | + case 'used-cpu-sys-ms': |
| 95 | + result.usedCpuSysMs = Number(value); |
| 96 | + break; |
| 97 | + case 'used-cpu-user-ms': |
| 98 | + result.usedCpuUserMs = Number(value); |
| 99 | + break; |
| 100 | + case 'total-net-bytes': |
| 101 | + result.totalNetBytes = Number(value); |
| 102 | + break; |
| 103 | + case 'by-cpu-time': |
| 104 | + result.byCpuTime = parseHotkeysList(value as unknown as Array<BlobStringReply | NumberReply>); |
| 105 | + break; |
| 106 | + case 'by-net-bytes': |
| 107 | + result.byNetBytes = parseHotkeysList(value as unknown as Array<BlobStringReply | NumberReply>); |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return result as HotkeysGetReply; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * HOTKEYS GET command - returns hotkeys tracking data |
| 117 | + * |
| 118 | + * State transitions: |
| 119 | + * - ACTIVE -> returns data (does not stop) |
| 120 | + * - STOPPED -> returns data |
| 121 | + * - EMPTY -> returns nil |
| 122 | + */ |
| 123 | +export default { |
| 124 | + NOT_KEYED_COMMAND: true, |
| 125 | + IS_READ_ONLY: true, |
| 126 | + /** |
| 127 | + * Returns the top K hotkeys by CPU time and network bytes. |
| 128 | + * Returns nil if no tracking has been started or tracking was reset. |
| 129 | + * @param parser - The Redis command parser |
| 130 | + * @see https://redis.io/commands/hotkeys-get/ |
| 131 | + */ |
| 132 | + parseCommand(parser: CommandParser) { |
| 133 | + parser.push('HOTKEYS', 'GET'); |
| 134 | + }, |
| 135 | + transformReply: { |
| 136 | + 2: (reply: UnwrapReply<HotkeysGetRawReply> | null): HotkeysGetReply | null => { |
| 137 | + if (reply === null) return null; |
| 138 | + return transformHotkeysGetReply(reply); |
| 139 | + }, |
| 140 | + 3: undefined as unknown as () => ReplyUnion |
| 141 | + }, |
| 142 | + unstableResp3: true |
| 143 | +} as const satisfies Command; |
| 144 | + |
0 commit comments