reporter_demo.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Reporter demo script.
  2. This script demonstrates how to use DailyFileReporter with UTC+8 timezone
  3. and year/month archived log directories. It simulates a short run with
  4. start/success/error/summary notifications.
  5. Usage (PowerShell):
  6. pwsh -NoProfile -File ./scripts/reporter_demo.py
  7. or
  8. python ./scripts/reporter_demo.py
  9. """
  10. from __future__ import annotations
  11. import random
  12. import time
  13. from pathlib import Path
  14. from databank.reporter.daily_file import DailyFileReporter
  15. from databank.core.models import RunSummary
  16. def main() -> None:
  17. """Run the reporter demo."""
  18. rep = DailyFileReporter(log_dir=str(Path.cwd() / "logs"), timezone="utc+8")
  19. # Simulate a run
  20. spider_name = "demoSpider"
  21. seeds = ["tokenA", "tokenB", "tokenC"]
  22. rep.notify_start(spider_name, seeds)
  23. # simulate successes
  24. docs = 0
  25. for _ in range(2):
  26. cnt = random.randint(1, 5)
  27. docs += cnt
  28. rep.notify_success(spider_name, cnt)
  29. time.sleep(0.2)
  30. # simulate an error
  31. rep.notify_error(spider_name, "Simulated network timeout")
  32. # Final summary
  33. summary = RunSummary()
  34. summary.total_docs = docs
  35. summary.per_spider[spider_name] = docs
  36. rep.notify_summary(summary)
  37. print("Reporter demo finished.")
  38. print("Logs are written under:", Path.cwd() / "logs")
  39. if __name__ == "__main__":
  40. main()