diff --git a/Dockerfile b/Dockerfile index ef74782..a865137 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/app/Gemfile b/app/Gemfile index 4280a60..84ae1c2 100644 --- a/app/Gemfile +++ b/app/Gemfile @@ -7,3 +7,4 @@ gem 'httparty', '~> 0.14.0' gem 'influxdb', '~> 0.3.12' gem 'awesome_print' +gem 'sinatra', '~> 2.0', '>= 2.0.1' diff --git a/app/Gemfile.lock b/app/Gemfile.lock index 4103619..5d38289 100644 --- a/app/Gemfile.lock +++ b/app/Gemfile.lock @@ -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 diff --git a/app/stats.rb b/app/stats.rb new file mode 100644 index 0000000..f155ff5 --- /dev/null +++ b/app/stats.rb @@ -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 diff --git a/app/stats_pull.rb b/app/stats_pull.rb index d5edfc5..ad2166e 100644 --- a/app/stats_pull.rb +++ b/app/stats_pull.rb @@ -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)) diff --git a/root/etc/services.d/sinatra/run b/root/etc/services.d/sinatra/run new file mode 100644 index 0000000..1bab24a --- /dev/null +++ b/root/etc/services.d/sinatra/run @@ -0,0 +1,4 @@ +#!/usr/bin/with-contenv bash + +cd /opt/dockerhub-stats/ +ruby stats.rb