Skip to content

Instantly share code, notes, and snippets.

@whoojemaflip
Created June 27, 2017 15:22
Show Gist options
  • Save whoojemaflip/9062cb584f7d575539f94c4b71b46854 to your computer and use it in GitHub Desktop.
Save whoojemaflip/9062cb584f7d575539f94c4b71b46854 to your computer and use it in GitHub Desktop.
Gather benchmarking stats
# yes, it's a pile of hacks
require 'set'
require 'csv'
require 'pry'
require 'rugged'
filename = 'deployments.csv'
release_regex = /^release_[\d]+$/
staging_and_prod = []
CSV.foreach(filename, headers: true) do |row|
if row['environment'] != 'preview'
if row['domain'] == 'github.com'
if release_regex =~ row['version']
staging_and_prod << row
end
end
end
end
repos = Set.new
repo_tags = {}
staging_and_prod.each do |release|
repo = release['repo']
project = repo.split('/')[1]
tag = release['version']
repos << repo
if release['environment'] == 'production'
repo_tags[repo] ||= {}
repo_tags[repo][tag] ||= {}
repo_tags[repo][tag]['deployed_at'] = Time.parse release['deployed_at']
end
end
def resolve_reference_to_commit(rugged_ref)
if rugged_ref.target.class == Rugged::Commit
return rugged_ref.target
else
return resolve_reference_to_commit rugged_ref.target
end
end
repos.each do |repo_name|
folder = repo_name.split("/")[1]
begin
repo = Rugged::Repository.new("../#{folder}")
repo.tags.each do |tag|
if release_regex =~ tag.name
time = resolve_reference_to_commit(tag).time
repo_tags[repo_name] ||= {}
repo_tags[repo_name][tag.name] ||= {}
repo_tags[repo_name][tag.name]['merged_at'] = time
end
end
rescue Rugged::OSError => e
repo_tags.delete repo_name
rescue Exception => e
binding.pry
next
end
end
repo_tags.each do |repo,tags|
repo_tags[repo] = tags
.select { |tag, data| data.key?('merged_at') }
.sort_by { |tag, data| data['merged_at'] }
end
def get_next_deploy_date(future_tag_data)
future_tag_data.each do |tag_data|
if tag_data[1].key? 'deployed_at'
return tag_data[1]['deployed_at']
end
end
nil
end
def hours(merged, deployed)
if deployed < merged
nil
else
((deployed - merged) / (60 * 60)).round
end
end
repo_tags.each do |repo, tags|
tags.each_with_index do |tag_data, i|
tag = tag_data[0]
data = tag_data[1]
if data.key?('deployed_at')
repo_tags[repo][i][1]['delay'] = hours(data['merged_at'], data['deployed_at'])
else
deploy_date = get_next_deploy_date(tags.last(tags.size - i - 1))
if deploy_date
repo_tags[repo][i][1]['delay'] = hours(data['merged_at'], deploy_date)
else
repo_tags[repo][i][1]['delay'] = hours(data['merged_at'], Time.now - (60*60*24))
end
end
repo_tags[repo][i][1]['year_merged'] = data['merged_at'].year
repo_tags[repo][i][1]['month_merged'] = data['merged_at'].month
repo_tags[repo][i][1]['day_merged'] = data['merged_at'].day
end
end
out = {}
repo_tags.each do |repo, tags|
tags.each do |tag_data|
year = tag_data[1]['year_merged']
month = tag_data[1]['month_merged']
out["#{year}-#{month}"] ||= []
out["#{year}-#{month}"] << tag_data[1]['delay']
end
end
outfile = File.new('release_delay_averaged.csv', 'w+')
outfile << "year-month,average hours\n"
out.each do |month, data|
average = data.reject(&:nil?).inject(0.0) { |sum, el| sum + el } / data.size
outfile << "#{month},#{average.round}\n"
end
outfile.close
outfile = File.new('release_delay.csv', 'w+')
outfile << "repo,tag,merged_at,delay_in_hours,year_merged,month_merged,day_merged\n"
repo_tags.each do |repo, tags|
tags.reject { |t| t[1]['delay'].nil? } each do |tag_data|
outfile << "#{repo},#{tag_data[0]},#{tag_data[1]['merged_at']},#{tag_data[1]['delay']},#{tag_data[1]['year_merged']},#{tag_data[1]['month_merged']},#{tag_data[1]['day_merged']}\n"
end
end
outfile.close
# Upload this to the Release app in integration
# run it
# download the resulting CSV
require 'csv'
desc "Exports a CSV of historic deployments to /tmp/deployments.csv"
task export_deployments: :environment do
deploys = Deployment.includes(:application).map do |deployment|
if deployment.application && deployment.environment != 'preview'
{
version: deployment.version,
environment: deployment.environment,
application: deployment.application.name,
repo: deployment.application.repo,
domain: deployment.application.domain,
deployed_at: deployment.created_at
}
end
end.compact
csv = CSV.generate do |csv|
csv << deploys.first.keys
deploys.each do |deploy|
csv << deploy.values
end
end
File.write('/tmp/deployments.csv', csv)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment