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.

206 lines
6.6 KiB

  1. $(function () {
  2. var base = document.getElementById("base-url").getAttribute("href").slice(0, -1);
  3. var period = document.getElementById("period-value");
  4. var periodUnit = document.getElementById("period-unit");
  5. var grace = document.getElementById("grace-value");
  6. var graceUnit = document.getElementById("grace-unit");
  7. $(".rw .timeout-grace").click(function() {
  8. var code = $(this).closest("tr.checks-row").attr("id");
  9. if (!code) {
  10. code = this.dataset.code;
  11. }
  12. var url = base + "/checks/" + code + "/timeout/";
  13. $("#update-timeout-form").attr("action", url);
  14. $("#update-cron-form").attr("action", url);
  15. // Simple, period
  16. var parsed = secsToUnits(this.dataset.timeout);
  17. period.value = parsed.value;
  18. periodUnit.value = parsed.unit;
  19. periodSlider.noUiSlider.set(this.dataset.timeout);
  20. $("#update-timeout-timeout").val(this.dataset.timeout);
  21. // Simple, grace
  22. var parsed = secsToUnits(this.dataset.grace);
  23. grace.value = parsed.value;
  24. graceUnit.value = parsed.unit;
  25. graceSlider.noUiSlider.set(this.dataset.grace);
  26. $("#update-timeout-grace").val(this.dataset.grace);
  27. // Cron
  28. currentPreviewHash = "";
  29. $("#cron-preview").html("<p>Updating...</p>");
  30. $("#schedule").val(this.dataset.schedule);
  31. $("#tz").selectpicker("val", this.dataset.tz);
  32. var minutes = parseInt(this.dataset.grace / 60);
  33. $("#update-timeout-grace-cron").val(minutes);
  34. updateCronPreview();
  35. this.dataset.kind == "simple" ? showSimple() : showCron();
  36. $('#update-timeout-modal').modal({"show":true, "backdrop":"static"});
  37. return false;
  38. });
  39. var secsToUnits = function(secs) {
  40. if (secs % 86400 == 0) {
  41. return {value: secs / 86400, unit: 86400}
  42. }
  43. if (secs % 3600 == 0) {
  44. return {value: secs / 3600, unit: 3600}
  45. }
  46. return {value: Math.round(secs / 60), unit: 60}
  47. }
  48. var pipLabels = {
  49. 60: "1 minute",
  50. 1800: "30 minutes",
  51. 3600: "1 hour",
  52. 43200: "12 hours",
  53. 86400: "1 day",
  54. 604800: "1 week",
  55. 2592000: "30 days",
  56. 31536000: "365 days"
  57. }
  58. var periodSlider = document.getElementById("period-slider");
  59. noUiSlider.create(periodSlider, {
  60. start: [20],
  61. connect: "lower",
  62. range: {
  63. 'min': [60, 60],
  64. '30%': [3600, 3600],
  65. '60%': [86400, 86400],
  66. '75%': [604800, 86400],
  67. '90%': [2592000, 2592000],
  68. 'max': 31536000
  69. },
  70. pips: {
  71. mode: 'values',
  72. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000, 31536000],
  73. density: 4,
  74. format: {
  75. to: function(v) { return pipLabels[v] },
  76. from: function() {}
  77. }
  78. }
  79. });
  80. // Update inputs and the hidden field when user slides the period slider
  81. periodSlider.noUiSlider.on("slide", function(a, b, value) {
  82. var rounded = Math.round(value);
  83. $("#update-timeout-timeout").val(rounded);
  84. var parsed = secsToUnits(rounded);
  85. period.value = parsed.value;
  86. periodUnit.value = parsed.unit;
  87. });
  88. // Update the slider and the hidden field when user changes period inputs
  89. $(".period-input").on("keyup change", function() {
  90. var secs = Math.round(period.value * periodUnit.value);
  91. period.setCustomValidity(secs <= 31536000 ? "" : "Must not exceed 365 days");
  92. if (secs >= 60) {
  93. periodSlider.noUiSlider.set(secs);
  94. $("#update-timeout-timeout").val(secs);
  95. }
  96. })
  97. var graceSlider = document.getElementById("grace-slider");
  98. noUiSlider.create(graceSlider, {
  99. start: [20],
  100. connect: "lower",
  101. range: {
  102. 'min': [60, 60],
  103. '30%': [3600, 3600],
  104. '60%': [86400, 86400],
  105. '75%': [604800, 86400],
  106. '90%': [2592000, 2592000],
  107. 'max': 31536000
  108. },
  109. pips: {
  110. mode: 'values',
  111. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000, 31536000],
  112. density: 4,
  113. format: {
  114. to: function(v) { return pipLabels[v] },
  115. from: function() {}
  116. }
  117. }
  118. });
  119. // Update inputs and the hidden field when user slides the grace slider
  120. graceSlider.noUiSlider.on("slide", function(a, b, value) {
  121. var rounded = Math.round(value);
  122. $("#update-timeout-grace").val(rounded);
  123. var parsed = secsToUnits(rounded);
  124. grace.value = parsed.value;
  125. graceUnit.value = parsed.unit;
  126. });
  127. // Update the slider and the hidden field when user changes grace inputs
  128. $(".grace-input").on("keyup change", function() {
  129. var secs = Math.round(grace.value * graceUnit.value);
  130. grace.setCustomValidity(secs <= 31536000 ? "" : "Must not exceed 365 days");
  131. if (secs >= 60) {
  132. graceSlider.noUiSlider.set(secs);
  133. $("#update-timeout-grace").val(secs);
  134. }
  135. });
  136. function showSimple() {
  137. $("#update-timeout-form").show();
  138. $("#update-cron-form").hide();
  139. }
  140. function showCron() {
  141. $("#update-timeout-form").hide();
  142. $("#update-cron-form").show();
  143. }
  144. var currentPreviewHash = "";
  145. function updateCronPreview() {
  146. var schedule = $("#schedule").val();
  147. var tz = $("#tz").val();
  148. var hash = schedule + tz;
  149. // Don't try preview with empty values, or if values have not changed
  150. if (!schedule || !tz || hash == currentPreviewHash)
  151. return;
  152. // OK, we're good
  153. currentPreviewHash = hash;
  154. $("#cron-preview-title").text("Updating...");
  155. var token = $('input[name=csrfmiddlewaretoken]').val();
  156. $.ajax({
  157. url: base + "/checks/cron_preview/",
  158. type: "post",
  159. headers: {"X-CSRFToken": token},
  160. data: {schedule: schedule, tz: tz},
  161. success: function(data) {
  162. if (hash != currentPreviewHash) {
  163. return; // ignore stale results
  164. }
  165. $("#cron-preview" ).html(data);
  166. var haveError = $("#invalid-arguments").length > 0;
  167. $("#update-cron-submit").prop("disabled", haveError);
  168. }
  169. });
  170. }
  171. // Wire up events for Timeout/Cron forms
  172. $(".kind-simple").click(showSimple);
  173. $(".kind-cron").click(showCron);
  174. $("#schedule").on("keyup", updateCronPreview);
  175. $("#tz").on("change", updateCronPreview);
  176. });