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:


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

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
    @ip                       = data_for_result["ip"]
  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

#ipObject

Returns the value of attribute ip.



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

def ip
  @ip
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



81
82
83
84
# File 'lib/kaya/results/result.rb', line 81

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



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

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

#api_responseObject



116
117
118
119
120
121
122
123
# File 'lib/kaya/results/result.rb', line 116

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



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

def append_result_to_console_output!
  $K_LOG.debug "console retrived #{Time.now.to_i}" if $K_LOG
  $K_LOG.debug "without changes #{self.seconds_without_changes}" if $K_LOG
  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
        self.save!
      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



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

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



127
128
129
# File 'lib/kaya/results/result.rb', line 127

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

#delete_asociated_files!Object



446
447
448
449
450
# File 'lib/kaya/results/result.rb', line 446

def delete_asociated_files!
  delete_console_output_file!
  delete_kaya_report_file!
  delete_copy_kaya_report_file!
end

#delete_console_output_file!Object



417
418
419
420
421
422
423
424
425
# File 'lib/kaya/results/result.rb', line 417

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



437
438
439
440
441
442
443
444
# File 'lib/kaya/results/result.rb', line 437

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



427
428
429
430
431
432
433
434
435
# File 'lib/kaya/results/result.rb', line 427

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



458
459
460
# File 'lib/kaya/results/result.rb', line 458

def elapsed_time
  (finished_at || now_in_seconds) - started_at
end

#finished!Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/kaya/results/result.rb', line 334

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)


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

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)


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

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

#finished_by_timeout!Object



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/kaya/results/result.rb', line 350

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



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

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



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

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)


211
212
213
# File 'lib/kaya/results/result.rb', line 211

def has_report?
  !@html_report.empty?
end

#has_scenario_executed?Boolean

Returns:

  • (Boolean)


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

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

#has_summary?Boolean

Returns:

  • (Boolean)


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

def has_summary?
  report_has_summary?
end

#is_finished?Boolean

Returns:

  • (Boolean)


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

def is_finished?; self.finished?; end

#is_there_a_report_file?Boolean

Returns:

  • (Boolean)


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

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

#is_there_console_output_file?Boolean

Returns:

  • (Boolean)


295
296
297
298
299
300
301
# File 'lib/kaya/results/result.rb', line 295

def is_there_console_output_file?
  begin
    open_console_output_file and true
  rescue
    false
  end
end

#load_values(data) ⇒ Object



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

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



411
412
413
414
415
# File 'lib/kaya/results/result.rb', line 411

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)


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

def now
  Time.now.localtime
end

#now_in_secondsObject

Returns the timestamp in seconds

Parameters:

  • timestamp (Fixnum)


484
485
486
# File 'lib/kaya/results/result.rb', line 484

def now_in_seconds
  now.to_i
end

#open_console_output_fileObject



303
304
305
306
307
308
309
310
311
312
# File 'lib/kaya/results/result.rb', line 303

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



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

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)


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

def process_finished?
  ! process_running?
end

#process_running?Boolean

Returns:

  • (Boolean)


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

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



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

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)


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

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

#report_says_finished?Boolean

Returns:

  • (Boolean)


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

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

#reset!(reason = nil) ⇒ Object



390
391
392
393
394
395
396
397
# File 'lib/kaya/results/result.rb', line 390

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



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

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,
    "ip"                        => ip
  }
end

#running!Object



255
256
257
258
# File 'lib/kaya/results/result.rb', line 255

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

#running?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/kaya/results/result.rb', line 260

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



491
492
493
494
# File 'lib/kaya/results/result.rb', line 491

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



329
330
331
332
# File 'lib/kaya/results/result.rb', line 329

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



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

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



321
322
323
324
# File 'lib/kaya/results/result.rb', line 321

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 has a debugger statement)



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

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

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  @status == "started"
end

#started_at_formattedString

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

Returns:

  • (String)


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

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

#stopped?Boolean

Returns:

  • (Boolean)


386
387
388
# File 'lib/kaya/results/result.rb', line 386

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

#suite_idObject



151
152
153
# File 'lib/kaya/results/result.rb', line 151

def suite_id
  @suite["id"]
end

#summary?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/kaya/results/result.rb', line 225

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



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

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



135
136
137
138
139
140
141
142
143
144
# File 'lib/kaya/results/result.rb', line 135

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