airflow-hack

"调整airflow web上的时间显示方式"

  1. // ==UserScript==
  2. // @name airflow-hack
  3. // @namespace thedream
  4. // @description "调整airflow web上的时间显示方式"
  5. // @version 1.5.1
  6. // @grant none
  7. // @include http://plat-crontab*/admin/
  8. // @include http://plat-crontab*/admin/airflow/tree*
  9. // @include http://plat-crontab*/admin/dagrun/*
  10. // @include http://plat-crontab*/admin/taskinstance/*
  11. // @include http://localhost:8082/admin/
  12. // @include http://localhost:8082/admin/airflow/tree*
  13. // @include http://localhost:8082/admin/dagrun/*
  14. // @include http://localhost:8082/admin/taskinstance/*
  15. // @include http://kettle-crontab*/admin/
  16. // @include http://kettle-crontab*/admin/taskinstance/*
  17. // @include http://kettle-crontab*/admin/dagrun/*
  18. // @include http://kettle-crontab*/admin/airflow/tree*
  19. // @author guanxiaoqin
  20. // ==/UserScript==
  21.  
  22. (function () {
  23. 'use strict';
  24.  
  25. function heightLight(tableEle) {
  26. let oldColor = '';
  27. let tableRows = tableEle.tBodies[0].rows.length;
  28. for (let i = 0; i < tableRows; i++) {
  29. let item = tableEle.tBodies[0].rows[i];
  30. item.onmouseover = function () {
  31. oldColor = this.style.background;
  32. this.style.background = "#f1f1f1"; //高亮背景
  33. };
  34. item.onmouseout = function () {
  35. this.style.background = oldColor;
  36. };
  37. }
  38. }
  39.  
  40. function replaceTimeInAdmin() {
  41. var $e = $(".text-nowrap.latest_dag_run");
  42. if ($e.length === 0) return;
  43. $e.each(function () {
  44. var time;
  45. var text = $(this).find("span").attr("data-original-title");
  46. if (text) time = text.split("Start Date: ")[1];
  47. time = changeTimeForm(time, true, false);
  48. $(this).find("a").text(time)
  49. })
  50. }
  51.  
  52. function replaceTimeInTreeView() {
  53. var $e = $(".stateboxes .state");
  54. if ($e.length === 0) return;
  55. $e.each(function () {
  56. let Arr;
  57. let text = $(this).attr("data-original-title");
  58. if (text) Arr = text.split("<br>");
  59. let arrNew = Arr.map(function (currentValue) { // 修改数组里面指定的时间文本
  60. let newVal = currentValue
  61. if (currentValue.indexOf('Run:') > -1 || currentValue.indexOf('Started:') > -1 || currentValue.indexOf('Ended:') > -1) {
  62. let time = currentValue.split(" ")[1] // 'Run: 2020-03-13T05:55:01.213686+00:00'
  63. let fnTime = changeTimeByRule(time)
  64. time = changeTimeForm(fnTime, true, true);
  65. newVal = currentValue.split(" ")[0] + ' ' + time
  66. }
  67. return newVal;
  68. });
  69. let newText = arrNew.join('<br>')
  70. $(this).attr("data-original-title", newText) // 替换属性内容
  71. })
  72. }
  73.  
  74. function addTitleInAdmin() {
  75. var $e = $(".text-nowrap.latest_dag_run");
  76. if ($e.length === 0) return; // 判断是否是admin页面
  77. var $td = $("tr td:nth-child(3)");
  78. if ($td.length === 0) return;
  79. $td.each(function () {
  80. var textAll = $(this).find('a').text();
  81. var text = $(this).find("a").attr("title");
  82. if (text) textAll = `${textAll} - (${text})`
  83. $(this).find("a").text(textAll)
  84. })
  85. }
  86.  
  87. function changeTimeInTask(element) {
  88. var $e = $(element);
  89. if ($e.length === 0) return;
  90. $e.each(function () {
  91. let time = $(this).text()
  92. if(!time) return // 容错性
  93. let fnTime = changeTimeByRule(time)
  94. fnTime = changeTimeForm(fnTime, true, true);
  95. $(this).text(fnTime)
  96. })
  97. }
  98.  
  99. function changeTimeByRule(time) { // 2020-03-13T05:55:01.213686+00:00 ====> 2019-12-22 06:09:28
  100. let fnTime = time.split("+")[0].split(".")[0];
  101. const arr = fnTime.split("T");
  102. if (arr.length < 2) return;
  103. const dateArr = arr[0].split("-"); // 年月日数组
  104. if (dateArr.length === 2) {
  105. fnTime = (new Date()).getFullYear() + "-" + arr[0] + " " + arr[1]; // 补上此刻的年份
  106. } else {
  107. fnTime = arr[0] + " " + arr[1]; // 格式为 2019-12-22 06:09:28
  108. }
  109. return fnTime
  110. }
  111.  
  112. function changeTimeForm(oldTime, with_year, with_second) { // 2019-12-22 06:09:28 ====> 2019-12-22 14:09:28
  113. function pad(str) {
  114. return +str >= 10 ? str : '0' + str;
  115. }
  116.  
  117. var timestamp = (new Date(oldTime)).getTime() + 8 * 60 * 60 * 1000; // 单位毫秒,增加8小时
  118. var dateObj = new Date(+timestamp);
  119. var year = dateObj.getFullYear();
  120. var month = pad(dateObj.getMonth() + 1);
  121. var date = pad(dateObj.getDate());
  122. var hours = pad(dateObj.getHours());
  123. var minutes = pad(dateObj.getMinutes());
  124. var seconds = pad(dateObj.getSeconds());
  125. var prefix_year = "";
  126. var prefix_second = "";
  127. if (with_year) prefix_year = year + '-';
  128. if (with_second) prefix_second = ':' + seconds;
  129. return prefix_year + month + '-' + date + ' ' + hours + ':' + minutes + prefix_second
  130. }
  131.  
  132. replaceTimeInAdmin();// 替换admin页面中时间
  133. addTitleInAdmin(); // admin title拼接
  134. changeTimeInTask("td.col-execution_date nobr");// 改变task页面中指定元素的时间
  135. changeTimeInTask("td.col-start_date nobr");
  136. changeTimeInTask("td.col-end_date nobr");
  137. changeTimeInTask("td.col-queued_dttm nobr");
  138. replaceTimeInTreeView() // 替换/airflow/tree页面的时间
  139.  
  140. let tableEle = document.getElementById('dags');// 获取admin页面表格
  141. if (tableEle) heightLight(tableEle);
  142.  
  143. })();