Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #102 from jcoene/master

Display timestamps as relative in web interface
  • Loading branch information...
commit 94d282469a9a888d1f799af3f7cbafddefa4e9db 2 parents 5c56aa5 + 502cc3b
@jc00ke jc00ke authored
View
1  Changes.md
@@ -1,6 +1,7 @@
HEAD
-----------
- Add details page for jobs in retry queue (jcoene)
+- Display relative timestamps in web interface (jcoene)
0.10.0
-----------
View
4 lib/sidekiq/web.rb
@@ -105,6 +105,10 @@ def current_status
return 'idle' if workers.size > 0 && workers.map { |x| x[1] }.compact.size == 0
return 'active'
end
+
+ def relative_time(time)
+ "<time datetime=\"#{time.getutc.iso8601}\">#{time}</time>"
+ end
end
get "/" do
View
6 web/assets/javascripts/application.js
@@ -1,3 +1,9 @@
//= require vendor/jquery
+//= require vendor/jquery.timeago
//= require bootstrap
//= require_tree .
+
+jQuery(document).ready(function() {
+ jQuery.timeago.settings.allowFuture = true
+ jQuery("time").timeago();
+});
View
148 web/assets/javascripts/vendor/jquery.timeago.js
@@ -0,0 +1,148 @@
+/**
+ * Timeago is a jQuery plugin that makes it easy to support automatically
+ * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
+ *
+ * @name timeago
+ * @version 0.11.1
+ * @requires jQuery v1.2.3+
+ * @author Ryan McGeary
+ * @license MIT License - http://www.opensource.org/licenses/mit-license.php
+ *
+ * For usage and examples, visit:
+ * http://timeago.yarp.com/
+ *
+ * Copyright (c) 2008-2011, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
+ */
+(function($) {
+ $.timeago = function(timestamp) {
+ if (timestamp instanceof Date) {
+ return inWords(timestamp);
+ } else if (typeof timestamp === "string") {
+ return inWords($.timeago.parse(timestamp));
+ } else {
+ return inWords($.timeago.datetime(timestamp));
+ }
+ };
+ var $t = $.timeago;
+
+ $.extend($.timeago, {
+ settings: {
+ refreshMillis: 60000,
+ allowFuture: false,
+ strings: {
+ prefixAgo: null,
+ prefixFromNow: null,
+ suffixAgo: "ago",
+ suffixFromNow: "from now",
+ seconds: "less than a minute",
+ minute: "about a minute",
+ minutes: "%d minutes",
+ hour: "about an hour",
+ hours: "about %d hours",
+ day: "a day",
+ days: "%d days",
+ month: "about a month",
+ months: "%d months",
+ year: "about a year",
+ years: "%d years",
+ wordSeparator: " ",
+ numbers: []
+ }
+ },
+ inWords: function(distanceMillis) {
+ var $l = this.settings.strings;
+ var prefix = $l.prefixAgo;
+ var suffix = $l.suffixAgo;
+ if (this.settings.allowFuture) {
+ if (distanceMillis < 0) {
+ prefix = $l.prefixFromNow;
+ suffix = $l.suffixFromNow;
+ }
+ }
+
+ var seconds = Math.abs(distanceMillis) / 1000;
+ var minutes = seconds / 60;
+ var hours = minutes / 60;
+ var days = hours / 24;
+ var years = days / 365;
+
+ function substitute(stringOrFunction, number) {
+ var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
+ var value = ($l.numbers && $l.numbers[number]) || number;
+ return string.replace(/%d/i, value);
+ }
+
+ var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
+ seconds < 90 && substitute($l.minute, 1) ||
+ minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
+ minutes < 90 && substitute($l.hour, 1) ||
+ hours < 24 && substitute($l.hours, Math.round(hours)) ||
+ hours < 42 && substitute($l.day, 1) ||
+ days < 30 && substitute($l.days, Math.round(days)) ||
+ days < 45 && substitute($l.month, 1) ||
+ days < 365 && substitute($l.months, Math.round(days / 30)) ||
+ years < 1.5 && substitute($l.year, 1) ||
+ substitute($l.years, Math.round(years));
+
+ var separator = $l.wordSeparator === undefined ? " " : $l.wordSeparator;
+ return $.trim([prefix, words, suffix].join(separator));
+ },
+ parse: function(iso8601) {
+ var s = $.trim(iso8601);
+ s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
+ s = s.replace(/-/,"/").replace(/-/,"/");
+ s = s.replace(/T/," ").replace(/Z/," UTC");
+ s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
+ return new Date(s);
+ },
+ datetime: function(elem) {
+ // jQuery's `is()` doesn't play well with HTML5 in IE
+ var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
+ var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
+ return $t.parse(iso8601);
+ }
+ });
+
+ $.fn.timeago = function() {
+ var self = this;
+ self.each(refresh);
+
+ var $s = $t.settings;
+ if ($s.refreshMillis > 0) {
+ setInterval(function() { self.each(refresh); }, $s.refreshMillis);
+ }
+ return self;
+ };
+
+ function refresh() {
+ var data = prepareData(this);
+ if (!isNaN(data.datetime)) {
+ $(this).text(inWords(data.datetime));
+ }
+ return this;
+ }
+
+ function prepareData(element) {
+ element = $(element);
+ if (!element.data("timeago")) {
+ element.data("timeago", { datetime: $t.datetime(element) });
+ var text = $.trim(element.text());
+ if (text.length > 0) {
+ element.attr("title", text);
+ }
+ }
+ return element.data("timeago");
+ }
+
+ function inWords(date) {
+ return $t.inWords(distance(date));
+ }
+
+ function distance(date) {
+ return (new Date().getTime() - date.getTime());
+ }
+
+ // fix for IE6 suckage
+ document.createElement("abbr");
+ document.createElement("time");
+}(jQuery));
View
6 web/views/index.slim
@@ -22,6 +22,7 @@
th Queue
th Class
th Arguments
+ th Started
- workers.each do |(worker, msg)|
tr
td= worker
@@ -29,8 +30,9 @@
td= msg['queue']
td= msg['payload']['class']
td= msg['payload']['args'].inspect[0..100]
+ td== relative_time(Time.parse(msg['run_at']))
- else
- td colspan=3 Idle
+ td colspan=4 Idle
form action="#{root_path}reset" method="post"
button.btn type="submit" Clear worker list
@@ -56,7 +58,7 @@
- retries.each do |(msg, score)|
tr
td
- a href="retries/#{score}" #{Time.at(score)}
+ a href="retries/#{score}"== relative_time(Time.at(score))
td= msg['retry_count']
td
a href="queues/#{msg['queue']}" #{msg['queue']}
View
6 web/views/retry.slim
@@ -29,12 +29,12 @@ header
td= msg['retry_count']
tr
th Last Retry
- td= Time.parse(msg['retried_at'])
+ td== relative_time(Time.parse(msg['retried_at']))
- else
tr
th Originally Failed
- td= Time.parse(msg['failed_at'])
+ td== relative_time(Time.parse(msg['failed_at']))
tr
th Next Retry
- td= Time.at(score)
+ td== relative_time(Time.at(score))
Please sign in to comment.
Something went wrong with that request. Please try again.