Skip to content

Instantly share code, notes, and snippets.

@dragonai
Last active October 9, 2015 01:09
Show Gist options
  • Save dragonai/fab612ffc94403b687e5 to your computer and use it in GitHub Desktop.
Save dragonai/fab612ffc94403b687e5 to your computer and use it in GitHub Desktop.
Rollbar Time of Last Deploy

##Preview

Description

Simple Dashing widget that displays how long ago the last deploy was according to Rollbar.

##Usage

#####Dependencies

Add httparty to the gemfile:

gem 'httparty'

and then just run

$ bundle install

#####Setup

To install this widget, simply run dashing install fab612ffc94403b687e5.

Then substitute the following placeholders in last_deploy.rb with the appropriate values:

  • YOUR_ROLLBAR_API_TOKEN => your Rollbar API key
  • DEPLOY_WIDGET_DATA_ID => the target HTML element's data-id attribute in your layout

Finally, to include the widget on a dashboard, drop the following snippet into your layout:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-view="LastDeploy" data-id="whatever_id_you_like"></div>
</li>
class Dashing.LastDeploy extends Dashing.Widget
@accessor 'current', Dashing.AnimatedValue
@accessor 'difference', ->
if @get('last')
last = parseInt(@get('last'))
current = parseInt(@get('current'))
if last != 0
diff = Math.abs(Math.round((current - last) / last * 100))
"#{diff}%"
else
""
@accessor 'arrow', ->
if @get('last')
if parseInt(@get('current')) > parseInt(@get('last')) then 'icon-arrow-up' else 'icon-arrow-down'
onData: (data) ->
if data.status
# clear existing "status-*" classes
$(@get('node')).attr 'class', (i,c) ->
c.replace /\bstatus-\S+/g, ''
# add new class
$(@get('node')).addClass "status-#{data.status}"
<h1 class="title" data-bind="title"></h1>
<h2 class="value" data-bind="current | shortenedNumber | prepend prefix | append suffix"></h2>
<p class="change-rate">
<i data-bind-class="arrow"></i><span data-bind="difference"></span>
</p>
<p class="more-info" data-bind="moreinfo"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'httparty'
rollbar_token = "YOUR_ROLLBAR_API_TOKEN"
SCHEDULER.every '5m', :first_in => 0 do |job|
deploys = HTTParty.get("https://api.rollbar.com/api/1/deploys/?access_token=#{rollbar_token}")['result']['deploys']
last_deploy_time_unix = deploys.find { |deploy| deploy['environment'] == "production" }['finish_time']
last_deploy_time = last_deploy_time_unix.nil? ? "Not available" : time_ago(last_deploy_time_unix)
send_event('DEPLOY_WIDGET_DATA_ID', { title: "Last Deploy", current: last_deploy_time })
end
def time_ago timestamp
diff_seconds = Time.now.utc.to_i - timestamp
case diff_seconds
when 0..59
"#{diff_seconds} seconds ago"
when 60..(3600-1)
"#{(diff_seconds/60.0).round} minutes ago"
when 3600..(3600*24-1)
"#{(diff_seconds/3600.0).round} hours ago"
when (3600*24)..(3600*24*30)
"#{(diff_seconds/(3600*24.0)).round} days ago"
else
Time.at(timestamp).strftime("%b %-d %Y, %l:%m %p")
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #47bbb3;
$value-color: #fff;
$title-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-last-deploy styles
// ----------------------------------------------------------------------------
.widget-last-deploy {
background-color: $background-color;
.title {
color: $title-color;
}
.value {
font-size: 50px;
text-transform: lowercase
}
.change-rate {
font-weight: 500;
font-size: 30px;
color: $value-color;
}
.more-info {
color: $moreinfo-color;
}
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment