class MediaConvertEnumerator
Attributes
from_date[RW]
job_status[RW]
jobs[R]
jobs_by_error_code[R]
jobs_by_status[R]
limit[R]
max_keys[R]
media_convert[R]
preview_only[R]
should_show_summary[R]
to_date[RW]
Public Class Methods
new(args = {})
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 17 def initialize(args = {}) aws_access_key_id = args[:aws_access_key_id] aws_secret_access_key = args[:aws_secret_access_key] aws_region = args[:aws_region] aws_profile = args.fetch(:aws_profile, args.fetch(:aws_profile_name, args.fetch(:profile, args.fetch(:profile_name, nil)))) aws_config = {} aws_config[:credentials] = (aws_access_key_id || aws_secret_access_key) ? Aws::Credentials.new(aws_access_key_id, aws_secret_access_key) : Aws::SharedCredentials.new(profile_name: aws_profile) aws_config[:region] ||= aws_region if aws_region @media_convert = Aws::MediaConvert::Client.new(aws_config) resp = @media_convert.describe_endpoints endpoint = resp.endpoints.first @media_convert = Aws::MediaConvert::Client.new(aws_config.merge(endpoint: endpoint.url)) @preview_only = args[:preview_only] @should_show_summary = args.fetch(:show_summary, preview_only) @job_status = args[:job_status] @limit = args[:limit] @limit = @limit.to_i if @limit @from_date = args[:from_date] @to_date = args[:to_date] @jobs = [] @jobs_by_status = Hash.new { |h, k| h[k] = [] } @jobs_by_error_code = Hash.new { |h, k| h[k] = [] } end
Public Instance Methods
build_grouped_data()
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 95 def build_grouped_data jobs.each do |job| status = job.status @jobs_by_status[status] << job @jobs_by_error_code[job.error_code] << job if status == 'ERROR' end @group_data_initialized = true end
concat_jobs(jobs, new_jobs)
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 53 def concat_jobs(jobs, new_jobs) jobs.concat new_jobs jobs end
grouped_data_to_table(collection, key_name)
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 143 def grouped_data_to_table(collection, key_name) table_data = [ [ key_name, 'Count' ] ] collection.each do |key, objects| row_values = [ key, humanize_number(objects.length) ] table_data << row_values end table_data end
human_readable_bytes_short(human_readable_number)
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 135 def human_readable_bytes_short(human_readable_number) "#{human_readable_number.split(',').first} #{HUMAN_READABLE_SHORT_SUFFIX[human_readable_number.count(',')]}" end
humanize_number(number)
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 139 def humanize_number(number) number.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(',').reverse end
print_table(data, options = { })
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 104 def print_table(data, options = { }) first_row = data.first table = first_row.is_a?(Hash) ? [first_row.keys] + data.map(&:values) : data widths = [] table.each do |line| line.each_with_index do |col, idx| cur_col_width = widths[idx] if cur_col_width col_len = col.to_s.length widths[idx] = col_len if col_len > cur_col_width else widths[idx] = col.to_s.length end end end # header separator separator_ary = widths.map { |n| '-' * n } table.insert(1, separator_ary) table.insert(-2, separator_ary) if options[:has_totals] format = widths.collect { |n| "%-#{n}s"}.join(' | ') table.each { |line| printf "| #{format} |\n", *line } end
retrieve_jobs(options = {}, &block)
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 58 def retrieve_jobs(options = {}, &block) @jobs = [] list_jobs_args = { } list_jobs_args[:status] = job_status if job_status #||= DEFAULT_LIST_JOBS_STATUS resp = @media_convert.list_jobs(list_jobs_args) # pp resp loop do concat_jobs(jobs, resp.jobs) job_count = jobs.length puts job_count break unless resp.next_token && (!limit || job_count < limit) created_date = resp.jobs.last.created_at break if from_date && created_date <= from_date resp = @media_convert.list_jobs(list_jobs_args.merge(next_token: resp.next_token)) end if from_date || to_date jobs.delete_if { |job| (from_date && job.created_at < from_date) || (to_date && job.created_at > to_date) } end @jobs = jobs.first(limit) if limit end
run(&block)
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 130 def run(&block) retrieve_jobs(&block) show_summary if @should_show_summary end
show_summary()
click to toggle source
# File lib/envoi/utils/mediaconvert_enumerator.rb, line 152 def show_summary jobs_by_status_table_data = grouped_data_to_table(jobs_by_status, 'Status') jobs_by_error_code_table_data = grouped_data_to_table(jobs_by_error_code.sort, 'Error Code') row_data = [ 'TOTAL', humanize_number(jobs.length)] jobs_by_status_table_data << row_data puts "\n\n--- Jobs Count by Status ---" print_table(jobs_by_status_table_data, { has_totals: true }) puts "\n\n--- Jobs Count by Error Code ---\n| https://docs.aws.amazon.com/mediaconvert/latest/ug/mediaconvert_error_codes.html\n" print_table(jobs_by_error_code_table_data, { has_totals: false }) end