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.

192 lines
5.9 KiB

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