Class: Cf::Production

Inherits:
Thor
  • Object
show all
Includes:
Config
Defined in:
lib/cf/cli/production.rb

Overview

:nodoc: all

Instance Method Summary collapse

Methods included from Config

#config_file, #find_home, #get_api_key, #load_config, #save_config, #set_api_key, #set_target_uri

Instance Method Details

#add_unitsObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/cf/cli/production.rb', line 295

def add_units
  set_target_uri(options[:live])
  set_api_key
  CF. = CF::Account.info['name']
  run_title = options[:run_title].parameterize
  input_data = options[:input_data].presence

  if input_data =~ /^\// #checking absolute input data path
    input_data_file = input_data
  else
    unless File.exist?(input_data)
      say("The input data file named #{input_data} doesn't exist", :red) and return
    end
    input_data_file = "#{Dir.pwd}/#{input_data}"
  end
  units = CF::Run.add_units({:run_title => run_title, :file => input_data_file})
  if units.nil?
    say("Error: Invalid File!", :red) and exit(1)
  else
    if units['error'].present?
      say("Error: #{units['error']['message']}", :red) and exit(1)
    end
    say("\"#{units['successfull']}\"!", :green)
  end
end

#deleteObject



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/cf/cli/production.rb', line 323

def delete
  set_target_uri(false)
  set_api_key
  CF. = CF::Account.info['name']
  run_title = options[:run_title].parameterize

  deleted_run = CF::Run.destroy(run_title)
  if deleted_run['error'].present?
    say("Error: #{deleted_run['error']['message']}", :red) and exit(1)
  end
  say("Run Deleted Successfully entitled: \"#{deleted_run['title']}\"!", :green)
end

#listObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/cf/cli/production.rb', line 207

def list
  set_target_uri(options[:live])
  set_api_key
  CF. = CF::Account.info['name']
  param = {}
  current_page = 1

  if options['line'].present?
    line_title = options['line'].parameterize
    param.merge!({:line_title => line_title})

    if options['all']
      param.merge!({:page => "all"})
      current_page = 1
    end

    if page = options['page'].presence
      param.merge!({:page => page})
      current_page = page
    end

  else
    if options['all']
      param = {:page => "all"}
      current_page = 1
    end

    if page = options['page'].presence
      param.merge!({:page => page})
      current_page = page
    end
  end

  resp_runs = CF::Run.all(param)

  if resp_runs.has_key?('error')
    say("#{resp_runs['error']}", :red) and exit(1)
  end

  if !resp_runs.has_key?("runs") && resp_runs['runs'].nil?
    say("\nRun list is empty.\n", :yellow) and return
  end

  if resp_runs.has_key?("total_runs") && resp_runs['total_runs']==0
    say("\nRun list is empty.\n", :yellow) and return
  end

  if resp_runs['total_pages']
    say("\nShowing page #{current_page} of #{resp_runs['total_pages']} (Total runs: #{resp_runs['total_runs']})")
  end
  runs = resp_runs['runs'].presence
  runs.sort! {|a, b| a['title'] <=> b['title'] }
  runs_table = table do |t|
    t.headings = ["Run Title", 'URL', 'Status']
    runs.each do |run|
      run = Hashie::Mash.new(run)
      t << [run.title, "http://#{CF.}.cloudfactory.com/runs/#{CF.}/#{run.title}", run.status]
    end
  end
  say("\n")
  if runs_table.rows.present?
    say(runs_table)
  else
    say("No production run for line #{line_title}", :yellow)
  end
end

#resumeObject



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/cf/cli/production.rb', line 276

def resume
  set_target_uri(false)
  set_api_key
  CF. = CF::Account.info['name']
  result = CF::Run.resume(options['run_title'].parameterize)

  if result['error'].present?
    say("Error: #{result['error']['message']}", :red) and exit(1)
  end

  # if result.status == "resumed"
  say("Run with title \"#{result['title']}\" is resumed!", :green)
  # end
end

#start(title = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
# File 'lib/cf/cli/production.rb', line 72

def start(title=nil)
  line_destination  = Dir.pwd
  yaml_source       = "#{line_destination}/line.yml"

  set_target_uri(options[:live])
  set_api_key
  CF. = CF::Account.info['name']
  available_balance = CF::Account.info['balance']['available_balance']
  if available_balance.to_f <= 0

    say("****************************************************************************************", :red)
    say("***  Warning! Your account balance is 0. Please recharge your account balance ASAP.  ***", :red)
    say("****************************************************************************************", :red)
  elsif available_balance.to_f <= 500
    say("***********************************************************************************************", :yellow)
    say("*** Warning! Your account balance is #{available_balance.to_s} cf cents. Please recharge your account balance.  ***", :yellow)
    say("***********************************************************************************************", :yellow)
  end


  if options[:line].present?
    line = CF::Line.find(options[:line])
    if line.nil?
      say("Line named #{options[:line]} does not exist !!!", :red) and exit(1)
    else
      line = Hashie::Mash.new(line)
      line_title = options[:line]
    end
  elsif File.exist?("#{yaml_source}")
    line_yaml_dump = YAML::load(File.read(yaml_source).strip)
    line_title = line_yaml_dump['title'].parameterize
    line = CF::Line.find(line_title)
    if line.nil?
      say("Line named #{options[:line]} does not exist !!!", :red) and exit(1)
    else
      line = Hashie::Mash.new(line)
    end
  else
    say("Looks like you're not in the Line directory or did not provide the line title to use the line", :red) and return
  end

  if title.nil?
    if line_title =~  /\w\/\w/
      run_title       = "#{line_title.split("/").last}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
    else
      run_title       = "#{line_title}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
    end
  else
    run_title       = "#{title.parameterize}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
  end

  say("Title is longer than expected. The system accepts only 40 no of characters", :red) and exit(1) if title.length > 40

  input_data = options[:input_data].presence

  if input_data =~ /^\// #checking absolute input data path
    input_data_file = input_data
  else
    if Dir.exist?("#{line_destination}/input")
      input_data_dir = "#{line_destination}/input"
      input_files = Dir["#{input_data_dir}/*.csv"]
      file_count = input_files.size
      case file_count
      when 0
        say("No input data file present inside the input folder", :red) and return
      when 1
        input_data_file = "#{Dir.pwd}/input/#{extract_name(input_files.first)}"
      else
        # Let the user choose the file
        chosen_file = nil
        choose do |menu|
          menu.header = "Input data files"
          menu.prompt = "Please choose which file to be used as input data: "

          input_files.each do |item|
            menu.choice(extract_name(item)) do
              chosen_file = extract_name(item)
              say("Using the file #{chosen_file} as input data")
            end
          end
        end
        input_data_file = "#{Dir.pwd}/input/#{chosen_file}"
      end

      #### zipping csv file
      say("CSV file size is greater than 2 MB. Aborting production start.", :red) and return if (File.size(input_data_file).to_f/(1024*1000) > 2)
      say("Zipping csv file...", :green)
      zipfile_name = "input/input.zip"
      Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
        begin
          zipfile.add("input.csv", input_data_file)
        rescue
          zipfile.remove("input.csv")
          zipfile.add("input.csv", input_data_file)
        end
      end

    else
      unless File.exist?(input_data)
        say("The input data file named #{input_data} doesn't exist", :red) and return
      end
      input_data_file = "#{Dir.pwd}/#{input_data}"
    end
  end

  unless File.exist?(input_data_file)
    say("The input data file named #{input_data} is missing", :red) and return
  end

  gold_standards = options[:gold_standards]
  say "Creating a production run with title #{run_title}", :green
  run = CF::Run.create(line_title, run_title, zipfile_name, gold_standards)
  if run.errors.blank?
    display_success_run(run)
    ## remove the input/input.zip file so that next time when production run is started it wont conflict
    if File.exist?(zipfile_name) then
        FileUtils.rm_rf zipfile_name
      end
  else
    say("Error: #{run.errors}", :red)
  end
end

#start_test(title = nil) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cf/cli/production.rb', line 18

def start_test(title=nil)
  line_destination  = Dir.pwd
  yaml_source       = "#{line_destination}/line.yml"

  set_target_uri(options[:live])
  set_api_key
  CF. = CF::Account.info['name']
  if options[:line].present?
    line = CF::Line.find(options[:line])
    if line.nil?
      say("Line named #{options[:line]} does not exist !!!", :red) and exit(1)
    else
      line = Hashie::Mash.new(line)
      line_title = options[:line]
    end
  elsif File.exist?("#{yaml_source}")
    line_yaml_dump = YAML::load(File.read(yaml_source).strip)
    line_title = line_yaml_dump['title'].parameterize
    line = CF::Line.find(line_title)
    if line.nil?
      say("Line named #{options[:line]} does not exist !!!", :red) and exit(1)
    else
      line = Hashie::Mash.new(line)
    end
  else
    say("Looks like you're not in the Line directory or did not provide the line title to use the line", :red) and return
  end

  if title.nil?
    if line_title =~  /\w\/\w/
      run_title       = "#{line_title.split("/").last}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
    else
      run_title       = "#{line_title}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
    end
  else
    run_title       = "#{title.parameterize}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
  end

  gold_standard_inputs = options[:gold_standards_inputs]
  say "Creating a test production run with title #{run_title}", :green
  run = CF::TestRun.new(line_title, run_title, gold_standard_inputs)
  if run.errors.blank?
    display_success_run(run)
  else
    say("Error: #{run.errors}", :red)
  end

end