dag.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* global document, window, CustomEvent, $ */
  20. import { getMetaValue } from "./utils";
  21. import { approxTimeFromNow, formatDateTime } from "./datetime_utils";
  22. import { openDatasetModal, getDatasetTooltipInfo } from "./datasetUtils";
  23. const dagId = getMetaValue("dag_id");
  24. const pausedUrl = getMetaValue("paused_url");
  25. // eslint-disable-next-line import/prefer-default-export
  26. export const dagTZ = getMetaValue("dag_timezone");
  27. const datasetsUrl = getMetaValue("datasets_url");
  28. const nextRun = {
  29. createAfter: getMetaValue("next_dagrun_create_after"),
  30. intervalStart: getMetaValue("next_dagrun_data_interval_start"),
  31. intervalEnd: getMetaValue("next_dagrun_data_interval_end"),
  32. };
  33. let nextDatasets = [];
  34. let nextDatasetsError;
  35. const setNextDatasets = (datasets, error) => {
  36. nextDatasets = datasets;
  37. nextDatasetsError = error;
  38. };
  39. $(window).on("load", function onLoad() {
  40. $(`a[href*="${this.location.pathname}"]`).parent().addClass("active");
  41. $(".never_active").removeClass("active");
  42. const run = $("#next-dataset-tooltip");
  43. const singleDatasetUri = $(run).data("uri");
  44. if (!singleDatasetUri) {
  45. getDatasetTooltipInfo(dagId, run, setNextDatasets);
  46. }
  47. });
  48. $("#pause_resume").on("change", function onChange() {
  49. const $input = $(this);
  50. const id = $input.data("dag-id");
  51. const isPaused = $input.is(":checked");
  52. const requireConfirmation = $input.is("[data-require-confirmation]");
  53. if (requireConfirmation) {
  54. const confirmation = window.confirm(
  55. `Are you sure you want to ${isPaused ? "resume" : "pause"} this DAG?`
  56. );
  57. if (!confirmation) {
  58. $input.prop("checked", !isPaused);
  59. return;
  60. }
  61. }
  62. const url = `${pausedUrl}?is_paused=${isPaused}&dag_id=${encodeURIComponent(
  63. id
  64. )}`;
  65. // Remove focus on element so the tooltip will go away
  66. $input.trigger("blur");
  67. $input.removeClass("switch-input--error");
  68. // dispatch an event that React can listen for
  69. const event = new CustomEvent("paused", { detail: isPaused });
  70. document.dispatchEvent(event);
  71. $.post(url).fail(() => {
  72. setTimeout(() => {
  73. $input.prop("checked", !isPaused);
  74. $input.addClass("switch-input--error");
  75. event.value = !isPaused;
  76. document.dispatchEvent(event);
  77. }, 500);
  78. });
  79. });
  80. $("#next-run").on("mouseover", () => {
  81. $("#next-run").attr("data-original-title", () => {
  82. let newTitle = "";
  83. if (nextRun.createAfter) {
  84. newTitle += `<strong>Run After:</strong> ${formatDateTime(
  85. nextRun.createAfter
  86. )}<br>`;
  87. newTitle += `Next Run: ${approxTimeFromNow(nextRun.createAfter)}<br><br>`;
  88. }
  89. if (nextRun.intervalStart && nextRun.intervalEnd) {
  90. newTitle += "<strong>Data Interval</strong><br>";
  91. newTitle += `Start: ${formatDateTime(nextRun.intervalStart)}<br>`;
  92. newTitle += `End: ${formatDateTime(nextRun.intervalEnd)}`;
  93. }
  94. return newTitle;
  95. });
  96. });
  97. $(".next-dataset-triggered").on("click", (e) => {
  98. const run = $("#next-dataset-tooltip");
  99. const summary = $(e.target).data("summary");
  100. const singleDatasetUri = $(run).data("uri");
  101. if (!singleDatasetUri) {
  102. openDatasetModal(dagId, summary, nextDatasets, nextDatasetsError);
  103. } else {
  104. window.location.href = `${datasetsUrl}?uri=${encodeURIComponent(
  105. singleDatasetUri
  106. )}`;
  107. }
  108. });