setup_logs_calendar.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. """Open the logs calendar view with executions on the current month."""
  3. import time
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. _START_ACTIONS_JS = """
  7. const done = arguments[arguments.length - 1];
  8. const titles = arguments[0];
  9. function bindingIdForTitle(title) {
  10. const button = document.querySelector('[title="' + title + '"]');
  11. if (!button) {
  12. throw new Error('Action button not found: ' + title);
  13. }
  14. return button.closest('.action-button').id.replace('actionButton-', '');
  15. }
  16. function uniqueTrackingId() {
  17. if (window.isSecureContext && window.crypto?.randomUUID) {
  18. return window.crypto.randomUUID();
  19. }
  20. return 'doc-screenshot-' + Date.now() + '-' + Math.random();
  21. }
  22. function startByTitle(title) {
  23. return window.client.startAction({
  24. bindingId: bindingIdForTitle(title),
  25. arguments: [],
  26. uniqueTrackingId: uniqueTrackingId(),
  27. });
  28. }
  29. Promise.all(titles.map(startByTitle)).then(() => done(true)).catch((err) => done(String(err)));
  30. """
  31. def _wait_for_dashboard(driver, timeout=15):
  32. WebDriverWait(driver, timeout).until(
  33. lambda d: bool(d.find_element(By.TAG_NAME, "body").get_attribute("loaded-dashboard"))
  34. )
  35. WebDriverWait(driver, timeout).until(
  36. lambda d: d.execute_script("return !!window.client")
  37. )
  38. def run(driver):
  39. _wait_for_dashboard(driver)
  40. driver.execute_async_script(
  41. _START_ACTIONS_JS,
  42. ["Check disk space", "Restart service"],
  43. )
  44. time.sleep(2)
  45. driver.execute_script("window.location.href = '/logs/calendar'")
  46. WebDriverWait(driver, 15).until(
  47. lambda d: len(d.find_elements(By.CSS_SELECTOR, ".calendar-event")) >= 2
  48. )
  49. driver.execute_script(
  50. """
  51. const today = new Date();
  52. const key = today.getFullYear() + '-'
  53. + String(today.getMonth() + 1).padStart(2, '0') + '-'
  54. + String(today.getDate()).padStart(2, '0');
  55. const cell = document.querySelector('[data-calendar-date="' + key + '"]');
  56. if (cell) {
  57. cell.scrollIntoView({ block: 'center' });
  58. }
  59. """
  60. )
  61. time.sleep(0.2)