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

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

#ds_listObject



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

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



125
126
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 125

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

  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



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

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
    STDERR.puts "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_csv 'data_imports/import_csv.json', params, file, '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



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

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



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

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



120
121
122
123
# File 'lib/holistics/api_client.rb', line 120

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

#parse_transport_config(filepath) ⇒ Object



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

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

#send_import(options) ⇒ Object



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

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.new("Failed Import Job #{job_id}: #{res['error']}")
  end
end

#send_transform(options) ⇒ Object



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

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



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

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



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

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