|
1 | | -#!/usr/bin/env python3 |
2 | 1 | # ruff: noqa: T201 |
3 | | -# /// script |
4 | | -# requires-python = ">=3.12" |
5 | | -# dependencies = [ |
6 | | -# "click", |
7 | | -# ] |
8 | | -# /// |
9 | | - |
10 | 2 | """ |
11 | 3 | This script compares CrateDB cluster settings against default values. |
12 | 4 | It parses a JSON file containing cluster settings and checks for differences |
@@ -218,9 +210,12 @@ def compare_memory_settings( |
218 | 210 | current_bytes = to_bytes(str(current_value), heap_size_bytes) |
219 | 211 | default_bytes = to_bytes(default_value, heap_size_bytes) |
220 | 212 |
|
221 | | - if not current_bytes or not default_bytes: |
| 213 | + if current_bytes is None or default_bytes is None: |
222 | 214 | return None |
223 | 215 |
|
| 216 | + if not heap_size_bytes: |
| 217 | + raise ValueError("Heap size must be provided to compare memory settings.") |
| 218 | + |
224 | 219 | # Calculate percentage of heap |
225 | 220 | current_percent = (current_bytes / heap_size_bytes) * 100 |
226 | 221 | default_percent = (default_bytes / heap_size_bytes) * 100 |
@@ -263,6 +258,68 @@ def find_cluster_settings(json_objects): |
263 | 258 | return None |
264 | 259 |
|
265 | 260 |
|
| 261 | +HEADER = "\033[95m" |
| 262 | +BOLD = "\033[1m" |
| 263 | +GREEN = "\033[92m" |
| 264 | +YELLOW = "\033[93m" |
| 265 | +RED = "\033[91m" |
| 266 | +BLUE = "\033[94m" |
| 267 | +RESET = "\033[0m" |
| 268 | +PURPLE = "\033[95m" # Define purple color |
| 269 | + |
| 270 | + |
| 271 | +def report_comparison(default_settings, non_default_settings): |
| 272 | + # Group settings by category |
| 273 | + categorized_settings = defaultdict(list) |
| 274 | + |
| 275 | + for setting in sorted(non_default_settings): |
| 276 | + # Extract the top-level category from the setting key |
| 277 | + category = setting.split(":", 1)[0].split(".")[0] |
| 278 | + categorized_settings[category].append(setting) |
| 279 | + |
| 280 | + # Print settings by category |
| 281 | + if categorized_settings: |
| 282 | + for category, settings in sorted(categorized_settings.items()): |
| 283 | + print(f"{BOLD}{GREEN}{category.upper()}{RESET}") |
| 284 | + print(f"{GREEN}{'=' * len(category)}{RESET}") |
| 285 | + |
| 286 | + # Print each setting in the category without blank lines between them |
| 287 | + for setting in settings: |
| 288 | + # Split into parts for colored output |
| 289 | + key_part, value_parts = setting.split(":", 1) |
| 290 | + |
| 291 | + # Format in a single line with appropriate wrapping |
| 292 | + full_line = f"{BOLD}{key_part}:{RESET} {value_parts.strip()}" |
| 293 | + |
| 294 | + # Add yellow color to the default value part |
| 295 | + full_line = full_line.replace(" (default: ", f" (default: {YELLOW}").replace(")", f"{RESET})") |
| 296 | + |
| 297 | + # Wrap long lines, preserving the setting name at the start of the first line |
| 298 | + wrapped_text = textwrap.fill( |
| 299 | + full_line, |
| 300 | + width=120, # Increased width |
| 301 | + subsequent_indent=" ", |
| 302 | + break_on_hyphens=False, |
| 303 | + break_long_words=False, # Prevent breaking words like large byte counts |
| 304 | + ) |
| 305 | + |
| 306 | + # Print the wrapped text for the setting |
| 307 | + print(wrapped_text) |
| 308 | + |
| 309 | + # Add the statement in purple as a separate line if available |
| 310 | + setting_key_clean = key_part.strip() |
| 311 | + if setting_key_clean in default_settings and "stmt" in default_settings[setting_key_clean]: |
| 312 | + stmt = default_settings[setting_key_clean]["stmt"] |
| 313 | + print(f" {PURPLE}{stmt}{RESET}") |
| 314 | + |
| 315 | + # Add blank line after each category |
| 316 | + print() |
| 317 | + else: |
| 318 | + print(f"{GREEN}No non-default settings found.{RESET}") |
| 319 | + |
| 320 | + print(f"\n{BOLD}Total non-default settings: {len(non_default_settings)}{RESET}") |
| 321 | + |
| 322 | + |
266 | 323 | @click.command() |
267 | 324 | @click.argument("cluster_file", type=click.Path(exists=True)) |
268 | 325 | @click.argument("heap_size_bytes", type=int, required=False) |
@@ -316,14 +373,6 @@ def compare_cluster_settings( |
316 | 373 | RED = "" |
317 | 374 | BLUE = "" |
318 | 375 | RESET = "" |
319 | | - else: |
320 | | - HEADER = "\033[95m" |
321 | | - BOLD = "\033[1m" |
322 | | - GREEN = "\033[92m" |
323 | | - YELLOW = "\033[93m" |
324 | | - RED = "\033[91m" |
325 | | - BLUE = "\033[94m" |
326 | | - RESET = "\033[0m" |
327 | 376 |
|
328 | 377 | print(f"{BOLD}Comparing settings in {BLUE}{cluster_file}{RESET}{BOLD} against defaults...{RESET}") |
329 | 378 | if heap_size_bytes: |
@@ -417,57 +466,4 @@ def compare_cluster_settings( |
417 | 466 | if result: |
418 | 467 | non_default_settings.add(result) # Using add() for a set instead of append() |
419 | 468 |
|
420 | | - # Group settings by category |
421 | | - categorized_settings = defaultdict(list) |
422 | | - |
423 | | - for setting in sorted(non_default_settings): |
424 | | - # Extract the top-level category from the setting key |
425 | | - category = setting.split(":", 1)[0].split(".")[0] |
426 | | - categorized_settings[category].append(setting) |
427 | | - |
428 | | - # Print settings by category |
429 | | - if categorized_settings: |
430 | | - for category, settings in sorted(categorized_settings.items()): |
431 | | - print(f"{BOLD}{GREEN}{category.upper()}{RESET}") |
432 | | - print(f"{GREEN}{'=' * len(category)}{RESET}") |
433 | | - |
434 | | - # Print each setting in the category without blank lines between them |
435 | | - for setting in settings: |
436 | | - # Split into parts for colored output |
437 | | - key_part, value_parts = setting.split(":", 1) |
438 | | - |
439 | | - # Format in a single line with appropriate wrapping |
440 | | - full_line = f"{BOLD}{key_part}:{RESET} {value_parts.strip()}" |
441 | | - |
442 | | - # Add yellow color to the default value part |
443 | | - full_line = full_line.replace(" (default: ", f" (default: {YELLOW}").replace(")", f"{RESET})") |
444 | | - |
445 | | - # Wrap long lines, preserving the setting name at the start of the first line |
446 | | - wrapped_text = textwrap.fill( |
447 | | - full_line, |
448 | | - width=120, # Increased width |
449 | | - subsequent_indent=" ", |
450 | | - break_on_hyphens=False, |
451 | | - break_long_words=False, # Prevent breaking words like large byte counts |
452 | | - ) |
453 | | - |
454 | | - # Print the wrapped text for the setting |
455 | | - print(wrapped_text) |
456 | | - |
457 | | - # Add the statement in purple as a separate line if available |
458 | | - setting_key_clean = key_part.strip() |
459 | | - if setting_key_clean in default_settings and "stmt" in default_settings[setting_key_clean]: |
460 | | - stmt = default_settings[setting_key_clean]["stmt"] |
461 | | - PURPLE = "\033[95m" # Define purple color |
462 | | - print(f" {PURPLE}{stmt}{RESET}") |
463 | | - |
464 | | - # Add blank line after each category |
465 | | - print() |
466 | | - else: |
467 | | - print(f"{GREEN}No non-default settings found.{RESET}") |
468 | | - |
469 | | - print(f"\n{BOLD}Total non-default settings: {len(non_default_settings)}{RESET}") |
470 | | - |
471 | | - |
472 | | -if __name__ == "__main__": |
473 | | - compare_cluster_settings() # Invoke Click command |
| 469 | + report_comparison(default_settings, non_default_settings) |
0 commit comments