Class: MediaConvertEnumerator
- Inherits:
-
Object
- Object
- MediaConvertEnumerator
- Defined in:
- lib/envoi/utils/mediaconvert_enumerator.rb
Instance Attribute Summary collapse
-
#from_date ⇒ Object
Returns the value of attribute from_date.
-
#job_status ⇒ Object
Returns the value of attribute job_status.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#jobs_by_error_code ⇒ Object
readonly
Returns the value of attribute jobs_by_error_code.
-
#jobs_by_status ⇒ Object
readonly
Returns the value of attribute jobs_by_status.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#max_keys ⇒ Object
readonly
Returns the value of attribute max_keys.
-
#media_convert ⇒ Object
readonly
Returns the value of attribute media_convert.
-
#preview_only ⇒ Object
readonly
Returns the value of attribute preview_only.
-
#should_show_summary ⇒ Object
readonly
Returns the value of attribute should_show_summary.
-
#to_date ⇒ Object
Returns the value of attribute to_date.
Instance Method Summary collapse
- #build_grouped_data ⇒ Object
- #concat_jobs(jobs, new_jobs) ⇒ Object
- #grouped_data_to_table(collection, key_name) ⇒ Object
- #human_readable_bytes_short(human_readable_number) ⇒ Object
- #humanize_number(number) ⇒ Object
-
#initialize(args = {}) ⇒ MediaConvertEnumerator
constructor
A new instance of MediaConvertEnumerator.
- #print_table(data, options = { }) ⇒ Object
- #retrieve_jobs(options = {}, &block) ⇒ Object
- #run(&block) ⇒ Object
- #show_summary ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ MediaConvertEnumerator
Returns a new instance of MediaConvertEnumerator.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |
Instance Attribute Details
#from_date ⇒ Object
Returns the value of attribute from_date.
13 14 15 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 13 def from_date @from_date end |
#job_status ⇒ Object
Returns the value of attribute job_status.
13 14 15 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 13 def job_status @job_status end |
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
7 8 9 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 7 def jobs @jobs end |
#jobs_by_error_code ⇒ Object (readonly)
Returns the value of attribute jobs_by_error_code.
15 16 17 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 15 def jobs_by_error_code @jobs_by_error_code end |
#jobs_by_status ⇒ Object (readonly)
Returns the value of attribute jobs_by_status.
15 16 17 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 15 def jobs_by_status @jobs_by_status end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
11 12 13 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 11 def limit @limit end |
#max_keys ⇒ Object (readonly)
Returns the value of attribute max_keys.
11 12 13 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 11 def max_keys @max_keys end |
#media_convert ⇒ Object (readonly)
Returns the value of attribute media_convert.
5 6 7 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 5 def media_convert @media_convert end |
#preview_only ⇒ Object (readonly)
Returns the value of attribute preview_only.
9 10 11 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 9 def preview_only @preview_only end |
#should_show_summary ⇒ Object (readonly)
Returns the value of attribute should_show_summary.
9 10 11 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 9 def should_show_summary @should_show_summary end |
#to_date ⇒ Object
Returns the value of attribute to_date.
13 14 15 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 13 def to_date @to_date end |
Instance Method Details
#build_grouped_data ⇒ Object
95 96 97 98 99 100 101 102 |
# 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) ⇒ Object
53 54 55 56 |
# 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) ⇒ Object
143 144 145 146 147 148 149 150 |
# 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) ⇒ Object
135 136 137 |
# 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) ⇒ Object
139 140 141 |
# 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 = { }) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 104 def print_table(data, = { }) 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 [:has_totals] format = widths.collect { |n| "%-#{n}s"}.join(' | ') table.each { |line| printf "| #{format} |\n", *line } end |
#retrieve_jobs(options = {}, &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 58 def retrieve_jobs( = {}, &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) ⇒ Object
130 131 132 133 |
# File 'lib/envoi/utils/mediaconvert_enumerator.rb', line 130 def run(&block) retrieve_jobs(&block) show_summary if @should_show_summary end |
#show_summary ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# 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 |