Class: Kaya::Results::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/kaya/results/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_for_result) ⇒ Result

data_for_result =

:suite_name,
:execution_name,
:type:


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
66
67
68
69
# File 'lib/kaya/results/result.rb', line 38

def initialize data_for_result

  if data_for_result["_id"]
    # It comes from mongo because there is a result id
    @id                       = data_for_result["_id"]
    load_values(data_for_result)

  else # It comes from a new execution request
    @id = Kaya::Database::MongoConnector.generate_id
    @suite                    = data_for_result['suite']
    @execution_name           = data_for_result["execution_name"] || ""
    @custom_params            = data_for_result["custom_params"]
    @git_log                  = data_for_result["git_log"]
    @started_at               = now_in_seconds
    @finished_at              = nil
    @status                   = "started"
    @timeout                  = nil
    @show_as                  = "pending"
    @html_report              = ""
    @summary                  = "Not available yet"

    # Save suite info
    suite_data                = Kaya::Database::MongoConnector.suite_data_for(@suite["id"])
    @command                  = data_for_result['command']
    @suite_name               = suite_data["name"]
    @command                  = suite_data["command"]
    @console_output           = ""
    @last_check_time          = now_in_seconds
    @execution_data           = {}
    @configuration_values     = Kaya::Support::Configuration.pretty_configuration_values
  end
end

Instance Attribute Details

#bundle_outputObject

Returns the value of attribute bundle_output.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def bundle_output
  @bundle_output
end

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def command
  @command
end

#configuration_valuesObject

Returns the value of attribute configuration_values.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def configuration_values
  @configuration_values
end

#console_outputObject

Returns the value of attribute console_output.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def console_output
  @console_output
end

#console_output_file_nameObject

Returns the value of attribute console_output_file_name.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def console_output_file_name
  @console_output_file_name
end

#custom_paramsObject

Returns the value of attribute custom_params.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def custom_params
  @custom_params
end

#execution_dataObject

Returns the value of attribute execution_data.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def execution_data
  @execution_data
end

#execution_nameObject

Returns the value of attribute execution_name.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def execution_name
  @execution_name
end

#finished_atObject

Returns the value of attribute finished_at.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def finished_at
  @finished_at
end

#git_logObject

Returns the value of attribute git_log.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def git_log
  @git_log
end

#html_reportObject

Returns the value of attribute html_report.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def html_report
  @html_report
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def id
  @id
end

#kaya_commandObject

Returns the value of attribute kaya_command.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def kaya_command
  @kaya_command
end

#kaya_report_file_nameObject

Returns the value of attribute kaya_report_file_name.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def kaya_report_file_name
  @kaya_report_file_name
end

#last_check_timeObject

Returns the value of attribute last_check_time.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def last_check_time
  @last_check_time
end

#pidObject

Returns the value of attribute pid.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def pid
  @pid
end

#sawObject

Returns the value of attribute saw.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def saw
  @saw
end

#show_asObject

Returns the value of attribute show_as.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def show_as
  @show_as
end

#started_atObject

Returns the value of attribute started_at.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def started_at
  @started_at
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def status
  @status
end

#suiteObject

Returns the value of attribute suite.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def suite
  @suite
end

#suite_nameObject

Returns the value of attribute suite_name.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def suite_name
  @suite_name
end

#summaryObject

Returns the value of attribute summary.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def summary
  @summary
end

#timeoutObject

Returns the value of attribute timeout.



5
6
7
# File 'lib/kaya/results/result.rb', line 5

def timeout
  @timeout
end

Class Method Details

.get(result_id) ⇒ Object



79
80
81
82
# File 'lib/kaya/results/result.rb', line 79

def self.get(result_id)
  result_data = Kaya::Database::MongoConnector.result_data_for_id(result_id)
  new(result_data) if result_data
end

Instance Method Details

#add_execution_data(key, value) ⇒ Object



143
144
145
146
# File 'lib/kaya/results/result.rb', line 143

def add_execution_data key, value
  @execution_data.store(key, value)
  self.save!
end

#api_responseObject



113
114
115
116
117
118
119
120
# File 'lib/kaya/results/result.rb', line 113

def api_response
  data = result_data_structure
  data["has_report"] = self.has_report?
  data["elapsed_time"] = self.elapsed_time
  # List of fields to omit in api response
  ["html_report","console_output_file_name","kaya_command","kaya_report_file_name","pid","last_check_time","console_output","git_log","bundle_output"].each{|field| data.delete(field)}
  data
end

#append_result_to_console_output!Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/kaya/results/result.rb', line 270

def append_result_to_console_output!
  if is_there_console_output_file?
    begin
      output = []
      open_console_output_file.each_line do |line|
        output << line
      end
      text = output.join "\n"
      if (text.size > @console_output.size )
        save_console_output(text)
        @last_check_time = now_in_seconds
      end
      true
    rescue
      false
    end
  end
end

#append_to_console_output(text) ⇒ Object

Append text to console output

Parameters:

  • text (String)

    the text to be appended



311
312
313
314
# File 'lib/kaya/results/result.rb', line 311

def append_to_console_output text
  @console_output += text
  self.save!
end

#custom_params_valuesString

Returns the string of custom params values

Returns:

  • (String)

    foo=bar john=doe



124
125
126
# File 'lib/kaya/results/result.rb', line 124

def custom_params_values
  "kaya_custom_params='#{validate_params(@custom_params).to_json}'".gsub(',', ', ')
end

#delete_asociated_files!Object



441
442
443
444
445
# File 'lib/kaya/results/result.rb', line 441

def delete_asociated_files!
  delete_console_output_file!
  delete_kaya_report_file!
  delete_copy_kaya_report_file!
end

#delete_console_output_file!Object



412
413
414
415
416
417
418
419
420
# File 'lib/kaya/results/result.rb', line 412

def delete_console_output_file!
  begin
    File.delete("#{Dir.pwd}/kaya/temp/#{console_output_file_name}")
    $K_LOG.debug "[#{@id}] Console output files deleted" if $K_LOG
    true
  rescue => e
    false
  end
end

#delete_copy_kaya_report_file!Object



432
433
434
435
436
437
438
439
# File 'lib/kaya/results/result.rb', line 432

def delete_copy_kaya_report_file!
  begin
    File.delete("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~")
    true
  rescue => e
    false
  end
end

#delete_kaya_report_file!Object



422
423
424
425
426
427
428
429
430
# File 'lib/kaya/results/result.rb', line 422

def delete_kaya_report_file!
  begin
    File.delete("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}")
    $K_LOG.debug "[#{@id}] Report files deleted" if $K_LOG
    true
  rescue => e
    false
  end
end

#elapsed_timeObject



453
454
455
# File 'lib/kaya/results/result.rb', line 453

def elapsed_time
  (finished_at || now_in_seconds) - started_at
end

#finished!Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/kaya/results/result.rb', line 329

def finished!
  @finished_at= now_in_seconds
  @status = "finished"
  save_report!
  get_summary!
  @summary = @status if @summary == "running"
  $K_LOG.debug "[#{@id}] Executuion finished" if $K_LOG
  self.save!
  begin
    $NOTIF.execution_finished self
  rescue => e
    $K_LOG.error "Error at notifying #{e}"
  end
  @summary
end

#finished?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/kaya/results/result.rb', line 363

def finished?
  @status =~ /(finished|stopped)/i
end

#finished_at_formattedString

Returns the finished at time attribute in a format way (configured on kaya info)

Returns:

  • (String)


473
474
475
# File 'lib/kaya/results/result.rb', line 473

def finished_at_formatted
  Kaya::Support::TimeHelper.formatted_time_for @finished_at
end

#finished_by_timeout!Object



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/kaya/results/result.rb', line 345

def finished_by_timeout!
  reason = "Inactivity Timeout reached"
  reset!(reason)
  @timeout = "#{Kaya::Support::Configuration.execution_time_to_live}"
  # @summary = @status if @summary == "running"
  $K_LOG.debug "[#{@id}] Finished by timeout (#{Kaya::Support::Configuration.execution_time_to_live} sec)" if $K_LOG
  begin
    $NOTIF.execution_stopped self, "#{reason} - (@timeout) sec"
  rescue => e
    $K_LOG.error "Error at notifying #{e}"
  end
  self.save!
end

#get_status!Object

tries to get status



263
264
265
266
267
268
# File 'lib/kaya/results/result.rb', line 263

def get_status!

  value = Kaya::View::Parser.get_status(read_report) if is_there_a_report_file?

  @status = @show_as = value if value
end

#get_summary!Object



213
214
215
216
217
218
219
220
221
# File 'lib/kaya/results/result.rb', line 213

def get_summary!
  report = if is_there_a_report_file?
    read_report
  else
    @html_report
  end
  @summary = Kaya::View::Parser.extract_summary(report) unless summary?
  self.save!
end

#has_report?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/kaya/results/result.rb', line 209

def has_report?
  !@html_report.empty?
end

#has_scenario_executed?Boolean

Returns:

  • (Boolean)


402
403
404
# File 'lib/kaya/results/result.rb', line 402

def has_scenario_executed?
  Kaya::View::Parser.has_scenarios_executed? @html_report
end

#has_summary?Boolean

Returns:

  • (Boolean)


394
395
396
# File 'lib/kaya/results/result.rb', line 394

def has_summary?
  report_has_summary?
end

#is_finished?Boolean

Returns:

  • (Boolean)


375
# File 'lib/kaya/results/result.rb', line 375

def is_finished?; self.finished?; end

#is_there_a_report_file?Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
# File 'lib/kaya/results/result.rb', line 201

def is_there_a_report_file?
  begin
    !open_report_file.nil?
  rescue
    false
  end
end

#is_there_console_output_file?Boolean

Returns:

  • (Boolean)


290
291
292
293
294
295
296
# File 'lib/kaya/results/result.rb', line 290

def is_there_console_output_file?
  begin
    open_console_output_file and true
  rescue
    false
  end
end

#load_values(data) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/kaya/results/result.rb', line 71

def load_values data
  data.each_pair do |var, value|
    begin
      send("#{var}=",value) if send("#{var}").nil?
    rescue; end
  end
end

#mark_as_saw!Object



406
407
408
409
410
# File 'lib/kaya/results/result.rb', line 406

def mark_as_saw!
  @saw = true
  $K_LOG.debug "[#{@id}] Marked as saw" if $K_LOG
  self.save!
end

#nowTime

Returns actal timestamp

Returns:

  • (Time)


461
462
463
# File 'lib/kaya/results/result.rb', line 461

def now
  Time.now.localtime
end

#now_in_secondsObject

Returns the timestamp in seconds

Parameters:

  • timestamp (Fixnum)


479
480
481
# File 'lib/kaya/results/result.rb', line 479

def now_in_seconds
  now.to_i
end

#open_console_output_fileObject



298
299
300
301
302
303
304
305
306
307
# File 'lib/kaya/results/result.rb', line 298

def open_console_output_file
  begin
    FileUtils.cp("#{Dir.pwd}/kaya/temp/#{console_output_file_name}", "#{Dir.pwd}/kaya/temp/#{console_output_file_name}~")
    file_content = File.open "#{Dir.pwd}/kaya/temp/#{console_output_file_name}~", "r"
    File.delete("#{Dir.pwd}/kaya/temp/#{console_output_file_name}~")
  rescue Errno::ENOENT
    false
  end
  file_content
end

#open_report_fileObject



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/kaya/results/result.rb', line 240

def open_report_file
  begin
    FileUtils.cp("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}", "#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~")
    file_content = File.open "#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~", "r"

    File.delete("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~")

  rescue
    false
  end
  file_content
end

#process_finished?Boolean

Returns:

  • (Boolean)


367
368
369
# File 'lib/kaya/results/result.rb', line 367

def process_finished?
  ! process_running?
end

#process_running?Boolean

Returns:

  • (Boolean)


371
372
373
# File 'lib/kaya/results/result.rb', line 371

def process_running?
  Kaya::Support::Processes.process_running? pid
end

#read_reportString

Returns the html report file created by KAYA

Parameters:

  • html (String)

    report file name

Returns:

  • (String)

    html code read from report



230
231
232
233
234
235
236
237
238
# File 'lib/kaya/results/result.rb', line 230

def read_report
  begin
    content = ''
    open_report_file.each_line do |line|
      content+= line
    end
  rescue; end
  content
end

#report_has_summary?Boolean

Returns:

  • (Boolean)


398
399
400
# File 'lib/kaya/results/result.rb', line 398

def report_has_summary?
  @summary.include? "scenario"
end

#report_says_finished?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'lib/kaya/results/result.rb', line 377

def report_says_finished?
  Kaya::View::Parser.finished_statement? @html_report
end

#reset!(reason = nil) ⇒ Object



385
386
387
388
389
390
391
392
# File 'lib/kaya/results/result.rb', line 385

def reset! reason=nil
  status_text = "stopped"
  status_text += " (#{reason})" if reason
  self.status= self.summary= status_text
  self.finished_at = now_in_seconds
  $K_LOG.debug "[#{@id}] Execution stoppped (reset)" if $K_LOG
  self.save!
end

#result_data_structureObject



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
# File 'lib/kaya/results/result.rb', line 85

def result_data_structure
  {
    "_id"                       => id,
    "suite"                     => @suite,
    "execution_name"            => execution_name,
    "command"                   => command,
    "custom_params"             => custom_params,
    "kaya_command"              => kaya_command,
    "kaya_report_file_name"     => kaya_report_file_name,
    "html_report"               => html_report,
    "started_at"                => started_at,
    "finished_at"               => finished_at,
    "status"                    => status,
    "timeout"                   => timeout,
    "summary"                   => summary,
    "show_as"                   => show_as,
    "bundle_output"             => bundle_output,
    "console_output_file_name"  => console_output_file_name,
    "console_output"            => console_output,
    "git_log"                   => git_log,
    "saw"                       => saw,
    "pid"                       => pid,
    "last_check_time"           => last_check_time,
    "execution_data"            => execution_data,
    "configuration_values"      => configuration_values
  }
end

#running!Object



253
254
255
256
# File 'lib/kaya/results/result.rb', line 253

def running!
  @status = "running"
  $K_LOG.debug "[#{@id}] Setted as running" if $K_LOG
end

#running?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/kaya/results/result.rb', line 258

def running?
  @status == "running"
end

#save!Boolean

Persists the status of the object on mongo. If result exist update it, else creates it

Returns:

  • (Boolean)

    operation result



486
487
488
489
# File 'lib/kaya/results/result.rb', line 486

def save!
  Kaya::Database::MongoConnector.result_data_for_id(id) ? Kaya::Database::MongoConnector.update_result(result_data_structure) : Kaya::Database::MongoConnector.insert_result(result_data_structure)
  $K_LOG.debug "[#{@id}] Result saved" if $K_LOG
end

#save_console_output(text) ⇒ Object

Save console output text

Parameters:

  • text (String)

    the text to be appended



324
325
326
327
# File 'lib/kaya/results/result.rb', line 324

def save_console_output text
  @console_output = text
  self.save!
end

#save_report!Object

Reads, copy html report from cucumber output file and saves it on a instance variable Persists changes on mongo and then deletes the html reporte file



190
191
192
193
194
195
196
197
198
199
# File 'lib/kaya/results/result.rb', line 190

def save_report!
  if is_there_a_report_file?
    new_content = Kaya::View::Parser.adapt_to_kaya(read_report, self)
    if new_content.size > @html_report.size
      @html_report= new_content
      $K_LOG.debug "[#{@id}] Report saved" if $K_LOG
      self.save!
    end
  end
end

#save_to_bundle_output(text) ⇒ Object



316
317
318
319
# File 'lib/kaya/results/result.rb', line 316

def save_to_bundle_output text
  @bundle_output = text
  self.save!
end

#seconds_without_changesObject

Returns the seconds that there is no console output changes only if it is not finished, else returns 0 This is aimed to help to detect if execution is freezed (or may have a debugger statement)



449
450
451
# File 'lib/kaya/results/result.rb', line 449

def seconds_without_changes
  (self.finished? or self.stopped?) ? 0 : (now_in_seconds - @last_check_time)
end

#started?Boolean

Returns:

  • (Boolean)


359
360
361
# File 'lib/kaya/results/result.rb', line 359

def started?
  @status == "started"
end

#started_at_formattedString

Returns the started at time attribute in a format way (configured on kaya info)

Returns:

  • (String)


467
468
469
# File 'lib/kaya/results/result.rb', line 467

def started_at_formatted
  Kaya::Support::TimeHelper.formatted_time_for @started_at
end

#stopped?Boolean

Returns:

  • (Boolean)


381
382
383
# File 'lib/kaya/results/result.rb', line 381

def stopped?
  @status =~ /(reset|stopped)/i
end

#suite_idObject



148
149
150
# File 'lib/kaya/results/result.rb', line 148

def suite_id
  @suite["id"]
end

#summary?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/kaya/results/result.rb', line 223

def summary?
  !(["Not available yet", "running"].include? @summary)
end

#update_values!Boolean

Gets all the console log, status, etc values and update itself If detect report as finished kill the asociated process and return true else returns false wich means that the process is still runnnig

Returns:

  • (Boolean)

    true if process has been killed



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
# File 'lib/kaya/results/result.rb', line 160

def update_values!
  $K_LOG.debug "[#{@id}] Updating values" if $K_LOG
  self.save_report!
  self.get_summary!
  self.get_status!
  self.append_result_to_console_output!
  if (self.report_says_finished? and !self.stopped?)
    self.finished!
    $K_LOG.debug "[#{@id}] Values updated" if $K_LOG
    finished = true

  elsif (self.seconds_without_changes > Kaya::Support::Configuration.execution_time_to_live)
    self.finished_by_timeout!
    finished = true
  else
    return false
  end

  if finished
    @summary = @status if @summary == "running"
    self.save!
    self.delete_asociated_files!
    Kaya::Support::Processes.kill_by_result_id(self.id)
    true
  end
end

#validate_params(custom_params = {}) ⇒ hash

Returns a hash with valid parameters. This is to prevent command line command with could cause problems

Parameters:

  • custom (hash)

    params

Returns:

  • (hash)

    validated custom params



132
133
134
135
136
137
138
139
140
141
# File 'lib/kaya/results/result.rb', line 132

def validate_params custom_params={}
  unless custom_params.nil?
    validated = custom_params.select do |key, value|
      unless value.nil?
        Kaya::Support::Risk.secure? value
      end
    end
  end
  validated || {}
end