Class: GoodData::LCM2::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/gooddata/lcm/helpers/tags_helper.rb,
lib/gooddata/lcm/helpers/check_helper.rb,
lib/gooddata/lcm/helpers/safe_failure_helper.rb,
lib/gooddata/lcm/helpers/release_table_helper.rb

Constant Summary collapse

ABORT_ON_ERROR_PARAM =
'abort_on_error'.to_sym
COLLECT_SYNCED_STATUS =
'collect_synced_status'.to_sym
DEFAULT_TABLE_NAME =
'LCM_RELEASE'
DEFAULT_NFS_DIRECTORY =
'/release-tables'

Class Method Summary collapse

Class Method Details

.check_params(specification, params) ⇒ Object



16
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
# File 'lib/gooddata/lcm/helpers/check_helper.rb', line 16

def check_params(specification, params)
  specification.keys.each do |param_name|
    value = params.send(param_name)
    type = specification[param_name][:type]
    if value.nil? || (value.is_a?(String) && value.empty?)
      if !specification[param_name][:opts][:default].nil?
        if specification.select { |x| specification[x][:opts][:replacement] == param_name }.first.nil?
          params[param_name] = specification[param_name][:opts][:default]
        else
          GoodData.logger.warn "WARNING: Default value for parameter '#{param_name}' was not filled because deprecated parameter is used instead."
        end
      elsif specification[param_name][:opts][:required]
        fail_if_development "Mandatory parameter '#{param_name}' of type '#{type}' is not specified"
      end
    else
      if type.class.const_get(:CATEGORY) == :complex && !value.is_a?(Hash)
        fail_if_development "Expected parameter '#{param_name}' to be kind of '#{type}', got '#{value.class.name}'"
      end

      if specification[param_name][:opts][:deprecated]
        GoodData.logger.warn("WARNING: Parameter '#{param_name}' is deprecated. Please use '#{specification[param_name][:opts][:replacement]}' instead.")
      end

      unless type.check(value)
        fail_if_development "Parameter '#{param_name}' has invalid type, expected: #{type}, got #{value.class}"
      end
    end
  end
end

.collect_synced_status(params) ⇒ Object



50
51
52
# File 'lib/gooddata/lcm/helpers/check_helper.rb', line 50

def collect_synced_status(params)
  params.include?(COLLECT_SYNCED_STATUS) && to_bool(COLLECT_SYNCED_STATUS, params[COLLECT_SYNCED_STATUS])
end

.continue_on_error(params) ⇒ Object



46
47
48
# File 'lib/gooddata/lcm/helpers/check_helper.rb', line 46

def continue_on_error(params)
  params.include?(ABORT_ON_ERROR_PARAM) && !to_bool(ABORT_ON_ERROR_PARAM, params[ABORT_ON_ERROR_PARAM])
end

.delete_master_project_from_ads(release_table_name, ads_client, segment_id, removal_master_project_ids) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 43

def delete_master_project_from_ads(release_table_name, ads_client, segment_id, removal_master_project_ids)
  replacements = {
    table_name: release_table_name || DEFAULT_TABLE_NAME,
    segment_id: segment_id,
    master_project_ids: removal_master_project_ids.map { |x| "'#{x}'" } * ', '
  }

  path = File.expand_path('../data/delete_from_lcm_release.sql.erb', __dir__)
  query = GoodData::Helpers::ErbHelper.template_file(path, replacements)

  ads_client.execute(query)
end

.fail_if_development(msg) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/gooddata/lcm/helpers/safe_failure_helper.rb', line 9

def fail_if_development(msg)
  if ENV['RSPEC_ENV'] == 'test'
    fail msg
  else
    GoodData.logger.error msg
  end
end

.get_master_project_list_from_ads(release_table_name, ads_client, segment_id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 29

def get_master_project_list_from_ads(release_table_name, ads_client, segment_id)
  replacements = {
    table_name: release_table_name || DEFAULT_TABLE_NAME,
    segment_id: segment_id
  }

  path = File.expand_path('../../data/select_from_lcm_release.sql.erb', __FILE__)
  query = GoodData::Helpers::ErbHelper.template_file(path, replacements)

  res = ads_client.execute_select(query)
  sorted = res.sort_by { |row| row[:version] }
  sorted
end

.get_master_project_list_from_nfs(domain_id, data_product_id, segment_id) ⇒ Object



56
57
58
59
60
61
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 56

def get_master_project_list_from_nfs(domain_id, data_product_id, segment_id)
  file_path = path_to_release_table_file(domain_id, data_product_id, segment_id)
  data = GoodData::Helpers::Csv.read_as_hash(file_path)
  sorted = data.sort_by { |master| master[:version] }
  sorted
end

.latest_master_project_from_ads(release_table_name, ads_client, segment_id) ⇒ Object



14
15
16
17
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 14

def latest_master_project_from_ads(release_table_name, ads_client, segment_id)
  sorted = get_master_project_list_from_ads(release_table_name, ads_client, segment_id)
  sorted.last
end

.latest_master_project_from_nfs(domain_id, data_product_id, segment_id) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 19

def latest_master_project_from_nfs(domain_id, data_product_id, segment_id)
  file_path = path_to_release_table_file(domain_id, data_product_id, segment_id)
  sorted = get_master_project_list_from_nfs(domain_id, data_product_id, segment_id)
  latest_master_project = sorted.last

  version_info = latest_master_project ? "master_pid=#{latest_master_project[:master_project_id]} version=#{latest_master_project[:version]}" : ""
  GoodData.gd_logger.info "Getting latest master project: file=#{file_path} domain=#{domain_id} data_product=#{data_product_id} segment=#{segment_id} #{version_info}"
  latest_master_project
end

.parse_production_tags(production_tags, segment_production_tags) ⇒ Object

Returns Array of production tags.

Examples:

['tag1', 'tag2']

Parameters:

  • production_tags

    Global production tags

  • segment_production_tags

    Segment-specific production tags

Returns:

  • Array of production tags



26
27
28
29
30
31
32
# File 'lib/gooddata/lcm/helpers/tags_helper.rb', line 26

def parse_production_tags(production_tags, segment_production_tags)
  separator = ','
  tags = segment_production_tags || production_tags
  return [] unless tags
  tags = tags.split(separator).map(&:strip) unless tags.is_a?(Array)
  tags
end

.path_to_release_table_file(domain_id, data_prod_id, segment_id) ⇒ Object



83
84
85
86
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 83

def path_to_release_table_file(domain_id, data_prod_id, segment_id)
  nsf_directory = ENV['RELEASE_TABLE_NFS_DIRECTORY'] || DEFAULT_NFS_DIRECTORY
  [nsf_directory, domain_id, data_prod_id + '-' + segment_id + '.csv'].join('/')
end

.segment_production_tags(segments) ⇒ Object

Returns Hash of segments and production tags.

Examples:

{ { segment: 'tag1, tag1' } }

Parameters:

  • Array

    of segments as in release brick parameters

Returns:

  • Hash of segments and production tags



14
15
16
17
18
19
20
# File 'lib/gooddata/lcm/helpers/tags_helper.rb', line 14

def segment_production_tags(segments)
  return {} unless segments
  segments
    .reject { |s| s.production_tags.nil? }
    .map { |s| [s.segment_id, s.production_tags] }
    .to_h
end

.to_bool(key, value) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
# File 'lib/gooddata/lcm/helpers/check_helper.rb', line 54

def to_bool(key, value)
  return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
  return true if value =~ /^(true|t|yes|y|1)$/i
  return false if value == '' || value =~ /^(false|f|no|n|0)$/i

  raise ArgumentError, "Invalid '#{value}' boolean value for '#{key}' parameter"
end

.update_latest_master_to_nfs(domain_id, data_product_id, segment_id, master_pid, version) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 63

def update_latest_master_to_nfs(domain_id, data_product_id, segment_id, master_pid, version)
  file_path = path_to_release_table_file(domain_id, data_product_id, segment_id)
  GoodData.gd_logger.info "Updating release table: file=#{file_path} domain=#{domain_id} data_product=#{data_product_id} segment=#{segment_id} master_pid=#{master_pid} version=#{version}" # rubocop:disable Metrics/LineLength
  GoodData::Helpers::Csv.ammend_line(
    file_path,
    master_project_id: master_pid,
    version: version
  )
end

.update_master_project_to_nfs(domain_id, data_product_id, segment_id, data) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/gooddata/lcm/helpers/release_table_helper.rb', line 73

def update_master_project_to_nfs(domain_id, data_product_id, segment_id, data)
  file_path = path_to_release_table_file(domain_id, data_product_id, segment_id)
  FileUtils.mkpath(file_path.split('/')[0...-1].join('/'))
  CSV.open(file_path, 'w', write_headers: true, headers: data.first.keys) do |csv|
    data.each do |r|
      csv << r.values
    end
  end
end