Class: GoodData::LCM2::UpdateMetricFormats
- Inherits:
-
BaseAction
- Object
- BaseAction
- GoodData::LCM2::UpdateMetricFormats
- Defined in:
- lib/gooddata/lcm/actions/update_metric_formats.rb
Constant Summary collapse
- DESCRIPTION =
'Localize Metric Formats'
- PARAMS =
define_params(self) do description 'Synchronization Info' param :synchronize, array_of(instance_of(Type::SynchronizationInfoType)), required: true, generated: true description 'Client Used for Connecting to GD' param :gdc_gd_client, instance_of(Type::GdClientType), required: true description 'Organization Name' param :organization, instance_of(Type::StringType), required: false description 'DataProduct to manage' param :data_product, instance_of(Type::GDDataProductType), required: false description 'Logger' param :gdc_logger, instance_of(Type::GdLogger), required: true description 'ADS Client' param :ads_client, instance_of(Type::AdsClientType), required: false description 'Input Source' param :input_source, instance_of(Type::HashType), required: false description 'Localization query' param :localization_query, instance_of(Type::StringType), required: false description 'Abort on error' param :abort_on_error, instance_of(Type::StringType), required: false description 'Collect synced status' param :collect_synced_status, instance_of(Type::BooleanType), required: false description 'Sync failed list' param :sync_failed_list, instance_of(Type::HashType), required: false end
- RESULT_HEADER =
%i[action ok_clients error_clients]
Constants inherited from BaseAction
BaseAction::FAILED_CLIENTS, BaseAction::FAILED_PROJECTS, BaseAction::FAILED_SEGMENTS, BaseAction::SYNC_FAILED_LIST
Constants included from Dsl::Dsl
Dsl::Dsl::DEFAULT_OPTS, Dsl::Dsl::TYPES
Class Method Summary collapse
- .call(params) ⇒ Object
- .get_clients_metrics(metric_data) ⇒ Object
- .load_metric_data(params) ⇒ Object
- .validate_input_source(input_source, continue_on_error) ⇒ Object
Methods inherited from BaseAction
add_failed_client, add_failed_project, add_failed_segment, add_new_clients_to_project_client_mapping, check_params, collect_synced_status, continue_on_error, print_result, process_failed_project, process_failed_projects, sync_failed_client, sync_failed_project, sync_failed_segment, without_check
Methods included from Dsl::Dsl
#define_params, #define_type, #process
Class Method Details
.call(params) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/gooddata/lcm/actions/update_metric_formats.rb', line 158 def call(params) data = load_metric_data(params) result = [] return result if data.nil? metric_group = get_clients_metrics(data) return result if metric_group.empty? GoodData.logger.debug("Clients have metrics which will be modified: #{metric_group.keys}") updated_clients = params.synchronize.map { |segment| segment.to.map { |client| client[:client_id] } }.flatten.uniq GoodData.logger.debug("Updating clients: #{updated_clients}") data_product = params.data_product data_product_clients = data_product.clients number_client_ok = 0 number_client_error = 0 collect_synced_status = collect_synced_status(params) metric_group.each do |client_id, formats| next if !updated_clients.include?(client_id) || (collect_synced_status && sync_failed_client(client_id, params)) client = data_product_clients.find { |c| c.id == client_id } begin GoodData.logger.info("Start updating metric format for client: '#{client_id}'") metrics = client.project.metrics.to_a formats.each do |tag, format| next if tag.blank? || format.blank? metrics_to_be_updated = metrics.select { |metric| metric..include?(tag) } metrics_to_be_updated.each do |metric| metric.format = format metric.save end end number_client_ok += 1 GoodData.logger.info("Finished updating metric format for client: '#{client_id}'") rescue StandardError => e number_client_error += 1 GoodData.logger.warn("Failed to update metric format for client: '#{client_id}'. Error: #{e.} - #{e}") end end [{ :action => 'Update metric format', :ok_clients => number_client_ok, :error_clients => number_client_error }] end |
.get_clients_metrics(metric_data) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gooddata/lcm/actions/update_metric_formats.rb', line 143 def get_clients_metrics(metric_data) return {} if metric_data.nil? metric_groups = {} clients = metric_data.map { |row| row[:client_id] }.uniq clients.each do |client| next if client.blank? formats = {} metric_data.select { |row| row[:client_id] == client && row[:tag].present? && row[:format].present? }.each { |row| formats[row[:tag]] = row[:format] } metric_groups[client.to_s] ||= formats end metric_groups end |
.load_metric_data(params) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gooddata/lcm/actions/update_metric_formats.rb', line 52 def load_metric_data(params) collect_synced_status = collect_synced_status(params) if params&.dig(:input_source, :metric_format) && params[:input_source][:metric_format].present? metric_input_source = validate_input_source(params[:input_source], collect_synced_status) return nil unless metric_input_source else return nil end metric_data_source = GoodData::Helpers::DataSource.new(metric_input_source) begin temp_csv = without_check(PARAMS, params) do File.open(metric_data_source.realize(params), 'r:UTF-8') end rescue StandardError => e GoodData.logger.warn("Unable to get metric input source, skip updating metric formats. Error: #{e.} - #{e}") return nil end metrics_hash = GoodData::Helpers::Csv.read_as_hash temp_csv return nil if metrics_hash.empty? expected_keys = %w[tag client_id format] unless expected_keys.map(&:to_sym).all? { |s| metrics_hash.first.key? s } GoodData.logger.warn("The input metric data is incorrect, expecting the following fields: #{expected_keys}") return nil end metrics_hash end |
.validate_input_source(input_source, continue_on_error) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/gooddata/lcm/actions/update_metric_formats.rb', line 83 def validate_input_source(input_source, continue_on_error) type = input_source[:type] if input_source&.dig(:type) metric_format = input_source[:metric_format] if type.blank? raise "Incorrect configuration: 'type' of 'input_source' is required" unless continue_on_error return nil end modified_input_source = input_source case type when 'ads', 'redshift', 'snowflake', 'bigquery', 'postgresql', 'mssql', 'mysql' if metric_format[:query].blank? GoodData.logger.warn("The metric input_source '#{type}' is missing property 'query'") return nil end modified_input_source[:query] = metric_format[:query] return modified_input_source when 's3' if metric_format[:file].blank? GoodData.logger.warn("The metric input_source '#{type}' is missing property 'file'") return nil end if modified_input_source.key?(:key) modified_input_source[:key] = metric_format[:file] else modified_input_source[:file] = metric_format[:file] end return modified_input_source when 'blobStorage' if metric_format[:file].blank? GoodData.logger.warn("The metric input_source '#{type}' is missing property 'file'") return nil end modified_input_source[:file] = metric_format[:file] return modified_input_source when 'staging' if metric_format[:file].blank? GoodData.logger.warn("The metric input_source '#{type}' is missing property 'file'") return nil end modified_input_source[:path] = metric_format[:file] return modified_input_source when 'web' if metric_format[:url].blank? GoodData.logger.warn("The metric input_source '#{type}' is missing property 'url'") return nil end modified_input_source[:url] = metric_format[:url] return modified_input_source else return nil end end |