@@ -466,26 +466,49 @@ def _parse_node_line(line):
466466 line_items = line .split (" " )
467467 node_id , addr , flags , master_id , ping , pong , epoch , connected = line .split (" " )[:8 ]
468468 addr = addr .split ("@" )[0 ]
469- slots = [sl .split ("-" ) for sl in line_items [8 :]]
470469 node_dict = {
471470 "node_id" : node_id ,
472471 "flags" : flags ,
473472 "master_id" : master_id ,
474473 "last_ping_sent" : ping ,
475474 "last_pong_rcvd" : pong ,
476475 "epoch" : epoch ,
477- "slots" : slots ,
476+ "slots" : [],
477+ "migrations" : [],
478478 "connected" : True if connected == "connected" else False ,
479479 }
480+ if len (line_items ) >= 9 :
481+ slots , migrations = _parse_slots (line_items [8 :])
482+ node_dict ["slots" ], node_dict ["migrations" ] = slots , migrations
480483 return addr , node_dict
481484
482485
486+ def _parse_slots (slot_ranges ):
487+ slots , migrations = [], []
488+ for s_range in slot_ranges :
489+ if "->-" in s_range :
490+ slot_id , dst_node_id = s_range [1 :- 1 ].split ("->-" , 1 )
491+ migrations .append (
492+ {"slot" : slot_id , "node_id" : dst_node_id , "state" : "migrating" }
493+ )
494+ elif "-<-" in s_range :
495+ slot_id , src_node_id = s_range [1 :- 1 ].split ("-<-" , 1 )
496+ migrations .append (
497+ {"slot" : slot_id , "node_id" : src_node_id , "state" : "importing" }
498+ )
499+ else :
500+ s_range = [sl for sl in s_range .split ("-" )]
501+ slots .append (s_range )
502+
503+ return slots , migrations
504+
505+
483506def parse_cluster_nodes (response , ** options ):
484507 """
485- @see: https://redis.io/commands/cluster-nodes # string
486- @see: https://redis.io/commands/cluster-replicas # list of string
508+ @see: https://redis.io/commands/cluster-nodes # string / bytes
509+ @see: https://redis.io/commands/cluster-replicas # list of string / bytes
487510 """
488- if isinstance (response , str ):
511+ if isinstance (response , ( str , bytes ) ):
489512 response = response .splitlines ()
490513 return dict (_parse_node_line (str_if_bytes (node )) for node in response )
491514
0 commit comments