|
| 1 | +import random |
| 2 | +import sys |
| 3 | +import time |
| 4 | +import uuid |
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | +from typing import Any, Callable, Dict, List, Optional |
| 7 | + |
| 8 | + |
| 9 | +class JSONGenerator: |
| 10 | + """ |
| 11 | + Utility class to generate and update reproducible JSON documents for testing. |
| 12 | +
|
| 13 | + Usage: |
| 14 | + gen = JSONGenerator(size=1000, format="json") |
| 15 | + docs = gen.generate_all_documents() |
| 16 | + updated_docs = gen.update_all_documents(docs) |
| 17 | +
|
| 18 | + Parameters: |
| 19 | + seed (int, optional): Random seed for reproducibility (default: random int). |
| 20 | + size (int, optional): Number of documents to generate (default: 60000). |
| 21 | + format (str, optional): Output format - "json" (dict) or "key-value" (list of dicts/documents). |
| 22 | + To insert/update in CB-server/SGW/ Edge-server : use format "json". |
| 23 | + To insert into test-server use format "key-value" . |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + seed: int = random.randint(0, sys.maxsize), |
| 29 | + size: int = 60000, |
| 30 | + format: str = "json", |
| 31 | + ): |
| 32 | + self.seed = seed |
| 33 | + self.size = size |
| 34 | + self.format = format |
| 35 | + |
| 36 | + def generate_document(self, doc_id: str) -> Dict[str, Any]: |
| 37 | + """Generate a single JSON document with reproducible random data""" |
| 38 | + random.seed(self.seed + int(doc_id.split("-")[0], 16)) |
| 39 | + if self.format == "json": |
| 40 | + return { |
| 41 | + doc_id: { |
| 42 | + "data": { |
| 43 | + "temperature": random.uniform(-20, 40), |
| 44 | + "humidity": random.randint(0, 100), |
| 45 | + "status": random.choice(["active", "inactive", "maintenance"]), |
| 46 | + }, |
| 47 | + "metadata": { |
| 48 | + "version": 1, |
| 49 | + "created_at": int(time.time()), |
| 50 | + "modified_at": int(time.time()), |
| 51 | + }, |
| 52 | + } |
| 53 | + } |
| 54 | + else: |
| 55 | + return { |
| 56 | + doc_id: [ |
| 57 | + { |
| 58 | + "data": { |
| 59 | + "temperature": random.uniform(-20, 40), |
| 60 | + "humidity": random.randint(0, 100), |
| 61 | + "status": random.choice( |
| 62 | + ["active", "inactive", "maintenance"] |
| 63 | + ), |
| 64 | + } |
| 65 | + }, |
| 66 | + { |
| 67 | + "metadata": { |
| 68 | + "version": 1, |
| 69 | + "created_at": int(time.time()), |
| 70 | + "modified_at": int(time.time()), |
| 71 | + } |
| 72 | + }, |
| 73 | + ] |
| 74 | + } |
| 75 | + |
| 76 | + def update_document(self, doc: Any, doc_id: str) -> Dict[str, Any]: |
| 77 | + """Update a document with reproducible modifications""" |
| 78 | + offset = int(doc_id.split("-")[0], 16) |
| 79 | + random.seed(self.seed + offset) |
| 80 | + if self.format == "json": |
| 81 | + doc["data"]["temperature"] += random.uniform(-5, 5) |
| 82 | + doc["data"]["humidity"] = ( |
| 83 | + doc["data"]["humidity"] + random.randint(-10, 10) |
| 84 | + ) % 100 |
| 85 | + doc["data"]["status"] = random.choice(["active", "inactive", "maintenance"]) |
| 86 | + doc["metadata"]["version"] = doc["metadata"]["version"] + 1 |
| 87 | + doc["metadata"]["modified_at"] = int(time.time()) |
| 88 | + |
| 89 | + else: |
| 90 | + doc[0]["data"] = { |
| 91 | + "temperature": random.uniform(-20, 40), |
| 92 | + "humidity": random.randint(0, 100), |
| 93 | + "status": random.choice(["active", "inactive", "maintenance"]), |
| 94 | + } |
| 95 | + doc[1]["metadata"]["version"] = doc[1]["metadata"]["version"] + 1 |
| 96 | + doc[1]["metadata"]["modified_at"] = int(time.time()) |
| 97 | + return {doc_id: doc} |
| 98 | + |
| 99 | + def batch_process( |
| 100 | + self, |
| 101 | + process_fn: Callable, |
| 102 | + items_ids: List[Any], |
| 103 | + items_doc: Optional[Dict[str, Any]] = None, |
| 104 | + batch_size: int = 1000, |
| 105 | + ) -> Dict[Any, Any]: |
| 106 | + """Generic batch processing function with threading""" |
| 107 | + results = {} |
| 108 | + |
| 109 | + def process_batch(batch): |
| 110 | + result = {} |
| 111 | + for item in batch: |
| 112 | + output = ( |
| 113 | + process_fn(items_doc[item], item) |
| 114 | + if items_doc is not None |
| 115 | + else process_fn(item) |
| 116 | + ) |
| 117 | + result.update(output) |
| 118 | + return result |
| 119 | + |
| 120 | + with ThreadPoolExecutor() as executor: |
| 121 | + futures = [ |
| 122 | + executor.submit(process_batch, items_ids[i : i + batch_size]) |
| 123 | + for i in range(0, len(items_ids), batch_size) |
| 124 | + ] |
| 125 | + for future in futures: |
| 126 | + results.update(future.result()) |
| 127 | + |
| 128 | + return results |
| 129 | + |
| 130 | + def generate_all_documents(self, size=None) -> Dict[str, Any]: |
| 131 | + """Generate all documents using parallel processing""" |
| 132 | + if size is None: |
| 133 | + size = self.size |
| 134 | + |
| 135 | + doc_ids = [str(uuid.uuid4()) for _ in range(size)] |
| 136 | + documents = self.batch_process(self.generate_document, doc_ids) |
| 137 | + return documents |
| 138 | + |
| 139 | + def update_all_documents( |
| 140 | + self, documents: Dict[str, Any] |
| 141 | + ) -> Dict[str, Dict[str, Any]]: |
| 142 | + """Update all documents with consistent modifications""" |
| 143 | + |
| 144 | + doc_ids = list(documents.keys()) |
| 145 | + updated = self.batch_process(self.update_document, doc_ids, documents) |
| 146 | + return updated |
0 commit comments