Skip to content

Instantly share code, notes, and snippets.

@kool79
Last active February 13, 2018 20:20
Show Gist options
  • Save kool79/360e5066b78f2152d1806dc0b19f5b83 to your computer and use it in GitHub Desktop.
Save kool79/360e5066b78f2152d1806dc0b19f5b83 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Jenkins timestamper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @include https://ci-master.ontrq.com/*
// @include https://ci-master-01.ecofabric.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// Your code here...
var StrUtil = {
add0Prefix: function(n){
if(n < 10) return '0' + n;
return '' + n;
},
remove0Prefix: function(str){
return str.replace(/^0+/,'');
},
_getEnglishMultiple: function(n){
if(n < 2) return '';
return 's';
},
getTimeReadable: function(sec){
var str;
var n;
var m;
if(sec < 60){
str = sec + ' sec';
}
else if(sec < 3600){
n = Math.floor(sec/60);
str = n + ' min';
}
else if(sec < 86400){
n = Math.floor(sec/3600);
m = Math.floor((sec - n*3600)/60);
str = n + ' h ' + m + ' min';
}
else {
n = Math.floor(sec/86400);
m = Math.floor((sec - n*86400)/3600);
str = n + ' d ' + m + ' h';
}
return str + ' ago';
}
};
var TimeUtil = {
_isCaliforniaDaylightSaving: function(ts){
// modified from the code at stackoverflow by captncraig
// http://stackoverflow.com/questions/5590429/calculating-daylight-saving-time-from-only-date
var t = new Date();
t.setTime(ts/1);
var month = t.getUTCMonth() + 1;
// January, february, and december are out.
if (month < 3 || month > 11) { return false; }
// April to October are in
if (month > 3 && month < 11) { return true; }
var previousSunday = t.getUTCDate() - t.getUTCDay();
//In march, we are DST if our previous sunday was on or after the 8th.
if (month == 3) { return previousSunday >= 8; }
//In november we must be before the first sunday to be dst.
//That means the previous sunday must be before the 1st.
return previousSunday <= 0;
},
_isUkraineDaylightSaving: function(ts){
// modified from the code at stackoverflow by captncraig
// http://stackoverflow.com/questions/5590429/calculating-daylight-saving-time-from-only-date
var t = new Date();
t.setTime(ts/1);
var month = t.getUTCMonth() + 1;
// January, february, and december are out.
if (month < 3 || month > 11) { return false; }
// April to October are in
if (month > 3 && month < 11) { return true; }
var previousSunday = t.getUTCDate() - t.getUTCDay();
//In march, we are DST if our previous sunday was on or after the 8th.
if (month == 3) { return previousSunday >= 8; }
//In november we must be before the first sunday to be dst.
//That means the previous sunday must be before the 1st.
return previousSunday <= 0;
},
getHTMLTime: function(ts){
var html = '<div class="timeDisplay" ts="' + ts + '">';
var displayProfile = [
{
name: 'UA',
icon: 'http://l.yimg.com/os/mit/media/m/sports/images/flags/50x50/tpe-550772.png',
offset: 3600000 * (this._isUkraineDaylightSaving(ts) ? 3 : 2)
}/*,
{
name: 'UTC',
icon: 'http://l.yimg.com/os/mit/media/m/sports/images/flags/50x50/isl-550772.png',
offset: 0
},
{
name: 'Sunnyvale',
icon: 'http://l.yimg.com/os/mit/media/m/sports/images/flags/50x50/usa-550772.png',
offset: 3600000 * (this._isCaliforniaDaylightSaving(ts) ? -7 : -8)
}*/
];
// console.log(displayProfile);
var dp;
var t = new Date();
var dayMap = ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'];
for(var i = 0 ; i < displayProfile.length ; i++){
dp = displayProfile[i];
t.setTime(ts/1 + dp.offset);
//html += '<div class="tz">';
//html += '<img src=' + dp.icon + ' height="16" alt="' + dp.name + '" title="' + dp.name + '" /> ';
html += '<div class="tz" title="' + dp.name + '"> ';
html += t.getUTCFullYear() + '/' + StrUtil.add0Prefix(t.getUTCMonth()+1) + '/' + StrUtil.add0Prefix(t.getUTCDate()) + ' ' + dayMap[t.getUTCDay()] + ' ' + StrUtil.add0Prefix(t.getUTCHours()) + ':' + StrUtil.add0Prefix(t.getUTCMinutes()) + ':' + StrUtil.add0Prefix(t.getUTCSeconds());
html += '</div>';
}
html += this.getRelativeTimeHTML(ts);
return html + '</div>';
},
getRelativeTimeHTML(ts){
var timeDiff = Math.floor(((new Date()).getTime() - ts) / 1000);
return '<div><img src="/jenkins/static/035b7cd2/images/16x16/clock.png" height="16"/> ' + StrUtil.getTimeReadable(timeDiff) + '</div>';
}
};
function modifyBuildHistory(){
$('#buildHistory .pane.build-details').each(function(){
var me = $(this);
if(me.find('.timeDisplay').length) return;
me.find('.build-link').replaceWith(TimeUtil.getHTMLTime(me.attr('time')));
//me.find('.build-link').html(TimeUtil.getHTMLTime(me.attr('time')));
me.removeAttr('style'); // remove style='height: 21px'
//var bar = me.find('.progress-bar');
//if(bar){
// bar.after(bar.attr('tooltip'));
//}
});
$('.build-row .build-row-cell .pane.build-details a').bind('click', function(e){
e.preventDefault();
});
}
function parseTimestamp(str){
if(str.match(/[AP]M/)){
// Jun 21, 2016 6:08:42 AM
return Date.parse(str + ' UTC');
}
return false;
}
setInterval(modifyBuildHistory, 100);
var titleDom = $('#main-panel .build-caption.page-headline');
if(titleDom){
var texts = titleDom.text().match(/(.+)\((.+)\)/);
if(texts){
var text = texts[2];
var t = parseTimestamp(text);
if(t){
titleDom.after(TimeUtil.getHTMLTime(t));
}
}
}
// Your code here...
var css = ".build-row-cell .pane.build-details {width: inherit;}";
if (typeof GM_addStyle != "undefined") {
GM_addStyle(css);
} else if (typeof addStyle != "undefined") {
addStyle(css);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment