You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.1 KiB

  1. $(function () {
  2. var base = document.getElementById("base-url").getAttribute("href").slice(0, -1);
  3. $(".rw .timeout-grace").click(function() {
  4. var code = $(this).closest("tr.checks-row").attr("id");
  5. if (!code) {
  6. code = this.dataset.code;
  7. }
  8. var url = base + "/checks/" + code + "/timeout/";
  9. $("#update-timeout-form").attr("action", url);
  10. $("#update-cron-form").attr("action", url);
  11. // Simple
  12. periodSlider.noUiSlider.set(this.dataset.timeout);
  13. graceSlider.noUiSlider.set(this.dataset.grace);
  14. // Cron
  15. currentPreviewHash = "";
  16. $("#cron-preview").html("<p>Updating...</p>");
  17. $("#schedule").val(this.dataset.schedule);
  18. $("#tz").selectpicker("val", this.dataset.tz);
  19. var minutes = parseInt(this.dataset.grace / 60);
  20. $("#update-timeout-grace-cron").val(minutes);
  21. updateCronPreview();
  22. this.dataset.kind == "simple" ? showSimple() : showCron();
  23. $('#update-timeout-modal').modal({"show":true, "backdrop":"static"});
  24. return false;
  25. });
  26. var MINUTE = {name: "minute", nsecs: 60};
  27. var HOUR = {name: "hour", nsecs: MINUTE.nsecs * 60};
  28. var DAY = {name: "day", nsecs: HOUR.nsecs * 24};
  29. var WEEK = {name: "week", nsecs: DAY.nsecs * 7};
  30. var UNITS = [WEEK, DAY, HOUR, MINUTE];
  31. var secsToText = function(total) {
  32. var remainingSeconds = Math.floor(total);
  33. var result = "";
  34. for (var i=0, unit; unit=UNITS[i]; i++) {
  35. if (unit === WEEK && remainingSeconds % unit.nsecs != 0) {
  36. // Say "8 days" instead of "1 week 1 day"
  37. continue
  38. }
  39. var count = Math.floor(remainingSeconds / unit.nsecs);
  40. remainingSeconds = remainingSeconds % unit.nsecs;
  41. if (count == 1) {
  42. result += "1 " + unit.name + " ";
  43. }
  44. if (count > 1) {
  45. result += count + " " + unit.name + "s ";
  46. }
  47. }
  48. return result;
  49. };
  50. var periodSlider = document.getElementById("period-slider");
  51. noUiSlider.create(periodSlider, {
  52. start: [20],
  53. connect: "lower",
  54. range: {
  55. 'min': [60, 60],
  56. '33%': [3600, 3600],
  57. '66%': [86400, 86400],
  58. '83%': [604800, 604800],
  59. 'max': 2592000,
  60. },
  61. pips: {
  62. mode: 'values',
  63. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000],
  64. density: 4,
  65. format: {
  66. to: secsToText,
  67. from: function() {}
  68. }
  69. }
  70. });
  71. periodSlider.noUiSlider.on("update", function(a, b, value) {
  72. var rounded = Math.round(value);
  73. $("#period-slider-value").text(secsToText(rounded));
  74. $("#update-timeout-timeout").val(rounded);
  75. });
  76. var graceSlider = document.getElementById("grace-slider");
  77. noUiSlider.create(graceSlider, {
  78. start: [20],
  79. connect: "lower",
  80. range: {
  81. 'min': [60, 60],
  82. '33%': [3600, 3600],
  83. '66%': [86400, 86400],
  84. '83%': [604800, 604800],
  85. 'max': 2592000,
  86. },
  87. pips: {
  88. mode: 'values',
  89. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000],
  90. density: 4,
  91. format: {
  92. to: secsToText,
  93. from: function() {}
  94. }
  95. }
  96. });
  97. graceSlider.noUiSlider.on("update", function(a, b, value) {
  98. var rounded = Math.round(value);
  99. $("#grace-slider-value").text(secsToText(rounded));
  100. $("#update-timeout-grace").val(rounded);
  101. });
  102. function showSimple() {
  103. $("#update-timeout-form").show();
  104. $("#update-cron-form").hide();
  105. }
  106. function showCron() {
  107. $("#update-timeout-form").hide();
  108. $("#update-cron-form").show();
  109. }
  110. var currentPreviewHash = "";
  111. function updateCronPreview() {
  112. var schedule = $("#schedule").val();
  113. var tz = $("#tz").val();
  114. var hash = schedule + tz;
  115. // Don't try preview with empty values, or if values have not changed
  116. if (!schedule || !tz || hash == currentPreviewHash)
  117. return;
  118. // OK, we're good
  119. currentPreviewHash = hash;
  120. $("#cron-preview-title").text("Updating...");
  121. var token = $('input[name=csrfmiddlewaretoken]').val();
  122. $.ajax({
  123. url: base + "/checks/cron_preview/",
  124. type: "post",
  125. headers: {"X-CSRFToken": token},
  126. data: {schedule: schedule, tz: tz},
  127. success: function(data) {
  128. if (hash != currentPreviewHash) {
  129. return; // ignore stale results
  130. }
  131. $("#cron-preview" ).html(data);
  132. var haveError = $("#invalid-arguments").length > 0;
  133. $("#update-cron-submit").prop("disabled", haveError);
  134. }
  135. });
  136. }
  137. // Wire up events for Timeout/Cron forms
  138. $(".kind-simple").click(showSimple);
  139. $(".kind-cron").click(showCron);
  140. $("#schedule").on("keyup", updateCronPreview);
  141. $("#tz").on("change", updateCronPreview);
  142. });