setup_max_rate.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. """Show blocked rate-limit log entries for the date action."""
  3. import time
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. _START_ACTION_REPEATEDLY_JS = """
  7. const done = arguments[arguments.length - 1];
  8. const title = arguments[0];
  9. const count = arguments[1];
  10. function bindingIdForTitle(actionTitle) {
  11. const button = document.querySelector('[title="' + actionTitle + '"]');
  12. if (!button) {
  13. throw new Error('Action button not found: ' + actionTitle);
  14. }
  15. return button.closest('.action-button').id.replace('actionButton-', '');
  16. }
  17. function uniqueTrackingId() {
  18. if (window.isSecureContext && window.crypto?.randomUUID) {
  19. return window.crypto.randomUUID();
  20. }
  21. return 'doc-screenshot-' + Date.now() + '-' + Math.random();
  22. }
  23. async function startAndWait(actionTitle) {
  24. const bindingId = bindingIdForTitle(actionTitle);
  25. const trackingId = uniqueTrackingId();
  26. const response = await window.client.startAction({
  27. bindingId,
  28. arguments: [],
  29. uniqueTrackingId: trackingId,
  30. });
  31. const executionTrackingId = response.executionTrackingId || trackingId;
  32. while (true) {
  33. const result = await window.client.executionStatus({
  34. executionTrackingId,
  35. });
  36. if (result.logEntry?.executionFinished) {
  37. return result.logEntry;
  38. }
  39. await new Promise((resolve) => setTimeout(resolve, 100));
  40. }
  41. }
  42. async function startRepeatedly(actionTitle, runs) {
  43. for (let i = 0; i < runs; i++) {
  44. await startAndWait(actionTitle);
  45. }
  46. }
  47. startRepeatedly(title, count).then(() => done(true)).catch((err) => done(String(err)));
  48. """
  49. def _wait_for_dashboard(driver, timeout=15):
  50. WebDriverWait(driver, timeout).until(
  51. lambda d: bool(d.find_element(By.TAG_NAME, "body").get_attribute("loaded-dashboard"))
  52. )
  53. WebDriverWait(driver, timeout).until(
  54. lambda d: d.execute_script("return !!window.client")
  55. )
  56. def _wait_for_rate_limited_logs(driver, timeout=20):
  57. def ready(d):
  58. try:
  59. rows = d.find_elements(By.CSS_SELECTOR, ".logs-table tbody tr")
  60. blocked = d.find_elements(By.CSS_SELECTOR, ".logs-table .status-blocked")
  61. completed = d.find_elements(By.CSS_SELECTOR, ".logs-table .status-success")
  62. except Exception:
  63. return False
  64. return len(rows) >= 5 and len(blocked) >= 2 and len(completed) >= 3
  65. WebDriverWait(driver, timeout).until(ready)
  66. def run(driver):
  67. _wait_for_dashboard(driver)
  68. driver.execute_async_script(_START_ACTION_REPEATEDLY_JS, "date", 5)
  69. time.sleep(1)
  70. driver.execute_script("window.location.href = '/logs'")
  71. _wait_for_rate_limited_logs(driver)
  72. time.sleep(0.2)