Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-25 08:29:08

0001 #!/usr/bin/env python3
0002 """
0003 Script to load fake AppLog data into the swf-monitor database for UI/demo purposes.
0004 Adds a few entries for each app, instance, and log level to exercise the log summary and drill-down views.
0005 
0006 Usage:
0007     python scripts/load_fake_logs.py
0008 
0009 Notes:
0010 - Timestamps are timezone-aware (Django's timezone.now()).
0011 - Run only in a virtual environment. Script will exit if not.
0012 - To avoid accidental duplicate logs, you can clear AppLog table before running (see code comments).
0013 """
0014 import os
0015 import sys
0016 import django
0017 import random
0018 from datetime import timedelta
0019 from django.utils import timezone
0020 
0021 # Robustness: Check for active virtual environment
0022 if sys.prefix == sys.base_prefix:
0023     print("\n[ERROR] This script should be run inside your project's Python virtual environment.")
0024     print("Activate your venv with:")
0025     print("  source ../swf-testbed/.venv/bin/activate   # (bash/zsh, macOS/Linux)")
0026     print("  ..\\swf-testbed\\.venv\\Scripts\\activate   # (Windows cmd)")
0027     print("Then run:\n  python scripts/load_fake_logs.py\n")
0028     sys.exit(1)
0029 
0030 # Setup Django environment
0031 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'swf_monitor_project.settings')
0032 django.setup()
0033 
0034 from monitor_app.models import AppLog
0035 
0036 APPS = ['app1', 'app2', 'app3']
0037 INSTANCES = ['inst1', 'inst2', 'inst3']
0038 LEVELS = [
0039     (10, 'DEBUG'),
0040     (20, 'INFO'),
0041     (30, 'WARNING'),
0042     (40, 'ERROR'),
0043     (50, 'CRITICAL'),
0044 ]
0045 
0046 now = timezone.now()
0047 
0048 # Uncomment the following lines to clear all AppLog entries before loading fake logs:
0049 # print("[INFO] Deleting all existing AppLog entries...")
0050 # AppLog.objects.all().delete()
0051 
0052 created = 0
0053 for app in APPS:
0054     for inst in INSTANCES:
0055         for level, level_name in LEVELS:
0056             for i in range(2):  # 2 logs per combination
0057                 # Add a long message for one of the logs
0058                 if i == 1 and level_name == 'INFO' and app == 'app1' and inst == 'inst1':
0059                     long_msg = (
0060                         f"Message {i+1} for {app}/{inst} - "
0061                         "This is a very long log message intended to test word wrapping in the UI. "
0062                         "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
0063                         "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
0064                         "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
0065                         "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
0066                     )
0067                     message = long_msg
0068                 else:
0069                     message = f"Message {i+1} for {app}/{inst}"
0070                 log = AppLog(
0071                     app_name=app,
0072                     instance_name=inst,
0073                     timestamp=now - timedelta(minutes=random.randint(0, 120)),
0074                     level=level,
0075                     levelname=level_name,
0076                     message=message,
0077                     module="demo_module",
0078                     funcname="demo_func",
0079                     lineno=random.randint(1, 100),
0080                     process=random.randint(1000, 2000),
0081                     thread=random.randint(1, 10),
0082                 )
0083                 log.save()
0084                 print(f"Created log: {log.app_name} {log.instance_name} {log.levelname} {log.message}")
0085                 created += 1
0086 
0087 print(f"Fake logs loaded. Total created: {created}")