memtui 0.3.2

An interactive TUI (Terminal User Interface) for browsing and inspecting key-value stores (Redis, Memcached, etc.)
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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
#!/usr/bin/env python3
"""
Seed Redis, Memcached, and etcd datastores with sample data.

This script populates Redis, Memcached, and etcd instances with realistic sample data
for testing and development purposes.
"""

import argparse
import json
import socket
import sys
import time

try:
    import redis
    import etcd3gw
    from faker import Faker
    from tqdm import tqdm
    from colorama import Fore, Style, init as colorama_init
    colorama_init(autoreset=True)
except ImportError as e:
    print(f"Error: {e}", file=sys.stderr)
    print("Please install required packages:", file=sys.stderr)
    print("  pip install -r scripts/requirements.txt", file=sys.stderr)
    sys.exit(1)


class DatastoreSeeder:
    """Manages seeding of Redis, Memcached, and etcd datastores."""

    # Default base counts for various data types
    BASE_REDIS_USERS = 200
    BASE_MEMCACHED_USERS = 120
    BASE_ETCD_SERVICES = 50
    BASE_ETCD_CONFIGS = 30
    BASE_JOBS = 40
    BASE_EVENT_BUCKETS = 5
    BASE_LEADERBOARD_ENTRIES = 4
    BASE_EMAIL_QUEUE = 3
    BASE_ORGS = 12
    BASE_PROJECTS_PER_ORG = 4
    BASE_PRODUCTS = 30
    BASE_CARTS = 45
    BASE_NOTIFICATIONS = 120
    BASE_AUDIT_EVENTS = 200
    BASE_SERVICE_INSTANCES = 12
    BASE_API_TOKENS = 80
    BASE_RATE_LIMIT_ENTRIES = 140
    BASE_DOCUMENTS = 90
    BASE_DASHBOARDS = 25
    BASE_WEBHOOKS = 16
    BASE_SUPPORT_TICKETS = 60
    BASE_RELEASES = 20

    def __init__(
        self,
        redis_host: str = "127.0.0.1",
        redis_port: int = 6379,
        memcached_host: str = "127.0.0.1",
        memcached_port: int = 11211,
        etcd_host: str = "127.0.0.1",
        etcd_port: int = 2379,
        scale: float = 1.0,
        seed: int = None,
        flush: bool = False,
        verbose: bool = False,
    ):
        self.redis_host = redis_host
        self.redis_port = redis_port
        self.memcached_host = memcached_host
        self.memcached_port = memcached_port
        self.etcd_host = etcd_host
        self.etcd_port = etcd_port
        self.scale = scale
        self.flush = flush
        self.verbose = verbose

        # Calculate scaled counts
        self.num_users = int(self.BASE_REDIS_USERS * scale)
        self.num_memcached_users = int(self.BASE_MEMCACHED_USERS * scale)
        self.num_etcd_services = max(5, int(self.BASE_ETCD_SERVICES * scale))
        self.num_etcd_configs = max(5, int(self.BASE_ETCD_CONFIGS * scale))
        self.num_jobs = int(self.BASE_JOBS * scale)
        self.num_event_buckets = max(1, int(self.BASE_EVENT_BUCKETS * scale))
        self.num_leaderboard_entries = max(1, int(self.BASE_LEADERBOARD_ENTRIES * scale))
        self.num_email_queue = max(1, int(self.BASE_EMAIL_QUEUE * scale))
        self.num_orgs = max(1, int(self.BASE_ORGS * scale))
        self.num_projects_per_org = max(1, int(self.BASE_PROJECTS_PER_ORG * scale))
        self.num_products = max(5, int(self.BASE_PRODUCTS * scale))
        self.num_carts = max(5, int(self.BASE_CARTS * scale))
        self.num_notifications = max(10, int(self.BASE_NOTIFICATIONS * scale))
        self.num_audit_events = max(10, int(self.BASE_AUDIT_EVENTS * scale))
        self.num_service_instances = max(2, int(self.BASE_SERVICE_INSTANCES * scale))
        self.num_api_tokens = max(10, int(self.BASE_API_TOKENS * scale))
        self.num_rate_limits = max(10, int(self.BASE_RATE_LIMIT_ENTRIES * scale))
        self.num_documents = max(10, int(self.BASE_DOCUMENTS * scale))
        self.num_dashboards = max(5, int(self.BASE_DASHBOARDS * scale))
        self.num_webhooks = max(3, int(self.BASE_WEBHOOKS * scale))
        self.num_support_tickets = max(5, int(self.BASE_SUPPORT_TICKETS * scale))
        self.num_releases = max(5, int(self.BASE_RELEASES * scale))

        # Setup Faker
        self.fake = Faker()
        if seed is not None:
            Faker.seed(seed)

    def log(self, message: str, level: str = "info"):
        """Print colored log message."""
        colors = {
            "info": Fore.CYAN,
            "success": Fore.GREEN,
            "warning": Fore.YELLOW,
            "error": Fore.RED,
            "header": Fore.MAGENTA,
        }
        color = colors.get(level, Fore.WHITE)
        prefix = f"{color}▸{Style.RESET_ALL}"
        print(f"{prefix} {message}")

    def print_header(self, text: str):
        """Print a fancy header."""
        width = 60
        print()
        print(f"{Fore.MAGENTA}{'═' * width}{Style.RESET_ALL}")
        print(f"{Fore.MAGENTA}{text.center(width)}{Style.RESET_ALL}")
        print(f"{Fore.MAGENTA}{'═' * width}{Style.RESET_ALL}")
        print()

    def memcached_set(self, sock: socket.socket, key: str, value, ttl: int = 1800):
        """Helper to set a value in Memcached."""
        if not isinstance(value, str):
            value = json.dumps(value)
        payload = value.encode()
        command = f"set {key} 0 {ttl} {len(payload)}\r\n".encode()
        sock.sendall(command + payload + b"\r\n")
        sock.recv(1024)

    def seed_redis(self):
        """Populate Redis with sample data."""
        self.print_header("REDIS DATASTORE")

        self.log(f"Connecting to Redis at {Fore.YELLOW}{self.redis_host}:{self.redis_port}{Style.RESET_ALL}")

        # Connect to Redis
        try:
            r = redis.Redis(
                host=self.redis_host,
                port=self.redis_port,
                decode_responses=True,
                socket_connect_timeout=5,
            )
            r.ping()
            self.log("Connected successfully", "success")
        except redis.ConnectionError as e:
            self.log(f"Failed to connect: {e}", "error")
            raise

        # Flush all existing data (if requested)
        if self.flush:
            self.log("Flushing existing data", "warning")
            r.flushall()
        else:
            self.log("Skipping flush (use --flush to clear existing data)")

        # Basic configuration
        self.log("Creating configuration entries")
        pipe = r.pipeline()
        pipe.set("app:config:version", "1.3.0")
        pipe.hset("app:config:flags", mapping={
            "readonly": "true",
            "telemetry_enabled": "true",
            "theme": "nord",
        })

        # Email queue with realistic emails
        email_types = ["welcome", "receipt", "digest", "notification"]
        for _ in range(self.num_email_queue):
            email_type = self.fake.random_element(email_types)
            username = self.fake.user_name()
            pipe.lpush("queue:emails", f"{email_type}:{username}")

        # Feature flags
        pipe.sadd("feature:flags", "dark_mode", "live_reload", "beta_banner")

        # Leaderboard with realistic names
        leaderboard_data = {}
        for _ in range(self.num_leaderboard_entries):
            name = self.fake.first_name().lower()
            score = self.fake.random_int(min=800, max=1300)
            leaderboard_data[name] = score
        for name, score in leaderboard_data.items():
            pipe.zadd("leaderboard", {name: score})

        # Event streams
        pipe.xadd("streams:events", {
            "type": "signup",
            "user_id": "1010",
            "plan": "pro",
        })
        pipe.xadd("streams:events", {
            "type": "plan_change",
            "user_id": "1001",
            "plan": "enterprise",
        })

        pipe.execute()

        # Sessions and users with progress bar
        self.log(f"Creating {Fore.YELLOW}{self.num_users}{Style.RESET_ALL} users with sessions")
        tiers = ["free", "pro", "enterprise"]

        bar_color = "\033[36m"  # Cyan
        for i in tqdm(
            range(1, self.num_users + 1),
            desc=f"{bar_color}Users{Style.RESET_ALL}",
            unit="user",
            bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]",
        ):
            # Determine tier
            if i % 3 == 0:
                tier = "enterprise"
            elif i % 2 == 0:
                tier = "pro"
            else:
                tier = "free"

            # Create session
            session_token = self.fake.uuid4()
            r.set(f"session:{i}", session_token)

            # Create user with realistic data
            r.hset(f"user:{i}", mapping={
                "name": self.fake.name(),
                "email": self.fake.email(),
                "tier": tier,
                "joined": self.fake.date_time_this_year().isoformat(),
                "last_login": self.fake.date_time_this_month().isoformat(),
            })

        # Event buckets
        events_per_bucket = max(1, int(20 * self.scale))
        total_events = self.num_event_buckets * events_per_bucket
        self.log(f"Creating {Fore.YELLOW}{total_events}{Style.RESET_ALL} events across {Fore.YELLOW}{self.num_event_buckets}{Style.RESET_ALL} buckets")

        for bucket in tqdm(
            range(self.num_event_buckets),
            desc=f"{bar_color}Event buckets{Style.RESET_ALL}",
            unit="bucket",
            bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}",
        ):
            for event in range(1, events_per_bucket + 1):
                timestamp = int(time.time()) - self.fake.random_int(min=0, max=86400)
                r.lpush(f"events:{bucket}", f"{timestamp}-event-{bucket}-{event}")

        # Service registry and health checks
        self.log(f"Registering {Fore.YELLOW}{self.num_service_instances}{Style.RESET_ALL} service instances")
        services = ["api", "jobs", "worker", "scheduler", "web", "metrics"]
        regions = ["us-west-1", "us-east-1", "eu-central-1", "ap-southeast-1"]
        for instance in range(1, self.num_service_instances + 1):
            service_name = self.fake.random_element(services)
            instance_id = f"{service_name}:{instance:03d}"
            mapping = {
                "service": service_name,
                "region": self.fake.random_element(regions),
                "host": f"{service_name}-{instance}.svc.local",
                "status": self.fake.random_element(["passing", "warning", "critical"]),
                "started": self.fake.date_time_this_month().isoformat(),
            }
            r.hset(f"service:{instance_id}", mapping=mapping)
            r.sadd(f"service:index:{service_name}", instance_id)
            r.zadd("services:uptime", {instance_id: self.fake.random_int(min=900, max=86400)})

        # Organizations and projects
        self.log(f"Creating {Fore.YELLOW}{self.num_orgs}{Style.RESET_ALL} organizations with projects")
        industry_tags = ["fintech", "gaming", "saas", "health", "education", "iot"]
        for org_id in range(1, self.num_orgs + 1):
            org_key = f"org:{org_id:03d}"
            org_mapping = {
                "name": f"{self.fake.company()}",
                "plan": self.fake.random_element(["starter", "growth", "enterprise"]),
                "industry": self.fake.random_element(industry_tags),
                "owner": self.fake.email(),
                "created_at": self.fake.date_time_this_year().isoformat(),
            }
            r.hset(org_key, mapping=org_mapping)
            for project_idx in range(1, self.num_projects_per_org + 1):
                slug = f"proj-{org_id:03d}-{project_idx:02d}"
                project_key = f"{org_key}:project:{project_idx:02d}"
                r.hset(project_key, mapping={
                    "slug": slug,
                    "status": self.fake.random_element(["active", "maintenance", "archived"]),
                    "region": self.fake.random_element(regions),
                    "last_deploy": self.fake.date_time_this_month().isoformat(),
                })
                r.sadd(f"{org_key}:projects", slug)
                r.zadd("projects:activity", {slug: self.fake.random_int(min=1, max=1000)})
                r.lpush(f"{project_key}:activity", self.fake.sentence(nb_words=6))

        # Product catalog and inventory
        self.log(f"Publishing {Fore.YELLOW}{self.num_products}{Style.RESET_ALL} catalog items")
        categories = ["observability", "productivity", "security", "billing", "messaging"]
        for product_id in range(1, self.num_products + 1):
            sku = f"sku-{product_id:04d}"
            price = round(self.fake.pyfloat(left_digits=3, right_digits=2, positive=True, min_value=9, max_value=499), 2)
            stock = self.fake.random_int(min=1, max=250)
            r.hset(f"catalog:product:{product_id:04d}", mapping={
                "sku": sku,
                "name": self.fake.catch_phrase(),
                "category": self.fake.random_element(categories),
                "price": f"{price:.2f}",
                "currency": "USD",
                "updated": self.fake.date_time_this_month().isoformat(),
            })
            r.zadd("catalog:prices", {sku: price})
            r.hset("inventory:levels", sku, stock)
            if stock < 10:
                r.sadd("inventory:low_stock", sku)

        # Shopping carts
        self.log(f"Building {Fore.YELLOW}{self.num_carts}{Style.RESET_ALL} shopping carts")
        for cart_id in range(1, self.num_carts + 1):
            item_count = self.fake.random_int(min=1, max=4)
            items = []
            for _ in range(item_count):
                sku = f"sku-{self.fake.random_int(min=1, max=self.num_products):04d}"
                qty = self.fake.random_int(min=1, max=3)
                items.append({"sku": sku, "qty": qty})
                r.hincrby("inventory:reserved", sku, qty)
            r.hset(f"cart:{cart_id:04d}", mapping={
                "user_id": str(self.fake.random_int(min=1, max=self.num_users)),
                "items": json.dumps(items),
                "updated": self.fake.date_time_this_month().isoformat(),
            })
            r.zadd("cart:abandoned", {f"cart:{cart_id:04d}": self.fake.random_int(min=1, max=1000)})

        # Notifications and channels
        self.log(f"Streaming {Fore.YELLOW}{self.num_notifications}{Style.RESET_ALL} notifications")
        notification_types = ["deployment", "invoice", "alert", "comment", "handoff"]
        channels = ["email", "slack", "pagerduty", "sms"]
        for _ in range(self.num_notifications):
            user_id = self.fake.random_int(min=1, max=self.num_users)
            payload = {
                "type": self.fake.random_element(notification_types),
                "channel": self.fake.random_element(channels),
                "message": self.fake.sentence(nb_words=8),
                "created": self.fake.date_time_this_month().isoformat(),
            }
            stream_key = f"stream:notifications:{user_id:04d}"
            r.xadd(stream_key, payload)
            r.hincrby("notifications:unread", str(user_id), 1)
            r.sadd(f"user:{user_id}:channels", payload["channel"])

        # Audit events
        self.log(f"Appending {Fore.YELLOW}{self.num_audit_events}{Style.RESET_ALL} audit events")
        audit_actions = ["login", "logout", "plan_change", "role_update", "api_access", "mfa_challenge"]
        for _ in range(self.num_audit_events):
            event = {
                "user": self.fake.random_int(min=1, max=self.num_users),
                "action": self.fake.random_element(audit_actions),
                "ip": self.fake.ipv4_public(),
                "ts": int(time.time()) - self.fake.random_int(min=0, max=604800),
                "success": self.fake.boolean(chance_of_getting_true=92),
            }
            r.lpush("audit:trail", json.dumps(event))

        # API tokens and rate limits
        self.log(f"Issuing {Fore.YELLOW}{self.num_api_tokens}{Style.RESET_ALL} API tokens")
        for _ in range(self.num_api_tokens):
            user_id = self.fake.random_int(min=1, max=self.num_users)
            token = self.fake.uuid4().replace("-", "")
            ttl = self.fake.random_int(min=3600, max=3600 * 24 * 14)
            r.set(f"api:token:{token}", str(user_id), ex=ttl)
            r.sadd("api:tokens:index", token)

        self.log(f"Configuring {Fore.YELLOW}{self.num_rate_limits}{Style.RESET_ALL} rate limit counters")
        endpoints = ["/v1/metrics", "/v1/events", "/v1/users", "/v1/search", "/v1/tokens"]
        for _ in range(self.num_rate_limits):
            endpoint = self.fake.random_element(endpoints)
            user_id = self.fake.random_int(min=1, max=self.num_users)
            limit_key = f"ratelimit:{user_id}:{endpoint}"
            r.hset(limit_key, mapping={
                "minute": self.fake.random_int(min=0, max=120),
                "hour": self.fake.random_int(min=0, max=500),
                "day": self.fake.random_int(min=0, max=3000),
                "window_start": self.fake.date_time_this_month().isoformat(),
            })

        # Documents and dashboards
        self.log(f"Publishing {Fore.YELLOW}{self.num_documents}{Style.RESET_ALL} knowledge base docs")
        for doc_id in range(1, self.num_documents + 1):
            tags = self.fake.words(nb=3)
            body = "\n".join(self.fake.paragraphs(nb=50))
            doc_key = f"doc:{doc_id:04d}"
            r.set(doc_key, body)
            r.sadd("doc:index", doc_key)
            for tag in tags:
                r.sadd(f"doc:tag:{tag}", doc_key)

        self.log(f"Designing {Fore.YELLOW}{self.num_dashboards}{Style.RESET_ALL} dashboards")
        themes = ["nord", "gruvbox", "light", "solarized"]
        for dash_id in range(1, self.num_dashboards + 1):
            definition = {
                "widgets": self.fake.random_int(min=3, max=9),
                "theme": self.fake.random_element(themes),
                "layout": self.fake.random_element(["grid", "stack", "freeform"]),
                "refresh_interval": self.fake.random_int(min=10, max=120),
            }
            r.hset(f"dashboard:{dash_id:03d}", mapping={
                "owner_org": str(self.fake.random_int(min=1, max=self.num_orgs)),
                "definition": json.dumps(definition),
                "updated": self.fake.date_time_this_month().isoformat(),
            })

        # Webhooks and releases
        self.log(f"Scheduling {Fore.YELLOW}{self.num_webhooks}{Style.RESET_ALL} webhook deliveries")
        for hook_id in range(1, self.num_webhooks + 1):
            payload = {
                "delivery": f"hook-{hook_id:05d}",
                "status": self.fake.random_element(["pending", "sent", "errored"]),
                "ms": str(self.fake.random_int(min=35, max=1200)),
            }
            r.xadd("stream:webhooks", payload)
            r.zadd("webhooks:latency", {payload["delivery"]: float(payload["ms"])})

        self.log(f"Queuing {Fore.YELLOW}{self.num_releases}{Style.RESET_ALL} releases")
        environments = ["dev", "staging", "prod"]
        for release_id in range(1, self.num_releases + 1):
            release = {
                "id": f"rel-{release_id:04d}",
                "environment": self.fake.random_element(environments),
                "version": f"{self.fake.random_int(1, 3)}.{self.fake.random_int(0, 20)}.{self.fake.random_int(0, 99)}",
                "owner": self.fake.email(),
            }
            r.rpush("deploy:pipelines", json.dumps(release))
            r.xadd("streams:deployments", {
                "release": release["id"],
                "env": release["environment"],
                "status": self.fake.random_element(["queued", "running", "complete"]),
            })

        # Support tickets
        self.log(f"Tracking {Fore.YELLOW}{self.num_support_tickets}{Style.RESET_ALL} support tickets")
        ticket_statuses = ["new", "triaged", "in_progress", "waiting", "closed"]
        priorities = ["low", "medium", "high", "urgent"]
        for ticket_id in range(1, self.num_support_tickets + 1):
            status = self.fake.random_element(ticket_statuses)
            ticket_key = f"support:ticket:{ticket_id:05d}"
            r.hset(ticket_key, mapping={
                "subject": self.fake.sentence(nb_words=5),
                "status": status,
                "priority": self.fake.random_element(priorities),
                "requester": self.fake.email(),
                "created": self.fake.date_time_this_year().isoformat(),
            })
            if status != "closed":
                r.sadd("support:open", ticket_key)

        # Analytics and metrics
        self.log("Populating analytics artifacts")
        for _ in range(self.num_users * 3):
            r.pfadd("analytics:unique_visitors", self.fake.uuid4())
        for user_id in range(1, self.num_users + 1):
            if user_id % 4 == 0:
                r.setbit("analytics:active_bitmap", user_id, 1)
        for channel in channels:
            r.hset("analytics:channel_usage", channel, self.fake.random_int(min=10, max=500))

        # Geospatial warehouse index
        self.log("Indexing warehouse locations")
        warehouses = ["us-west", "us-east", "eu-north", "apac"]
        # Predefined coordinates for warehouses to ensure valid ranges
        warehouse_coords = {
            "us-west": (-122.4194, 37.7749),  # San Francisco
            "us-east": (-74.0060, 40.7128),   # New York
            "eu-north": (18.0686, 59.3293),   # Stockholm
            "apac": (151.2093, -33.8688),     # Sydney
        }
        for warehouse in warehouses:
            longitude, latitude = warehouse_coords.get(warehouse, (self.fake.longitude(), self.fake.latitude()))
            # Use execute_command to avoid nx/xx option conflicts in geoadd method
            r.execute_command("GEOADD", "geo:warehouses", longitude, latitude, warehouse)
            r.hset(f"warehouse:{warehouse}", mapping={
                "capacity": self.fake.random_int(min=50, max=500),
                "manager": self.fake.name(),
                "updated": self.fake.date_time_this_year().isoformat(),
            })

        self.log("Redis seeding complete", "success")

    def seed_memcached(self):
        """Populate Memcached with sample data."""
        self.print_header("MEMCACHED DATASTORE")

        self.log(f"Connecting to Memcached at {Fore.YELLOW}{self.memcached_host}:{self.memcached_port}{Style.RESET_ALL}")

        # Connect to Memcached
        try:
            sock = socket.create_connection(
                (self.memcached_host, self.memcached_port),
                timeout=5,
            )
            self.log("Connected successfully", "success")
        except (socket.error, socket.timeout) as e:
            self.log(f"Failed to connect: {e}", "error")
            raise

        try:
            # Flush all existing data (if requested)
            if self.flush:
                self.log("Flushing existing data", "warning")
                sock.sendall(b"flush_all\r\n")
                sock.recv(1024)
            else:
                self.log("Skipping flush (use --flush to clear existing data)")

            # Users with progress bar
            self.log(f"Creating {Fore.YELLOW}{self.num_memcached_users}{Style.RESET_ALL} users")
            bar_color = "\033[36m"  # Cyan
            for i in tqdm(
                range(1, self.num_memcached_users + 1),
                desc=f"{bar_color}Users{Style.RESET_ALL}",
                unit="user",
                bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]",
            ):
                segment = "enterprise" if i % 2 == 1 else "consumer"

                # Generate realistic user data
                user_data = {
                    "id": i,
                    "name": self.fake.name(),
                    "segment": segment,
                    "email": self.fake.email(),
                    "company": self.fake.company() if segment == "enterprise" else None,
                }

                # Remove None values
                user_data = {k: v for k, v in user_data.items() if v is not None}

                key = f"users:{i:03d}"
                self.memcached_set(sock, key, user_data, ttl=1800)

            # Jobs with progress bar
            self.log(f"Creating {Fore.YELLOW}{self.num_jobs}{Style.RESET_ALL} jobs")
            states = ["scheduled", "running", "finished"]
            for i in tqdm(
                range(1, self.num_jobs + 1),
                desc=f"{bar_color}Jobs{Style.RESET_ALL}",
                unit="job",
                bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}",
            ):
                state = states[i % 3]
                duration = 20 + i

                value = f"job-{i:02d}|state={state}|duration={duration}s"
                key = f"jobs:{i:02d}"
                self.memcached_set(sock, key, value, ttl=600)

            # Metrics
            self.memcached_set(sock, "metrics:ingest", "0", ttl=3600)
            self.memcached_set(sock, "metrics:alerts", "0", ttl=3600)

            # App config with realistic region
            regions = ["us-west-1", "us-west-2", "us-east-1", "eu-west-1", "ap-southeast-1"]
            envs = ["dev", "staging", "prod"]
            region = self.fake.random_element(regions)
            env = self.fake.random_element(envs)
            config_value = f"region={region}|env={env}"
            self.memcached_set(sock, "app:config", config_value, ttl=3600)

            # Session cache
            self.log(f"Caching {Fore.YELLOW}{self.num_users}{Style.RESET_ALL} session artifacts")
            for i in tqdm(
                range(1, self.num_users + 1),
                desc=f"{bar_color}Sessions{Style.RESET_ALL}",
                unit="session",
                bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}",
            ):
                session = {
                    "user_id": i,
                    "client": self.fake.random_element(["web", "mobile", "cli"]),
                    "ip": self.fake.ipv4_public(),
                    "token": self.fake.uuid4(),
                    "expires_at": self.fake.date_time_this_month().isoformat(),
                }
                ttl = self.fake.random_int(min=900, max=7200)
                self.memcached_set(sock, f"cache:session:{i:04d}", session, ttl=ttl)

            # Experiment assignments
            self.log("Storing experiment assignments")
            experiments = ["onboarding_flow", "search_v2", "beta_nav", "billing_banner"]
            for idx in range(1, self.num_users + 1):
                assignment = {exp: self.fake.random_element(["control", "variant"]) for exp in experiments}
                self.memcached_set(sock, f"cache:experiments:{idx:04d}", assignment, ttl=14400)

            # Notification digests
            self.log("Creating notification digests")
            for idx in range(1, self.num_notifications + 1):
                digest = {
                    "count": self.fake.random_int(min=1, max=12),
                    "channels": self.fake.random_elements(elements=["email", "slack", "sms"], length=2, unique=False),
                    "generated": self.fake.date_time_this_month().isoformat(),
                }
                self.memcached_set(sock, f"cache:notifications:{idx:04d}", digest, ttl=3600)

            # Inventory snapshots
            self.log("Caching inventory snapshots")
            for product_id in range(1, self.num_products + 1):
                snapshot = {
                    "sku": f"sku-{product_id:04d}",
                    "stock": self.fake.random_int(min=0, max=250),
                    "reserved": self.fake.random_int(min=0, max=25),
                    "updated": self.fake.date_time_this_month().isoformat(),
                }
                self.memcached_set(sock, f"cache:inventory:{product_id:04d}", snapshot, ttl=300)

            # Dashboard renders
            self.log("Preparing dashboard render cache")
            for dash_id in range(1, self.num_dashboards + 1):
                render = {
                    "dashboard_id": dash_id,
                    "widgets": [self.fake.word() for _ in range(self.fake.random_int(min=3, max=7))],
                    "generated": self.fake.date_time_this_month().isoformat(),
                }
                self.memcached_set(sock, f"cache:dashboard:{dash_id:03d}", render, ttl=600)

            # Org usage reports
            self.log("Caching org usage reports")
            for org_id in range(1, self.num_orgs + 1):
                report = {
                    "org_id": org_id,
                    "events": self.fake.random_int(min=1000, max=50000),
                    "errors": self.fake.random_int(min=0, max=500),
                    "spend": round(self.fake.pyfloat(left_digits=4, right_digits=2, positive=True, min_value=120, max_value=9000), 2),
                }
                self.memcached_set(sock, f"cache:org_report:{org_id:03d}", report, ttl=1800)

            # Feature flag snapshots
            self.log("Caching feature flag snapshots")
            flag_variants = ["enabled", "disabled", "beta"]
            for flag in ["dark_mode", "live_reload", "new_console", "ai_assist"]:
                payload = {
                    "flag": flag,
                    "value": self.fake.random_element(flag_variants),
                    "updated": self.fake.date_time_this_month().isoformat(),
                }
                self.memcached_set(sock, f"cache:feature:{flag}", payload, ttl=120)

            # Search suggestions
            self.log("Seeding search suggestions")
            keywords = ["error", "latency", "timeout", "deployment", "billing"]
            for keyword in keywords:
                suggestions = [self.fake.word() for _ in range(5)]
                self.memcached_set(sock, f"cache:search:{keyword}", "|".join(suggestions), ttl=900)

            # Pricing cache
            self.log("Caching pricing tiers")
            tiers = ["free", "pro", "enterprise", "scale", "edge"]
            for tier in tiers:
                price = round(self.fake.pyfloat(left_digits=3, right_digits=2, positive=True, min_value=1, max_value=250), 2)
                details = f"tier={tier}|price={price}|currency=USD"
                self.memcached_set(sock, f"pricing:{tier}", details, ttl=7200)

            # Report exports
            self.log("Caching report exports")
            for idx in range(1, self.num_jobs + 5):
                export = {
                    "job": f"export-{idx:04d}",
                    "owner": self.fake.email(),
                    "format": self.fake.random_element(["csv", "json", "parquet"]),
                    "rows": self.fake.random_int(min=100, max=5000),
                }
                self.memcached_set(sock, f"cache:export:{idx:04d}", export, ttl=5400)

            # Quit
            sock.sendall(b"quit\r\n")

            self.log("Memcached seeding complete", "success")
        finally:
            sock.close()

    def etcd_put(self, client, key: str, value):
        """Helper to put a value in etcd."""
        if not isinstance(value, str):
            value = json.dumps(value)
        client.put(key, value)

    def seed_etcd(self):
        """Populate etcd with sample data using etcd3gw client."""
        self.print_header("ETCD DATASTORE")

        endpoint = f"http://{self.etcd_host}:{self.etcd_port}"
        self.log(f"Connecting to etcd at {Fore.YELLOW}{endpoint}{Style.RESET_ALL}")

        # Connect to etcd
        try:
            from etcd3gw.client import Etcd3Client
            client = Etcd3Client(host=self.etcd_host, port=self.etcd_port, timeout=5)
            # Test connection
            try:
                client.status()
            except (AttributeError, Exception):
                # Some versions might not have status(), try a simple operation
                pass
            self.log("Connected successfully", "success")
        except Exception as e:
            self.log(f"Failed to connect: {e}", "error")
            raise

        # Flush all existing data (if requested)
        if self.flush:
            self.log("Flushing existing data", "warning")
            try:
                # Get all keys and delete them
                # etcd3gw uses get_prefix to get all keys with a prefix
                all_keys = client.get_prefix('')
                for key, value, metadata in all_keys:
                    if key:
                        key_str = key.decode('utf-8') if isinstance(key, bytes) else key
                        client.delete(key_str)
            except Exception as e:
                self.log(f"Warning: Error during flush: {e}", "warning")
        else:
            self.log("Skipping flush (use --flush to clear existing data)")

        bar_color = "\033[36m"  # Cyan

        # Service discovery entries
        self.log(f"Registering {Fore.YELLOW}{self.num_etcd_services}{Style.RESET_ALL} services")
        services = ["api-gateway", "auth-service", "user-service", "order-service",
                    "payment-service", "notification-service", "inventory-service",
                    "search-service", "analytics-service", "cache-service"]
        regions = ["us-west-1", "us-east-1", "eu-central-1", "ap-southeast-1"]
        statuses = ["healthy", "degraded", "maintenance"]

        for i in tqdm(
            range(1, self.num_etcd_services + 1),
            desc=f"{bar_color}Services{Style.RESET_ALL}",
            unit="svc",
            bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]",
        ):
            service_name = self.fake.random_element(services)
            instance_id = f"{service_name}-{i:03d}"
            region = self.fake.random_element(regions)

            # Service registration
            service_data = {
                "id": instance_id,
                "name": service_name,
                "address": f"{service_name}-{i}.{region}.svc.cluster.local",
                "port": self.fake.random_int(min=8000, max=9000),
                "region": region,
                "status": self.fake.random_element(statuses),
                "version": f"{self.fake.random_int(1, 3)}.{self.fake.random_int(0, 15)}.{self.fake.random_int(0, 99)}",
                "started_at": self.fake.date_time_this_month().isoformat(),
                "metadata": {
                    "cpu_cores": self.fake.random_int(min=2, max=16),
                    "memory_mb": self.fake.random_int(min=512, max=8192),
                    "environment": self.fake.random_element(["dev", "staging", "prod"]),
                },
            }
            key = f"/services/{service_name}/{region}/{instance_id}"
            self.etcd_put(client, key, service_data)

            # Health check entry
            health_data = {
                "status": self.fake.random_element(["passing", "warning", "critical"]),
                "last_check": self.fake.date_time_this_month().isoformat(),
                "check_interval": "10s",
            }
            self.etcd_put(client, f"/health/{instance_id}", health_data)

        # Configuration entries
        self.log(f"Creating {Fore.YELLOW}{self.num_etcd_configs}{Style.RESET_ALL} configuration entries")
        environments = ["dev", "staging", "prod"]
        config_types = ["database", "cache", "messaging", "logging", "security", "feature-flags"]

        for i in tqdm(
            range(1, self.num_etcd_configs + 1),
            desc=f"{bar_color}Configs{Style.RESET_ALL}",
            unit="cfg",
            bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}",
        ):
            env = self.fake.random_element(environments)
            config_type = self.fake.random_element(config_types)

            if config_type == "database":
                config_data = {
                    "host": f"db-{env}.internal",
                    "port": 5432,
                    "database": f"app_{env}",
                    "pool_size": self.fake.random_int(min=5, max=50),
                    "ssl": env == "prod",
                    "timeout_ms": self.fake.random_int(min=1000, max=5000),
                }
            elif config_type == "cache":
                config_data = {
                    "host": f"redis-{env}.internal",
                    "port": 6379,
                    "ttl_seconds": self.fake.random_int(min=300, max=3600),
                    "max_connections": self.fake.random_int(min=10, max=100),
                }
            elif config_type == "messaging":
                config_data = {
                    "broker": f"kafka-{env}.internal:9092",
                    "consumer_group": f"app-{env}",
                    "topics": [self.fake.word() for _ in range(3)],
                    "batch_size": self.fake.random_int(min=100, max=1000),
                }
            elif config_type == "logging":
                config_data = {
                    "level": self.fake.random_element(["debug", "info", "warn", "error"]),
                    "format": self.fake.random_element(["json", "text"]),
                    "output": self.fake.random_element(["stdout", "file", "remote"]),
                    "retention_days": self.fake.random_int(min=7, max=90),
                }
            elif config_type == "security":
                config_data = {
                    "jwt_expiry_hours": self.fake.random_int(min=1, max=24),
                    "rate_limit_per_minute": self.fake.random_int(min=60, max=1000),
                    "allowed_origins": [self.fake.domain_name() for _ in range(3)],
                    "enable_cors": True,
                }
            else:  # feature-flags
                config_data = {
                    "dark_mode": self.fake.boolean(),
                    "new_dashboard": self.fake.boolean(),
                    "beta_features": self.fake.boolean(),
                    "ai_assistant": self.fake.boolean(),
                    "rollout_percentage": self.fake.random_int(min=0, max=100),
                }

            key = f"/config/{env}/{config_type}"
            self.etcd_put(client, key, config_data)

        # Cluster membership
        self.log("Recording cluster membership")
        nodes = ["node-1", "node-2", "node-3", "node-4", "node-5"]
        for node in nodes:
            node_data = {
                "id": node,
                "ip": self.fake.ipv4_private(),
                "role": self.fake.random_element(["leader", "follower"]),
                "joined_at": self.fake.date_time_this_year().isoformat(),
                "last_heartbeat": self.fake.date_time_this_month().isoformat(),
            }
            self.etcd_put(client, f"/cluster/nodes/{node}", node_data)

        # Locks and leases (simulated)
        self.log("Creating distributed locks")
        lock_owners = ["scheduler", "replicator", "cleaner", "indexer", "aggregator"]
        for owner in lock_owners:
            lock_data = {
                "owner": owner,
                "acquired_at": self.fake.date_time_this_month().isoformat(),
                "ttl_seconds": self.fake.random_int(min=10, max=60),
            }
            self.etcd_put(client, f"/locks/{owner}", lock_data)

        # Leader election entries
        self.log("Setting up leader election")
        leader_services = ["job-scheduler", "event-processor", "cache-manager"]
        for service in leader_services:
            leader_data = {
                "leader_id": f"{service}-{self.fake.random_int(1, 5)}",
                "elected_at": self.fake.date_time_this_month().isoformat(),
                "term": self.fake.random_int(min=1, max=100),
            }
            self.etcd_put(client, f"/election/{service}/leader", leader_data)

        # Rate limiting counters
        self.log("Initializing rate limit counters")
        for i in range(1, min(self.num_users, 50) + 1):
            user_id = f"user-{i:04d}"
            rate_data = {
                "requests_minute": self.fake.random_int(min=0, max=100),
                "requests_hour": self.fake.random_int(min=0, max=500),
                "last_request": self.fake.date_time_this_month().isoformat(),
            }
            self.etcd_put(client, f"/ratelimit/{user_id}", rate_data)

        # Secret references (not actual secrets, just metadata)
        self.log("Storing secret references")
        secret_types = ["api-key", "database-password", "jwt-secret", "oauth-client"]
        for secret_type in secret_types:
            for env in environments:
                secret_meta = {
                    "type": secret_type,
                    "environment": env,
                    "version": self.fake.random_int(min=1, max=10),
                    "rotated_at": self.fake.date_time_this_year().isoformat(),
                    "vault_path": f"secret/data/{env}/{secret_type}",
                }
                self.etcd_put(client, f"/secrets/{env}/{secret_type}", secret_meta)

        # Workflow state
        self.log("Storing workflow states")
        workflow_types = ["deploy", "backup", "migration", "cleanup"]
        for i in range(1, self.num_releases + 1):
            workflow = {
                "id": f"wf-{i:05d}",
                "type": self.fake.random_element(workflow_types),
                "status": self.fake.random_element(["pending", "running", "completed", "failed"]),
                "started_at": self.fake.date_time_this_month().isoformat(),
                "steps_completed": self.fake.random_int(min=0, max=10),
                "total_steps": 10,
            }
            self.etcd_put(client, f"/workflows/{workflow['id']}", workflow)

        # Tenant isolation
        self.log("Creating tenant configurations")
        for org_id in range(1, self.num_orgs + 1):
            tenant = {
                "id": f"tenant-{org_id:03d}",
                "name": self.fake.company(),
                "tier": self.fake.random_element(["free", "pro", "enterprise"]),
                "features": {
                    "max_users": self.fake.random_int(min=5, max=500),
                    "max_storage_gb": self.fake.random_int(min=10, max=1000),
                    "api_rate_limit": self.fake.random_int(min=100, max=10000),
                },
                "created_at": self.fake.date_time_this_year().isoformat(),
            }
            self.etcd_put(client, f"/tenants/{tenant['id']}", tenant)

        # etcd3gw client doesn't need explicit close, but we can clean up if needed
        self.log("etcd seeding complete", "success")

    def run(self):
        """Run the full seeding process."""
        try:
            # Print welcome banner
            print()
            print(f"{Fore.CYAN}╔═══════════════════════════════════════════════════════════╗")
            print(f"{Fore.CYAN}║           DATASTORE SEEDING UTILITY                       ║")
            print(f"{Fore.CYAN}╚═══════════════════════════════════════════════════════════╝{Style.RESET_ALL}")
            print()

            # Show configuration
            self.log(f"Scale factor: {Fore.YELLOW}{self.scale}x{Style.RESET_ALL}")
            if self.scale != 1.0:
                self.log(f"  Redis users: {Fore.YELLOW}{self.num_users}{Style.RESET_ALL} (base: {self.BASE_REDIS_USERS})")
                self.log(f"  Memcached users: {Fore.YELLOW}{self.num_memcached_users}{Style.RESET_ALL} (base: {self.BASE_MEMCACHED_USERS})")
                self.log(f"  etcd services: {Fore.YELLOW}{self.num_etcd_services}{Style.RESET_ALL} (base: {self.BASE_ETCD_SERVICES})")
                self.log(f"  Jobs: {Fore.YELLOW}{self.num_jobs}{Style.RESET_ALL} (base: {self.BASE_JOBS})")
            print()

            # Seed all datastores
            self.seed_redis()
            self.seed_memcached()
            self.seed_etcd()

            # Print success summary
            print()
            print(f"{Fore.GREEN}╔═══════════════════════════════════════════════════════════╗")
            print(f"{Fore.GREEN}║                    SEEDING COMPLETE                       ║")
            print(f"{Fore.GREEN}╚═══════════════════════════════════════════════════════════╝{Style.RESET_ALL}")
            print()
            self.log(f"Total Redis keys: {Fore.YELLOW}~{self.num_users * 2 + 50}{Style.RESET_ALL}")
            self.log(f"Total Memcached keys: {Fore.YELLOW}~{self.num_memcached_users + self.num_jobs + 3}{Style.RESET_ALL}")
            self.log(f"Total etcd keys: {Fore.YELLOW}~{self.num_etcd_services * 2 + self.num_etcd_configs + 100}{Style.RESET_ALL}")
            print()

        except KeyboardInterrupt:
            print()
            self.log("Interrupted by user", "warning")
            sys.exit(130)
        except Exception as e:
            print()
            self.log(f"Error: {e}", "error")
            if self.verbose:
                raise
            sys.exit(1)


def main():
    """Main entry point."""
    parser = argparse.ArgumentParser(
        description="Seed Redis, Memcached, and etcd datastores with sample data",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument(
        "--redis-host",
        default="127.0.0.1",
        help="Redis host address",
    )
    parser.add_argument(
        "--redis-port",
        type=int,
        default=6379,
        help="Redis port",
    )
    parser.add_argument(
        "--memcached-host",
        default="127.0.0.1",
        help="Memcached host address",
    )
    parser.add_argument(
        "--memcached-port",
        type=int,
        default=11211,
        help="Memcached port",
    )
    parser.add_argument(
        "--etcd-host",
        default="127.0.0.1",
        help="etcd host address",
    )
    parser.add_argument(
        "--etcd-port",
        type=int,
        default=2379,
        help="etcd port",
    )
    parser.add_argument(
        "--scale",
        type=float,
        default=1.0,
        help=(
            "Scale multiplier for data generation (default: 1.0). "
            f"Base amounts: {DatastoreSeeder.BASE_REDIS_USERS} Redis users, "
            f"{DatastoreSeeder.BASE_MEMCACHED_USERS} Memcached users, "
            f"{DatastoreSeeder.BASE_ETCD_SERVICES} etcd services, "
            f"{DatastoreSeeder.BASE_JOBS} jobs. "
            "Use 0.5 for half, 2.0 for double, etc."
        ),
    )
    parser.add_argument(
        "--seed",
        type=int,
        help="Random seed for reproducible data generation",
    )
    parser.add_argument(
        "--flush",
        action="store_true",
        help="Flush all existing data before seeding (WARNING: destructive operation)",
    )
    parser.add_argument(
        "-v", "--verbose",
        action="store_true",
        help="Enable verbose output",
    )

    args = parser.parse_args()

    seeder = DatastoreSeeder(
        redis_host=args.redis_host,
        redis_port=args.redis_port,
        memcached_host=args.memcached_host,
        memcached_port=args.memcached_port,
        etcd_host=args.etcd_host,
        etcd_port=args.etcd_port,
        scale=args.scale,
        seed=args.seed,
        flush=args.flush,
        verbose=args.verbose,
    )

    seeder.run()


if __name__ == "__main__":
    main()