airflow-hack

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

Ajankohdalta 11.11.2019. Katso uusin versio.

// ==UserScript==
// @name     airflow-hack
// @namespace thedream
// @description "调整airflow web上的时间显示方式"
// @version  1.0.1
// @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/*
// @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)
        })
    }

    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
            fnTime = (new Date()).getFullYear() + "-" + arr[0] + " " + arr[1]; // 补上此刻的年份
            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页面中时间
    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");

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

})();