redis 1.2.2

Redis driver for Rust.
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
//! Defines types to use with the HOTKEYS commands.
//!
//! The HOTKEYS command is a stateful, node-local command requiring session affinity.
//! It should only be used on standalone clients (Connection, MultiplexedConnection, etc.)
//! and NOT on cluster clients (ClusterConnection).
//!
//! # Command Syntax (Redis 8.6.0+)
//!
//! ```text
//! HOTKEYS START METRICS count [CPU] [NET] [COUNT k] [DURATION seconds] [SAMPLE ratio] [SLOTS count slot [slot ...]]
//! HOTKEYS GET
//! HOTKEYS STOP
//! HOTKEYS RESET
//! ```
//!
//! # Using HOTKEYS in Cluster Mode
//!
//! While the high-level `HotkeysCommands` trait is not available on `ClusterConnection`,
//! HOTKEYS commands can still be used in cluster mode through `route_command` with
//! explicit routing. Two patterns are useful:
//!
//! ## Routing to a single node
//!
//! Useful for inspecting a specific shard, e.g. when investigating a known hot slot.
//!
//! ```rust,no_run
//! # #[cfg(feature = "cluster")]
//! # {
//! use redis::cluster::ClusterClient;
//! use redis::cluster_routing::{RoutingInfo, SingleNodeRoutingInfo};
//! use redis::{cmd, FromRedisValue, HotkeysOptions, HotkeysResponse};
//!
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/"];
//! let client = ClusterClient::new(nodes).unwrap();
//! let mut connection = client.get_connection().unwrap();
//!
//! // Route to a specific node
//! let routing = RoutingInfo::SingleNode(SingleNodeRoutingInfo::ByAddress {
//!     host: "127.0.0.1".to_string(),
//!     port: 6379,
//! });
//!
//! // Start tracking on that specific node - track keys by CPU time percentage
//! let opts = HotkeysOptions::new_with_cpu();
//! let _ = connection.route_command(
//!     &cmd("HOTKEYS").arg("START").arg(opts),
//!     routing.clone()
//! ).unwrap();
//!
//! // ... perform operations ...
//!
//! // Get metrics from the same node
//! let value = connection.route_command(
//!     &cmd("HOTKEYS").arg("GET"),
//!     routing.clone()
//! ).unwrap();
//! let response = HotkeysResponse::from_redis_value(value).unwrap();
//!
//! // Stop tracking on that node
//! let _ = connection.route_command(
//!     &cmd("HOTKEYS").arg("STOP"),
//!     routing
//! ).unwrap();
//! # }
//! ```
//!
//! ## Routing to all primaries (cluster-wide tracking)
//!
//! For cluster-wide observability the natural pattern is to fan the command out to
//! every primary so each shard tracks its own keys. `START` and `STOP` can use
//! [`ResponsePolicy::AllSucceeded`](crate::cluster_routing::ResponsePolicy::AllSucceeded)
//! to assert every node accepted the command. `GET` should be issued without a
//! response policy: the response is then a [`Value::Map`] keyed by node address,
//! and each entry is parsed with [`HotkeysResponse::from_redis_value`].
//!
//! ```rust,no_run
//! # #[cfg(feature = "cluster")]
//! # {
//! use redis::cluster::ClusterClient;
//! use redis::cluster_routing::{
//!     MultipleNodeRoutingInfo, ResponsePolicy, RoutingInfo,
//! };
//! use redis::{cmd, from_redis_value, FromRedisValue, HotkeysOptions, HotkeysResponse, Value};
//!
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/"];
//! let client = ClusterClient::new(nodes).unwrap();
//! let mut connection = client.get_connection().unwrap();
//!
//! // START on every primary and require success everywhere.
//! // `with_slots` is not used in here and each primary tracks the keys for the slots it owns.
//! let start_opts = HotkeysOptions::new_with_cpu().and_net();
//! let _ = connection.route_command(
//!     &cmd("HOTKEYS").arg("START").arg(&start_opts),
//!     RoutingInfo::MultiNode((
//!         MultipleNodeRoutingInfo::AllMasters,
//!         Some(ResponsePolicy::AllSucceeded),
//!     )),
//! ).unwrap();
//!
//! // ... perform operations ...
//!
//! // GET from every primary with no policy.
//! // The cluster client will get back the per-node responses.
//! let value = connection.route_command(
//!     &cmd("HOTKEYS").arg("GET"),
//!     RoutingInfo::MultiNode((MultipleNodeRoutingInfo::AllMasters, None)),
//! ).unwrap();
//! if let Value::Map(per_node) = value {
//!     for (addr_val, response_val) in per_node {
//!         let addr: String = from_redis_value(addr_val).unwrap();
//!         let snapshot = HotkeysResponse::from_redis_value(response_val).unwrap();
//!         println!("node {addr}: {} hot keys by CPU",
//!             snapshot.by_cpu_time_us.as_ref().map(|v| v.len()).unwrap_or(0));
//!     }
//! }
//!
//! // STOP on every primary.
//! let _ = connection.route_command(
//!     &cmd("HOTKEYS").arg("STOP"),
//!     RoutingInfo::MultiNode((
//!         MultipleNodeRoutingInfo::AllMasters,
//!         Some(ResponsePolicy::AllSucceeded),
//!     )),
//! ).unwrap();
//! # }
//! ```

use crate::errors::ParsingError;
use crate::types::{FromRedisValue, RedisWrite, ToRedisArgs, Value};
use std::collections::HashMap;

/// Minimum value for the COUNT parameter of HOTKEYS START.
pub const HOTKEYS_COUNT_MIN: u64 = 1;
/// Maximum value for the COUNT parameter of HOTKEYS START.
pub const HOTKEYS_COUNT_MAX: u64 = 64;

/// Options for the HOTKEYS START command.
///
/// At least one of `cpu` or `net` must be enabled to specify which metrics to collect.
/// The `METRICS count` is automatically derived from how many metric types are enabled.
///
/// Use [`HotkeysOptions::new_with_cpu()`] or [`HotkeysOptions::new_with_net()`] constructors to create
/// valid options with at least one metric enabled.
///
/// # Example
///
/// ```rust,no_run
/// use redis::{HotkeysOptions, HotkeysCommands};
///
/// # fn example() -> redis::RedisResult<()> {
/// let client = redis::Client::open("redis://127.0.0.1/")?;
/// let mut con = client.get_connection()?;
///
/// // Track hotkeys by both CPU and network usage for 60 seconds
/// let opts = HotkeysOptions::new_with_cpu()
///     .and_net()
///     .with_duration_secs(60);
///
/// con.hotkeys_start(opts)?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct HotkeysOptions {
    /// Track hotkeys by CPU time percentage
    cpu: bool,
    /// Track hotkeys by network bytes percentage
    net: bool,
    /// Value of K for top-K hotkeys tracking (optional COUNT parameter)
    count_k: Option<u64>,
    /// Duration in seconds for tracking (optional DURATION parameter)
    duration_secs: Option<u64>,
    /// Sampling ratio for probabilistic tracking (optional SAMPLE parameter)
    sample_ratio: Option<u64>,
    /// Specific slots to track in cluster mode (optional SLOTS parameter)
    slots: Option<Vec<u16>>,
}

impl HotkeysOptions {
    /// Creates options to track hotkeys by CPU time percentage.
    ///
    /// # Example
    ///
    /// ```rust
    /// use redis::HotkeysOptions;
    ///
    /// // Track hotkeys by CPU time
    /// let opts = HotkeysOptions::new_with_cpu();
    ///
    /// // Track by both CPU and network
    /// let opts = HotkeysOptions::new_with_cpu().and_net();
    /// ```
    pub fn new_with_cpu() -> Self {
        Self {
            cpu: true,
            net: false,
            count_k: None,
            duration_secs: None,
            sample_ratio: None,
            slots: None,
        }
    }

    /// Creates options to track hotkeys by network bytes percentage.
    ///
    /// # Example
    ///
    /// ```rust
    /// use redis::HotkeysOptions;
    ///
    /// // Track hotkeys by network bytes
    /// let opts = HotkeysOptions::new_with_net();
    ///
    /// // Track by both network and CPU
    /// let opts = HotkeysOptions::new_with_net().and_cpu();
    /// ```
    pub fn new_with_net() -> Self {
        Self {
            cpu: false,
            net: true,
            count_k: None,
            duration_secs: None,
            sample_ratio: None,
            slots: None,
        }
    }

    /// Also track hotkeys by CPU time percentage.
    ///
    /// Used when both metrics are needed and the options were created using [`HotkeysOptions::new_with_net()`]
    pub fn and_cpu(mut self) -> Self {
        self.cpu = true;
        self
    }

    /// Also track hotkeys by network bytes percentage.
    ///
    /// Used when both metrics are needed and the options were created using [`HotkeysOptions::new_with_cpu()`]
    pub fn and_net(mut self) -> Self {
        self.net = true;
        self
    }

    /// Returns the number of metrics being tracked.
    fn metrics_count(&self) -> u64 {
        self.cpu as u64 + self.net as u64
    }

    /// Set the value of K for top-K hotkeys tracking.
    ///
    /// This is the COUNT parameter in the Redis command.
    ///
    /// # Errors
    /// Returns an error if `k` is not in the valid range `1..=64`
    /// (see [`HOTKEYS_COUNT_MIN`] and [`HOTKEYS_COUNT_MAX`]).
    pub fn with_count(mut self, k: u64) -> Result<Self, String> {
        if !(HOTKEYS_COUNT_MIN..=HOTKEYS_COUNT_MAX).contains(&k) {
            return Err(format!(
                "COUNT must be between {HOTKEYS_COUNT_MIN} and {HOTKEYS_COUNT_MAX}, got: {k}"
            ));
        }
        self.count_k = Some(k);
        Ok(self)
    }

    /// Set the duration in seconds for how long tracking should run.
    ///
    /// After this time period, tracking will automatically stop.
    /// If not specified, tracking continues until manually stopped with HOTKEYS STOP.
    pub fn with_duration_secs(mut self, seconds: u64) -> Self {
        self.duration_secs = Some(seconds);
        self
    }

    /// Set the sampling ratio for probabilistic tracking.
    ///
    /// Each key is sampled with probability 1/ratio. Higher values reduce
    /// performance impact but may miss some hotkeys. Lower values provide
    /// more accurate results but with higher performance cost.
    pub fn with_sample_ratio(mut self, ratio: u64) -> Self {
        self.sample_ratio = Some(ratio);
        self
    }

    /// Set specific hash slots to track in a cluster environment.
    ///
    /// Only keys that hash to the specified slots will be tracked.
    /// Useful for narrowing tracking to a subset of the slots owned by a single shard.
    ///
    /// # When to use
    ///
    /// This option is meaningful only in cluster mode and only with **single-node**
    /// routing (e.g. [`RoutingInfo::SingleNode`](crate::cluster_routing::RoutingInfo::SingleNode))
    /// targeting a primary that owns the requested slots. The Redis server will
    /// reject the command if any of the requested slots are not owned by the
    /// receiving node.
    ///
    /// When fanning the command out to every node (e.g.
    /// [`MultipleNodeRoutingInfo::AllMasters`](crate::cluster_routing::MultipleNodeRoutingInfo::AllMasters)),
    /// omit `with_slots` and let each primary track the keys for the slots it
    /// owns, otherwise every primary that does not own the supplied slot list
    /// will return an error.
    ///
    /// Note: Using SLOTS when not in cluster mode will result in an error.
    pub fn with_slots(mut self, slots: Vec<u16>) -> Self {
        self.slots = Some(slots);
        self
    }
}

impl ToRedisArgs for HotkeysOptions {
    fn write_redis_args<W>(&self, out: &mut W)
    where
        W: ?Sized + RedisWrite,
    {
        // METRICS count [CPU] [NET] - required
        out.write_arg(b"METRICS");
        out.write_arg_fmt(self.metrics_count());

        if self.cpu {
            out.write_arg(b"CPU");
        }

        if self.net {
            out.write_arg(b"NET");
        }

        // Optional: COUNT k
        if let Some(k) = self.count_k {
            out.write_arg(b"COUNT");
            out.write_arg_fmt(k);
        }

        // Optional: DURATION seconds
        if let Some(secs) = self.duration_secs {
            out.write_arg(b"DURATION");
            out.write_arg_fmt(secs);
        }

        // Optional: SAMPLE ratio
        if let Some(ratio) = self.sample_ratio {
            out.write_arg(b"SAMPLE");
            out.write_arg_fmt(ratio);
        }

        // Optional: SLOTS count slot [slot ...]
        if let Some(ref slots) = self.slots {
            out.write_arg(b"SLOTS");
            out.write_arg_fmt(slots.len());
            for slot in slots {
                out.write_arg_fmt(slot);
            }
        }
    }

    fn num_of_args(&self) -> usize {
        // METRICS + count
        let mut n = 2;
        n += self.cpu as usize;
        n += self.net as usize;
        if self.count_k.is_some() {
            n += 2;
        }
        if self.duration_secs.is_some() {
            n += 2;
        }
        if self.sample_ratio.is_some() {
            n += 2;
        }
        if let Some(ref slots) = self.slots {
            // SLOTS + count + one arg per slot
            n += 2 + slots.len();
        }
        n
    }
}

/// A single hotkey entry with its metric value.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct HotKeyEntry {
    /// The key name.
    pub key: String,
    /// The metric value (CPU time in microseconds or network bytes, depending on context).
    pub value: u64,
}

/// Represents a range of slots.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct SlotRange {
    /// Start of the slot range (inclusive).
    pub start: u16,
    /// End of the slot range (inclusive).
    pub end: u16,
}

/// Response from the HOTKEYS GET command.
///
/// Contains information about the hotkeys tracking session,
/// including tracking metadata, performance statistics, and lists of top K
/// hot keys sorted by the metrics specified in HOTKEYS START.
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct HotkeysResponse {
    /// Whether tracking is currently active (1) or stopped (0).
    pub tracking_active: bool,
    /// The sampling ratio used during tracking.
    pub sample_ratio: u64,
    /// Array of selected slot ranges.
    pub selected_slots: Vec<SlotRange>,
    /// CPU time in microseconds for all commands on all slots.
    pub all_commands_all_slots_us: u64,
    /// Network bytes for all commands on all slots.
    pub net_bytes_all_commands_all_slots: u64,
    /// Unix timestamp in milliseconds when tracking started.
    pub collection_start_time_unix_ms: u64,
    /// Duration of tracking in milliseconds.
    pub collection_duration_ms: u64,
    /// User CPU time used in milliseconds (only when CPU metric was specified).
    pub total_cpu_time_user_ms: Option<u64>,
    /// System CPU time used in milliseconds (only when CPU metric was specified).
    pub total_cpu_time_sys_ms: Option<u64>,
    /// Total network bytes processed (only when NET metric was specified).
    pub total_net_bytes: Option<u64>,
    /// Array of hotkeys sorted by CPU time in microseconds (only when CPU metric was specified).
    pub by_cpu_time_us: Option<Vec<HotKeyEntry>>,
    /// Array of hotkeys sorted by network bytes (only when NET metric was specified).
    pub by_net_bytes: Option<Vec<HotKeyEntry>>,

    // Cluster-specific fields (when SLOTS was used)
    /// CPU time in microseconds for sampled commands in selected slots (cluster mode with SAMPLE).
    pub sampled_commands_selected_slots_us: Option<u64>,
    /// CPU time in microseconds for all commands in selected slots (cluster mode).
    pub all_commands_selected_slots_us: Option<u64>,
    /// Network bytes for sampled commands in selected slots (cluster mode with SAMPLE).
    pub net_bytes_sampled_commands_selected_slots: Option<u64>,
    /// Network bytes for all commands on selected slots (cluster mode).
    pub net_bytes_all_commands_selected_slots: Option<u64>,
}

/// Helper to strip surrounding quotes from a string if present
fn strip_quotes(s: String) -> String {
    if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') {
        s[1..s.len() - 1].to_string()
    } else {
        s
    }
}

/// Helper function to parse a key-value pair array into HotKeyEntry vec
fn parse_hotkey_entries(arr: &[Value]) -> Result<Vec<HotKeyEntry>, ParsingError> {
    use crate::types::from_redis_value_ref;

    let mut entries = Vec::with_capacity(arr.len() / 2);

    let mut iter = arr.iter();
    while let Some(key_val) = iter.next() {
        let key: String = from_redis_value_ref(key_val)?;
        // Strip surrounding quotes if present (Redis returns quoted keys)
        let key = strip_quotes(key);
        let value: u64 = iter
            .next()
            .ok_or_else(|| ParsingError::from("Expected value after key in hotkey entry"))
            .and_then(from_redis_value_ref)?;

        entries.push(HotKeyEntry { key, value });
    }

    Ok(entries)
}

/// Helper function to parse slot ranges from the selected-slots array
fn parse_slot_ranges(arr: &[Value]) -> Result<Vec<SlotRange>, ParsingError> {
    use crate::types::from_redis_value_ref;

    let mut ranges = Vec::with_capacity(arr.len());

    for item in arr {
        let Value::Array(range_arr) = item else {
            crate::errors::invalid_type_error!("Expected array for slot range", item);
        };

        match range_arr.len() {
            1 => {
                let slot: u16 = from_redis_value_ref(&range_arr[0])?;
                ranges.push(SlotRange {
                    start: slot,
                    end: slot,
                });
            }
            n if n >= 2 => {
                let start: u16 = from_redis_value_ref(&range_arr[0])?;
                let end: u16 = from_redis_value_ref(&range_arr[1])?;
                ranges.push(SlotRange { start, end });
            }
            _ => crate::errors::invalid_type_error!("Empty slot range entry", range_arr),
        }
    }

    Ok(ranges)
}

impl FromRedisValue for HotkeysResponse {
    fn from_redis_value(v: Value) -> Result<Self, ParsingError> {
        use crate::types::from_redis_value;

        // Redis 8.6 wraps every HOTKEYS GET response in a single-element outer
        // array with one entry per tracking session. Unwrap it here so the inner
        // value (Array of field/value pairs in RESP2, Map in RESP3) can be
        // parsed uniformly below. The passthrough arm keeps unit-test fixtures
        // that build the inner value directly working without needing to
        // mirror the server's outer wrapping.
        let v = match v {
            Value::Array(mut arr) if arr.len() == 1 => arr.remove(0),
            other => other,
        };

        // The response can be an Array (RESP2) or Map (RESP3)
        // Parse it into a HashMap for easier field access
        let mut fields: HashMap<String, Value> = match v {
            Value::Array(arr) => {
                // RESP2: flat array with alternating field names and values
                let mut map = HashMap::new();
                let mut iter = arr.into_iter();
                while let Some(key) = iter.next() {
                    let key_str: String = from_redis_value(key)?;
                    // Strip surrounding quotes if present (Redis returns quoted keys)
                    let key_str = strip_quotes(key_str);
                    if let Some(val) = iter.next() {
                        map.insert(key_str, val);
                    }
                }
                map
            }
            Value::Map(pairs) => {
                // RESP3: proper map
                let mut map = HashMap::new();
                for (k, v) in pairs {
                    let key_str: String = from_redis_value(k)?;
                    let key_str = strip_quotes(key_str);
                    map.insert(key_str, v);
                }
                map
            }
            _ => {
                crate::errors::invalid_type_error!(
                    "Expected array or map response for HOTKEYS GET",
                    v
                );
            }
        };

        let mut response = HotkeysResponse::default();

        // Parse required fields
        if let Some(v) = fields.remove("tracking-active") {
            response.tracking_active = from_redis_value::<i64>(v)? != 0;
        }

        if let Some(v) = fields.remove("sample-ratio") {
            response.sample_ratio = from_redis_value(v)?;
        }

        if let Some(Value::Array(arr)) = fields.remove("selected-slots") {
            response.selected_slots = parse_slot_ranges(&arr)?;
        }

        if let Some(v) = fields.remove("all-commands-all-slots-us") {
            response.all_commands_all_slots_us = from_redis_value(v)?;
        }

        if let Some(v) = fields.remove("net-bytes-all-commands-all-slots") {
            response.net_bytes_all_commands_all_slots = from_redis_value(v)?;
        }

        if let Some(v) = fields.remove("collection-start-time-unix-ms") {
            response.collection_start_time_unix_ms = from_redis_value(v)?;
        }

        if let Some(v) = fields.remove("collection-duration-ms") {
            response.collection_duration_ms = from_redis_value(v)?;
        }

        // Parse optional CPU-related fields
        if let Some(v) = fields.remove("total-cpu-time-user-ms") {
            response.total_cpu_time_user_ms = Some(from_redis_value(v)?);
        }

        if let Some(v) = fields.remove("total-cpu-time-sys-ms") {
            response.total_cpu_time_sys_ms = Some(from_redis_value(v)?);
        }

        if let Some(Value::Array(arr)) = fields.remove("by-cpu-time-us") {
            response.by_cpu_time_us = Some(parse_hotkey_entries(&arr)?);
        }

        // Parse optional NET-related fields
        if let Some(v) = fields.remove("total-net-bytes") {
            response.total_net_bytes = Some(from_redis_value(v)?);
        }

        if let Some(Value::Array(arr)) = fields.remove("by-net-bytes") {
            response.by_net_bytes = Some(parse_hotkey_entries(&arr)?);
        }

        // Parse cluster-specific fields
        if let Some(v) = fields.remove("sampled-commands-selected-slots-us") {
            response.sampled_commands_selected_slots_us = Some(from_redis_value(v)?);
        }

        if let Some(v) = fields.remove("all-commands-selected-slots-us") {
            response.all_commands_selected_slots_us = Some(from_redis_value(v)?);
        }

        if let Some(v) = fields.remove("net-bytes-sampled-commands-selected-slots") {
            response.net_bytes_sampled_commands_selected_slots = Some(from_redis_value(v)?);
        }

        if let Some(v) = fields.remove("net-bytes-all-commands-selected-slots") {
            response.net_bytes_all_commands_selected_slots = Some(from_redis_value(v)?);
        }

        Ok(response)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hotkeys_options_cpu_constructor() {
        let opts = HotkeysOptions::new_with_cpu();
        assert_eq!(opts.num_of_args(), 3); // METRICS 1 CPU
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 3);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"1");
        assert_eq!(args[2], b"CPU");
    }

    #[test]
    fn test_hotkeys_options_net_constructor() {
        let opts = HotkeysOptions::new_with_net();
        assert_eq!(opts.num_of_args(), 3); // METRICS 1 NET
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 3);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"1");
        assert_eq!(args[2], b"NET");
    }

    #[test]
    fn test_hotkeys_options_cpu_and_net() {
        let opts = HotkeysOptions::new_with_cpu().and_net();
        assert_eq!(opts.num_of_args(), 4); // METRICS 2 CPU NET
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 4);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"2");
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"NET");
    }

    #[test]
    fn test_hotkeys_options_net_and_cpu() {
        let opts = HotkeysOptions::new_with_net().and_cpu();
        assert_eq!(opts.num_of_args(), 4); // METRICS 2 CPU NET
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 4);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"2");
        // CPU comes before NET in serialization order
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"NET");
    }

    #[test]
    fn test_hotkeys_options_with_duration() {
        let opts = HotkeysOptions::new_with_cpu().with_duration_secs(60);
        assert_eq!(opts.num_of_args(), 5); // METRICS 1 CPU DURATION 60
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 5);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"1");
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"DURATION");
        assert_eq!(args[4], b"60");
    }

    #[test]
    fn test_hotkeys_options_with_count() {
        let opts = HotkeysOptions::new_with_cpu().with_count(50).unwrap();
        assert_eq!(opts.num_of_args(), 5); // METRICS 1 CPU COUNT 50
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 5);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"1");
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"COUNT");
        assert_eq!(args[4], b"50");
    }

    #[test]
    fn test_hotkeys_options_with_count_min_valid() {
        let opts = HotkeysOptions::new_with_cpu()
            .with_count(HOTKEYS_COUNT_MIN)
            .unwrap();
        let args = opts.to_redis_args();
        assert_eq!(args[3], b"COUNT");
        assert_eq!(args[4], HOTKEYS_COUNT_MIN.to_string().as_bytes());
    }

    #[test]
    fn test_hotkeys_options_with_count_max_valid() {
        let opts = HotkeysOptions::new_with_cpu()
            .with_count(HOTKEYS_COUNT_MAX)
            .unwrap();
        let args = opts.to_redis_args();
        assert_eq!(args[3], b"COUNT");
        assert_eq!(args[4], HOTKEYS_COUNT_MAX.to_string().as_bytes());
    }

    #[test]
    fn test_hotkeys_options_with_count_too_low() {
        let result = HotkeysOptions::new_with_cpu().with_count(HOTKEYS_COUNT_MIN - 1);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains(&format!(
            "COUNT must be between {HOTKEYS_COUNT_MIN} and {HOTKEYS_COUNT_MAX}"
        )));
    }

    #[test]
    fn test_hotkeys_options_with_count_too_high() {
        let result = HotkeysOptions::new_with_cpu().with_count(HOTKEYS_COUNT_MAX + 1);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains(&format!(
            "COUNT must be between {HOTKEYS_COUNT_MIN} and {HOTKEYS_COUNT_MAX}"
        )));
    }

    #[test]
    fn test_hotkeys_options_with_sample() {
        let opts = HotkeysOptions::new_with_cpu().with_sample_ratio(1000);
        assert_eq!(opts.num_of_args(), 5); // METRICS 1 CPU SAMPLE 1000
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 5);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"1");
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"SAMPLE");
        assert_eq!(args[4], b"1000");
    }

    #[test]
    fn test_hotkeys_options_with_slots() {
        let opts = HotkeysOptions::new_with_cpu().with_slots(vec![0, 100, 200]);
        assert_eq!(opts.num_of_args(), 8); // METRICS 1 CPU SLOTS 3 0 100 200
        let args = opts.to_redis_args();
        assert_eq!(args.len(), 8);
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"1");
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"SLOTS");
        assert_eq!(args[4], b"3");
        assert_eq!(args[5], b"0");
        assert_eq!(args[6], b"100");
        assert_eq!(args[7], b"200");
    }

    #[test]
    fn test_hotkeys_options_full() {
        let opts = HotkeysOptions::new_with_cpu()
            .and_net()
            .with_count(50)
            .unwrap()
            .with_duration_secs(120)
            .with_sample_ratio(500);
        // METRICS 2 CPU NET COUNT 50 DURATION 120 SAMPLE 500
        assert_eq!(opts.num_of_args(), 10);
        let args = opts.to_redis_args();
        assert_eq!(args[0], b"METRICS");
        assert_eq!(args[1], b"2");
        assert_eq!(args[2], b"CPU");
        assert_eq!(args[3], b"NET");
        assert_eq!(args[4], b"COUNT");
        assert_eq!(args[5], b"50");
        assert_eq!(args[6], b"DURATION");
        assert_eq!(args[7], b"120");
        assert_eq!(args[8], b"SAMPLE");
        assert_eq!(args[9], b"500");
    }

    #[test]
    fn test_hotkeys_response_parsing_resp2() {
        use crate::Value;

        // Simulate RESP2 flat array response
        let response = Value::Array(vec![
            Value::BulkString(b"tracking-active".to_vec()),
            Value::Int(1),
            Value::BulkString(b"sample-ratio".to_vec()),
            Value::Int(1),
            Value::BulkString(b"selected-slots".to_vec()),
            Value::Array(vec![Value::Array(vec![Value::Int(0), Value::Int(16383)])]),
            Value::BulkString(b"all-commands-all-slots-us".to_vec()),
            Value::Int(5000),
            Value::BulkString(b"net-bytes-all-commands-all-slots".to_vec()),
            Value::Int(2048),
            Value::BulkString(b"collection-start-time-unix-ms".to_vec()),
            Value::Int(1700000000000),
            Value::BulkString(b"collection-duration-ms".to_vec()),
            Value::Int(10000),
            Value::BulkString(b"total-cpu-time-user-ms".to_vec()),
            Value::Int(100),
            Value::BulkString(b"total-cpu-time-sys-ms".to_vec()),
            Value::Int(50),
            Value::BulkString(b"by-cpu-time-us".to_vec()),
            Value::Array(vec![
                Value::BulkString(b"key1".to_vec()),
                Value::Int(1500),
                Value::BulkString(b"key2".to_vec()),
                Value::Int(750),
            ]),
        ]);

        let result = HotkeysResponse::from_redis_value(response).unwrap();

        assert!(result.tracking_active);
        assert_eq!(result.sample_ratio, 1);
        assert_eq!(result.selected_slots.len(), 1);
        assert_eq!(result.selected_slots[0].start, 0);
        assert_eq!(result.selected_slots[0].end, 16383);
        assert_eq!(result.all_commands_all_slots_us, 5000);
        assert_eq!(result.net_bytes_all_commands_all_slots, 2048);
        assert_eq!(result.collection_start_time_unix_ms, 1700000000000);
        assert_eq!(result.collection_duration_ms, 10000);
        assert_eq!(result.total_cpu_time_user_ms, Some(100));
        assert_eq!(result.total_cpu_time_sys_ms, Some(50));

        let cpu_keys = result.by_cpu_time_us.unwrap();
        assert_eq!(cpu_keys.len(), 2);
        assert_eq!(cpu_keys[0].key, "key1");
        assert_eq!(cpu_keys[0].value, 1500);
        assert_eq!(cpu_keys[1].key, "key2");
        assert_eq!(cpu_keys[1].value, 750);
    }

    #[test]
    fn test_hotkeys_response_parsing_resp3() {
        use crate::Value;

        // Simulate RESP3 map response.
        let response = Value::Map(vec![
            (
                Value::BulkString(b"tracking-active".to_vec()),
                Value::Int(1),
            ),
            (Value::BulkString(b"sample-ratio".to_vec()), Value::Int(1)),
            (
                Value::BulkString(b"selected-slots".to_vec()),
                Value::Array(vec![Value::Array(vec![Value::Int(0), Value::Int(16383)])]),
            ),
            (
                Value::BulkString(b"all-commands-all-slots-us".to_vec()),
                Value::Int(5000),
            ),
            (
                Value::BulkString(b"all-commands-selected-slots-us".to_vec()),
                Value::Int(4000),
            ),
            (
                Value::BulkString(b"net-bytes-all-commands-all-slots".to_vec()),
                Value::Int(2048),
            ),
            (
                Value::BulkString(b"net-bytes-all-commands-selected-slots".to_vec()),
                Value::Int(1024),
            ),
            (
                Value::BulkString(b"collection-start-time-unix-ms".to_vec()),
                Value::Int(1700000000000),
            ),
            (
                Value::BulkString(b"collection-duration-ms".to_vec()),
                Value::Int(10000),
            ),
            (
                Value::BulkString(b"total-cpu-time-user-ms".to_vec()),
                Value::Int(100),
            ),
            (
                Value::BulkString(b"total-cpu-time-sys-ms".to_vec()),
                Value::Int(50),
            ),
            (
                Value::BulkString(b"by-cpu-time-us".to_vec()),
                Value::Array(vec![
                    Value::BulkString(b"key1".to_vec()),
                    Value::Int(1500),
                    Value::BulkString(b"key2".to_vec()),
                    Value::Int(750),
                ]),
            ),
        ]);

        let result = HotkeysResponse::from_redis_value(response).unwrap();

        assert!(result.tracking_active);
        assert_eq!(result.sample_ratio, 1);
        assert_eq!(result.selected_slots.len(), 1);
        assert_eq!(result.selected_slots[0].start, 0);
        assert_eq!(result.selected_slots[0].end, 16383);
        assert_eq!(result.all_commands_all_slots_us, 5000);
        assert_eq!(result.all_commands_selected_slots_us, Some(4000));
        assert_eq!(result.net_bytes_all_commands_all_slots, 2048);
        assert_eq!(result.net_bytes_all_commands_selected_slots, Some(1024));
        assert_eq!(result.collection_start_time_unix_ms, 1700000000000);
        assert_eq!(result.collection_duration_ms, 10000);
        assert_eq!(result.total_cpu_time_user_ms, Some(100));
        assert_eq!(result.total_cpu_time_sys_ms, Some(50));

        let cpu_keys = result.by_cpu_time_us.unwrap();
        assert_eq!(cpu_keys.len(), 2);
        assert_eq!(cpu_keys[0].key, "key1");
        assert_eq!(cpu_keys[0].value, 1500);
        assert_eq!(cpu_keys[1].key, "key2");
        assert_eq!(cpu_keys[1].value, 750);
    }

    #[test]
    fn test_hotkeys_response_parsing_with_net() {
        use crate::Value;

        let response = Value::Array(vec![
            Value::BulkString(b"tracking-active".to_vec()),
            Value::Int(0),
            Value::BulkString(b"sample-ratio".to_vec()),
            Value::Int(1),
            Value::BulkString(b"selected-slots".to_vec()),
            Value::Array(vec![]),
            Value::BulkString(b"all-commands-all-slots-us".to_vec()),
            Value::Int(0),
            Value::BulkString(b"net-bytes-all-commands-all-slots".to_vec()),
            Value::Int(4096),
            Value::BulkString(b"collection-start-time-unix-ms".to_vec()),
            Value::Int(1700000000000),
            Value::BulkString(b"collection-duration-ms".to_vec()),
            Value::Int(5000),
            Value::BulkString(b"total-net-bytes".to_vec()),
            Value::Int(8192),
            Value::BulkString(b"by-net-bytes".to_vec()),
            Value::Array(vec![
                Value::BulkString(b"bigkey".to_vec()),
                Value::Int(4096),
                Value::BulkString(b"smallkey".to_vec()),
                Value::Int(256),
            ]),
        ]);

        let result = HotkeysResponse::from_redis_value(response).unwrap();

        assert!(!result.tracking_active);
        assert_eq!(result.total_net_bytes, Some(8192));

        let net_keys = result.by_net_bytes.unwrap();
        assert_eq!(net_keys.len(), 2);
        assert_eq!(net_keys[0].key, "bigkey");
        assert_eq!(net_keys[0].value, 4096);
        assert_eq!(net_keys[1].key, "smallkey");
        assert_eq!(net_keys[1].value, 256);
    }

    #[test]
    fn test_hotkeys_response_nil() {
        use crate::Value;
        use crate::types::from_redis_value;

        // Redis returns `Nil` for HOTKEYS GET when there is no active tracking
        // session (e.g. never started, stopped, or reset). `hotkeys_get` decodes
        // into `Option<HotkeysResponse>`, so `Nil` must surface as `None` rather
        // than a default-constructed response.
        let response = Value::Nil;
        let result: Option<HotkeysResponse> = from_redis_value(response).unwrap();
        assert!(result.is_none());
    }
}