Skip to content

Instantly share code, notes, and snippets.

@jonasrosland
Last active September 22, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasrosland/47b71cb4920fb6df1a45 to your computer and use it in GitHub Desktop.
Save jonasrosland/47b71cb4920fb6df1a45 to your computer and use it in GitHub Desktop.
Docker Hub Dashing widget

Docker Hub Dashing widget

Enabling you to track your Docker Hub metrics such as stars and pulls.

Sample HTML to put in your .erb file:

    <li data-row="1" data-col="1" data-sizex="2" data-sizey="1" onclick="location.href='https://registry.hub.docker.com/repos/emccode/';">
      <div data-id="docker_hub_stars" data-view="List" data-unordered="true" data-title="Docker stars" style="background-color: #9b59b6"></div>
      <i class="icon-star icon-background"></i>
    </li>
    <li data-row="1" data-col="1" data-sizex="2" data-sizey="1" onclick="location.href='https://registry.hub.docker.com/repos/emccode/';">
      <div data-id="docker_hub_pulls" data-view="List" data-unordered="true" data-title="Docker pulls" style="background-color: #9b59b6"></div>
      <i class="icon-download icon-background"></i>
    </li>
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'mechanize'
# Created by Jonas Rosland, https://github.com/jonasrosland, https://twitter.com/jonasrosland
# and Kendrick Coleman, https://github.com/kacole2, https://twitter.com/kendrickcoleman
# Template used from https://github.com/foobugs/foobugs-dashboard/blob/master/jobs/twitter_user.rb
# This job tracks metrics of Docker Hub downloads and stars by scraping
# the public website since only stars and not downloads are
# available through the API
#
# This job should use the `List` widget
# Config
# ------
docker_username = ENV['DOCKER_USERNAME'] || 'emccode'
mechanize = Mechanize.new
max_length = 7
ordered = true
# Number of pages with results you get when going to the site and looking under the DOCKER_USERNAME
last_page_number = 4
SCHEDULER.every '1m', :first_in => 0 do |job|
repoArray = []
for page_number in 1..last_page_number
page = mechanize.get('https://hub.docker.com/u/' + docker_username + '?page=' + page_number.to_s)
page.search('.RepositoryListItem__flexible___9wm16').each do |repo|
repoTitle = repo.at('.RepositoryListItem__repoName___3iIWs').text.strip.gsub(docker_username + '/', '')
repoStars = repo.at('.RepositoryListItem__stats___3GILF:nth-child(2)').text.strip.gsub('STARS', '')
repoPulls = repo.at('.RepositoryListItem__stats___3GILF:nth-child(3)').text.strip.gsub('PULLS', '')
repoArray.push({title: repoTitle, stars: repoStars.to_i, pulls: repoPulls.to_i})
end
end
repos_stars = Array.new
repos_pulls = Array.new
repoArray.each do |repo|
repos_stars.push({
label: repo[:title],
value: repo[:stars]
})
repos_pulls.push({
label: repo[:title],
value: repo[:pulls]
})
end
if ordered
repos_stars = repos_stars.sort_by { |obj| -obj[:value] }
repos_pulls = repos_pulls.sort_by { |obj| -obj[:value] }
end
send_event('docker_hub_stars', { items: repos_stars.slice(0, max_length) })
send_event('docker_hub_pulls', { items: repos_pulls.slice(0, max_length) })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment