Class: Holistics::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/holistics/api_client.rb

Defined Under Namespace

Classes: ImportError

Instance Method Summary collapse

Instance Method Details

#build_submit_params(options) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/holistics/api_client.rb', line 146

def build_submit_params(options)
  params = options.except(:config_path)
  if options[:config_path]
    params[:configs] = parse_transport_config(options[:config_path]).to_json
  end
  params
end

#dbt_upload(filepath, ds_name) ⇒ Object



168
# File 'lib/holistics/api_client.rb', line 168

def dbt_upload(filepath, ds_name); end

#ds_listObject



44
45
46
47
48
49
50
51
52
# File 'lib/holistics/api_client.rb', line 44

def ds_list
  result = http_request.get 'data_sources.json', 'Error retrieving list of data sources'

  table = [%w[ID Type Name]]
  rows = result.map { |record| [record['id'], record['dbtype'], record['name']] }
  table.concat(rows)

  puts TabularFormatter.new(table).to_pretty_table
end

#generate_configs(options) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/holistics/api_client.rb', line 127

def generate_configs(options)
  raise 'Output location is invalid.' unless Dir.exist?(options['output'])

  puts "Generating transport JSON config files to #{options['output']} directory..."

  result = http_request.post_json 'transports/generate_configs.json', options, 'Error generating transport configs'

  result.each do |table_data|
    File.open("#{options['output']}/#{table_data['filename']}", 'w') do |f|
      f.write(JSON.pretty_generate(table_data['json_content']))
    end

    puts "Generated #{table_data['filename']}"
  end

  puts
  puts "Configs generation succeeded with #{result.length} files in total."
end

#import_csv(options) ⇒ Object



15
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
# File 'lib/holistics/api_client.rb', line 15

def import_csv(options)
  local_filepath = options[:filepath]
  dest_ds_id = options[:dest_ds_id]
  dest_fqname = options[:dest_table_name]

  begin
    file = File.open local_filepath
  rescue StandardError
    warn "Could not open file at '#{local_filepath}'."
    puts 'Invalid file path. Please check your file path.'

    return
  end

  puts "Read csv file '#{local_filepath}'."

  params = {
    dest_fqname: dest_fqname,
    dest_ds_id: dest_ds_id
  }

  json = http_request.post_file 'data_imports/import_csv.json', params, file, 'text/csv', 'Error uploading CSV file'

  job_id = json['message']['job_id']

  puts "Job submitted. Job ID: #{job_id}."
  job_manager.tail_logs job_id.to_i
end

#import_listObject



54
55
56
57
58
59
60
61
62
# File 'lib/holistics/api_client.rb', line 54

def import_list
  result = http_request.get 'data_imports.json', 'Error retrieving list of data imports'

  table = [%w[ID Name]]
  rows = result.map { |record| [record['id'], record['title']] }
  table.concat(rows)

  puts TabularFormatter.new(table).to_pretty_table
end

#invoke_email_schedule(es_id, options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/holistics/api_client.rb', line 85

def invoke_email_schedule(es_id, options)
  puts "Invoking email schedule ID #{es_id}"

  result = http_request.post_json "email_schedules/#{es_id}/execute.json", options, 'Error submitting email schedule job'

  job_id = result['job_id']

  puts "Job submitted. Job ID: #{job_id}."
  job_manager.tail_logs job_id
end

#job_show(options) ⇒ Object



122
123
124
125
# File 'lib/holistics/api_client.rb', line 122

def job_show(options)
  puts 'Getting job log...'
  job_manager.tail_logs options[:job_id]
end

#parse_transport_config(filepath) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/holistics/api_client.rb', line 154

def parse_transport_config(filepath)
  file_ext = File.extname(filepath).downcase
  if file_ext == '.json'
    JSON.parse(File.read(filepath))
  elsif file_ext == '.yml'
    YAML.safe_load(File.read(filepath))
  else
    raise StandardError, 'Invalid config file extension. Please use either JSON or YML'
  end
rescue StandardError => e
  warn "Error parsing transport config file: #{e.message}"
  exit 1
end

#send_import(options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/holistics/api_client.rb', line 107

def send_import(options)
  puts 'Invoking import job...'

  result = http_request.post_json "data_imports/#{options[:import_id]}/execute.json", options, 'Error submitting import job'

  job_id = result['job_id']

  puts "Job submitted. Job ID: #{job_id}."
  job_manager.tail_logs job_id
  res = job_manager.fetch_job_results job_id
  unless res['status'] == 'success'
    raise ImportError, "Failed Import Job #{job_id}: #{res['error']}"
  end
end

#send_transform(options) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/holistics/api_client.rb', line 96

def send_transform(options)
  puts 'Invoking transform job...'

  result = http_request.post_json "data_transforms/#{options[:transform_id]}/execute.json", options, 'Error submitting transform job'

  job_id = result['job_id']

  puts "Job submitted. Job ID: #{job_id}."
  job_manager.tail_logs job_id
end

#send_transport(options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/holistics/api_client.rb', line 74

def send_transport(options)
  puts 'Submitting transport job ...'

  params = build_submit_params(options)
  result = http_request.post_json('transports/submit.json', params, 'Error submitting transport job')

  job_id = result['job_id']
  puts "Job submitted. Job ID: #{job_id}."
  job_manager.tail_logs job_id
end

#transform_listObject



64
65
66
67
68
69
70
71
72
# File 'lib/holistics/api_client.rb', line 64

def transform_list
  result = http_request.get 'data_transforms.json', 'Error retrieving list of data transports'

  table = [%w[ID Name]]
  rows = result.map { |record| [record['id'], record['title']] }
  table.concat(rows)

  puts TabularFormatter.new(table).to_pretty_table
end