Class: MCollective::Util::Playbook::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/util/playbook/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(playbook) ⇒ Report

Returns a new instance of Report.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mcollective/util/playbook/report.rb', line 7

def initialize(playbook)
  @playbook = playbook
  @logs = []
  @timestamp = Time.now.utc
  @version = 1
  @nodes = {}
  @tasks = []
  @success = false
  @final = false

  @metrics = {
    "start_time" => @timestamp,
    "end_time" => @timestamp,
    "run_time" => 0,
    "task_count" => 0,
    "task_types" => {}
  }

  @inputs = {
    "static" => {},
    "dynamic" => []
  }
end

Instance Attribute Details

#timestampObject (readonly)

Returns the value of attribute timestamp.



5
6
7
# File 'lib/mcollective/util/playbook/report.rb', line 5

def timestamp
  @timestamp
end

Instance Method Details

#append_log(time, level, from, msg) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/mcollective/util/playbook/report.rb', line 141

def append_log(time, level, from, msg)
  @logs << {
    "time" => time.utc,
    "level" => level.to_s,
    "from" => from.strip,
    "msg" => msg
  }
end

#calculate_metricsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mcollective/util/playbook/report.rb', line 79

def calculate_metrics
  @metrics["end_time"] = Time.now.utc
  @metrics["run_time"] = @metrics["end_time"] - @metrics["start_time"]
  @metrics["task_count"] = @tasks.size

  @tasks.each do |task|
    @metrics["task_types"][task["type"]] ||= {
      "count" => 0,
      "total_time" => 0,
      "pass" => 0,
      "fail" => 0
    }

    metrics = @metrics["task_types"][task["type"]]

    metrics["count"] += 1
    metrics["total_time"] += task["run_time"]
    task["success"] ? metrics["pass"] += 1 : metrics["fail"] += 1
  end

  @metrics
end

#elapsed_timeObject



35
36
37
# File 'lib/mcollective/util/playbook/report.rb', line 35

def elapsed_time
  Time.now.utc - @metrics["start_time"]
end

#finalize(success, fail_msg = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mcollective/util/playbook/report.rb', line 39

def finalize(success, fail_msg=nil)
  return(to_report) if @final

  @success = success
  @fail_message = fail_msg

  
  store_static_inputs
  store_dynamic_inputs
  store_node_groups
  store_task_results
  calculate_metrics

  @final = true

  to_report
end

#start!Object



31
32
33
# File 'lib/mcollective/util/playbook/report.rb', line 31

def start!
  @metrics["start_time"] = Time.now.utc
end

#store_dynamic_inputsObject



131
132
133
# File 'lib/mcollective/util/playbook/report.rb', line 131

def store_dynamic_inputs
  @inputs["dynamic"] = @playbook.dynamic_inputs
end

#store_node_groupsObject



125
126
127
128
129
# File 'lib/mcollective/util/playbook/report.rb', line 125

def store_node_groups
  @playbook.nodes.each do |key|
    @nodes[key] = @playbook.discovered_nodes(key)
  end
end

#store_playbook_metadataObject



120
121
122
123
# File 'lib/mcollective/util/playbook/report.rb', line 120

def 
  @playbook_name = @playbook.name
  @playbook_version = @playbook.version
end

#store_static_inputsObject



135
136
137
138
139
# File 'lib/mcollective/util/playbook/report.rb', line 135

def store_static_inputs
  @playbook.static_inputs.each do |key|
    @inputs["static"][key] = @playbook.input_value(key)
  end
end

#store_task_resultsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mcollective/util/playbook/report.rb', line 102

def store_task_results
  @playbook.task_results.each do |result|
    @tasks << {
      "type" => result.task_type,
      "set" => result.set,
      "description" => result.description,
      "start_time" => result.start_time.utc,
      "end_time" => result.end_time.utc,
      "run_time" => result.run_time,
      "ran" => result.ran,
      "msg" => result.msg,
      "success" => result.success
    }
  end

  @tasks
end

#to_reportObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mcollective/util/playbook/report.rb', line 57

def to_report
  {
    "report" => {
      "version" => @version,
      "timestamp" => @timestamp,
      "success" => @success,
      "playbook_error" => @fail_message
    },

    "playbook" => {
      "name" => @playbook_name,
      "version" => @playbook_version
    },

    "inputs" => @inputs,
    "nodes" => @nodes,
    "tasks" => @tasks,
    "metrics" => @metrics,
    "logs" => @logs
  }
end