add sinatra, json pull, etc

This commit is contained in:
Paul Hendryx 2018-03-25 12:10:32 -04:00
parent f45bcb41b1
commit b807b6876e
6 changed files with 66 additions and 11 deletions

View File

@ -163,5 +163,5 @@ chmod 744 /etc/cron.d/dockerhub-stats
# ports and volumes
#WORKDIR /opt/docker-readme-sync
EXPOSE 3000 8083 8086
EXPOSE 3000 8083 8086 4567
VOLUME /config

View File

@ -7,3 +7,4 @@ gem 'httparty', '~> 0.14.0'
gem 'influxdb', '~> 0.3.12'
gem 'awesome_print'
gem 'sinatra', '~> 2.0', '>= 2.0.1'

View File

@ -10,7 +10,17 @@ GEM
json
json (2.0.2)
multi_xml (0.5.5)
mustermann (1.0.2)
rack (2.0.4)
rack-protection (2.0.1)
rack
rake (11.3.0)
sinatra (2.0.1)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.1)
tilt (~> 2.0)
tilt (2.0.8)
PLATFORMS
ruby
@ -20,9 +30,10 @@ DEPENDENCIES
httparty (~> 0.14.0)
influxdb (~> 0.3.12)
rake (~> 11.3)
sinatra (~> 2.0, >= 2.0.1)
RUBY VERSION
ruby 2.3.1p112
BUNDLED WITH
1.13.6
1.16.1

31
app/stats.rb Normal file
View File

@ -0,0 +1,31 @@
require "awesome_print"
require "influxdb"
require "sinatra"
def get_data
port = 8086
port = ENV['INFLUXDB_PORT'].to_i unless ENV['INFLUXDB_PORT'].nil?
influxdb = InfluxDB::Client.new database: "dockerhub_stats", port: port
query = 'select max(pull_count) as count from dockerhub_stats where time > now() - 1h group by repo,arch'
res = influxdb.query(query)
data = []
res.each do |repo|
repo_name = repo['tags']['repo']
count = repo['values'][0]['count']
arch = repo['tags']['arch']
data << { name: repo_name, count: count, arch: arch }
end
data
end
set :logging, true
set :dump_errors, true
set :bind, '0.0.0.0'
get '/stats' do
content_type :json
get_data.to_json
end

View File

@ -14,15 +14,21 @@ def processs_org(org)
res = HTTParty.get(
"https://hub.docker.com/v2/repositories/#{org}/",
query: { page_size: 100 })
data = JSON.parse(res.body)
query: { page_size: 20 })
pull_counts = {}
while data.include?('next') && data['next'].nil? == false
url = data['next']
res = HTTParty.get(url)
continue = true
while continue
data = JSON.parse(res.body)
pull_counts.merge!(process_counts(data, arch))
if data.include?('next') && data['next'].nil? == false
url = data['next']
res = HTTParty.get(url)
else
continue = false
end
end
pull_counts
@ -40,7 +46,10 @@ def process_counts(data, arch)
end
def insert_counts(data)
influxdb = InfluxDB::Client.new database: "dockerhub_stats", port: 18086
port = 8086
port = ENV['INFLUXDB_PORT'].to_i unless ENV['INFLUXDB_PORT'].nil?
influxdb = InfluxDB::Client.new database: "dockerhub_stats", port: port
data.each do |repo, repo_data|
influx_data = {
values: { pull_count: repo_data[:pull_count] },
@ -54,10 +63,12 @@ end
def run
orgs = nil
# pull orgs from env var
unless ENV['DOCKERHUB_ORGS'].nil?
orgs = ENV['DOCKERHUB_ORGS'].split(',')
end
# pull orgs from settings file if not specified in env var
if orgs.nil?
config_file = File.dirname(__FILE__) + "/config/settings.yml"
config = YAML.load_file(config_file)
@ -65,9 +76,6 @@ def run
orgs = config["orgs"]
end
ap orgs
exit
counts = {}
orgs.each do |org|
counts.merge!(processs_org(org))

View File

@ -0,0 +1,4 @@
#!/usr/bin/with-contenv bash
cd /opt/dockerhub-stats/
ruby stats.rb