airflow-hack

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

От 13.03.2020. Виж последната версия.

// ==UserScript==
// @name     airflow-hack
// @namespace thedream
// @description "调整airflow web上的时间显示方式"
// @version  1.4.0
// @grant    none
// @include http://plat-crontab*/admin/
// @include http://plat-crontab*/admin/dagrun/*
// @include http://plat-crontab*/admin/taskinstance/*
// @include http://localhost:8082/admin/
// @include http://localhost:8082/admin/dagrun/*
// @include http://localhost:8082/admin/taskinstance/*
// @include http://kettle-crontab*/admin/
// @include http://kettle-crontab*/admin/taskinstance/*
// @include http://kettle-crontab*/admin/dagrun/*
// @include http://kettle-crontab*/admin/airflow/tree*
// @author guanxiaoqin
// ==/UserScript==

(function () {
  'use strict';

  function heightLight(tableEle) {
    let oldColor = '';
    let tableRows = tableEle.tBodies[0].rows.length;
    for (let i = 0; i < tableRows; i++) {
      let item = tableEle.tBodies[0].rows[i];
      item.onmouseover = function () {
        oldColor = this.style.background;
        this.style.background = "#f1f1f1";  //高亮背景
      };
      item.onmouseout = function () {
        this.style.background = oldColor;
      };
    }
  }

  function replaceTimeInAdmin() {
    var $e = $(".text-nowrap.latest_dag_run");
    if ($e.length === 0) return;
    $e.each(function () {
      var time;
      var text = $(this).find("span").attr("data-original-title");
      if (text) time = text.split("Start Date: ")[1];
      time = changeTimeForm(time, true, false);
      $(this).find("a").text(time)
    })
  }

// todo
  function replaceTimeInTreeView() {
    var $e = $(".stateboxes .state");
    if ($e.length === 0) return;
    $e.each(function () {
      var Arr;
      var text = $(this).attr("data-original-title");
      if (text) Arr = text.split("<br>");
      let arrNew = Arr.map(function (currentValue) {
        let val = currentValue
        // Run: 2020-03-13T05:50:00+00:00   Started: 2020-03-13T05:55:01.213686+00:00   Ended: 2020-03-13T05:55:58.823775+00:00 // todo 判断是否需要处理 时间处理
        if (currentValue.indexOf('Run:') > -1 || currentValue.indexOf('Started:') > -1 || currentValue.indexOf('Ended:') > -1) {
          let time = currentValue.split(" ")[1]
          let fnTime = time
          var arr = fnTime.split("T");
          if (arr.length < 2) return; // 形式不是如此则arr.length为1
          if (arr[1]) {
            if (arr[1].indexOf("+") > -1) {  // 2020-03-13T08:30:00+00:00 去掉‘+’后面部分
              arr[1] = arr[1].split('+')[0]
            }
          }
          var dateArr = arr[0].split("-");

          if (dateArr.length === 2) {
            fnTime = (new Date()).getFullYear() + "-" + arr[0] + " " + arr[1]; // 补上此刻的年份
          } else {
            fnTime = arr[0] + " " + arr[1]; // 格式为 2019-12-22 06:09:28
          }
          time = changeTimeForm(fnTime, true, true);
          val = currentValue.split(" ")[0] + ' ' + time
        }
        return val;
      });

      let newText = arrNew.join('<br>')
      $(this).attr("data-original-title", newText) // 修改时间文本
    })
  }

  function addTitleInAdmin() {
    var $e = $(".text-nowrap.latest_dag_run");
    if ($e.length === 0) return; // 判断是否是admin页面
    var $td = $("tr td:nth-child(3)");
    if ($td.length === 0) return;
    $td.each(function () {
      var textAll = $(this).find('a').text();
      var text = $(this).find("a").attr("title");
      if (text) textAll = `${textAll} - (${text})`
      $(this).find("a").text(textAll)
    })
  }

  function changeTimeInTask(element) {
    var $e = $(element);
    if ($e.length === 0) return;
    $e.each(function () {
      var fnTime = $(this).text().split("+")[0].split(".")[0];
      var arr = fnTime.split("T");
      if (arr.length < 2) return; // 形式不是如此则arr.length为1
      var dateArr = arr[0].split("-");
      if (dateArr.length === 2) {
        fnTime = (new Date()).getFullYear() + "-" + arr[0] + " " + arr[1]; // 补上此刻的年份
      } else {
        fnTime = arr[0] + " " + arr[1]; // 格式为 2019-12-22 06:09:28
      }
      fnTime = changeTimeForm(fnTime, true, true);
      $(this).text(fnTime)
    })
  }

  function changeTimeForm(oldTime, with_year, with_second) {
    function pad(str) {
      return +str >= 10 ? str : '0' + str;
    }

    var timestamp = (new Date(oldTime)).getTime() + 8 * 60 * 60 * 1000; // 单位毫秒,增加8小时
    var dateObj = new Date(+timestamp);
    var year = dateObj.getFullYear();
    var month = pad(dateObj.getMonth() + 1);
    var date = pad(dateObj.getDate());
    var hours = pad(dateObj.getHours());
    var minutes = pad(dateObj.getMinutes());
    var seconds = pad(dateObj.getSeconds());
    var prefix_year = "";
    var prefix_second = "";
    if (with_year) prefix_year = year + '-';
    if (with_second) prefix_second = ':' + seconds;
    return prefix_year + month + '-' + date + ' ' + hours + ':' + minutes + prefix_second
  }

  replaceTimeInAdmin();// 替换admin页面中时间
  addTitleInAdmin(); // admin title拼接
  changeTimeInTask("td.col-execution_date nobr");// 改变task页面中指定元素的时间
  changeTimeInTask("td.col-start_date nobr");
  changeTimeInTask("td.col-end_date nobr");
  changeTimeInTask("td.col-queued_dttm nobr");
  replaceTimeInTreeView() // 替换/airflow/tree页面的时间

  let tableEle = document.getElementById('dags');// 获取admin页面表格
  if (tableEle) heightLight(tableEle);

})();