Class: MCollective::Util::TasksSupport::CLI::JSONFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/util/tasks_support/json_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli, verbose = false, out = $stdout) ⇒ JSONFormatter

Returns a new instance of JSONFormatter.



8
9
10
11
12
13
14
15
16
17
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 8

def initialize(cli, verbose=false, out=$stdout)
  @cli = cli
  @verbose = verbose
  @out = out

  @started = false
  @in_results = false
  @in_stats = false
  @first_item = true
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



6
7
8
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 6

def out
  @out
end

Instance Method Details

#end_resultsObject



28
29
30
31
32
33
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 28

def end_results
  return unless @in_results

  @in_results = false
  out.puts "],"
end

Prints an individual result

Parameters:

  • result [RPC::Result]



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
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 74

def print_result(result)
  start

  result = result.results

  item = {
    "host" => result[:sender]
  }

  result[:data].each do |k, v|
    item[k.to_s] = v
  end

  begin
    item["stdout"] = JSON.parse(item["stdout"])
    item["stdout"] = item["stdout"].delete("_output") if item["stdout"]["_output"]
  rescue # rubocop:disable Lint/SuppressedException
  end

  out.puts "," unless @first_item

  out.puts item.to_json

  @first_item = false
end

Prints metadata for an individual result

Parameters:

  • status [RPC::Reply] the individual task status to print



101
102
103
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 101

def (status)
  print_result(status)
end

Prints the RPC stats for a request

Parameters:

  • stats [RPC::Stats] request stats



69
70
71
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 69

def print_rpc_stats(stats)
  end_results
end

Prints the summary of a task over a number of machines

Parameters:

  • taskid [String] the task id

  • names [Array<String>] list of unique task names seen in the estate

  • callers [Array<String>] list of unique callers seen in the estate

  • completed [Integer] nodes that completed the task - success or fails

  • running [Integer] nodes still running the task

  • task_not_known [Integer] nodes where the task is unknown

  • wrapper_failure [Integer] nodes where the wrapper executable failed to launch

  • success [Integer] nodes that completed the task succesfully

  • fails [Integer] nodes that did not complete the task succesfully

  • runtime [Float] total run time across all nodes

  • rpcstats [RPC::Stats] the stats from the RPC client that fetched the statusses



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
66
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 36

def print_task_summary(taskid, names, callers, completed, running, task_not_known, wrapper_failure, success, fails, runtime, rpcstats)
  end_results

  stats = {
    "names" => names,
    "callers" => callers,
    "completed" => completed,
    "running" => running,
    "task_not_known" => task_not_known,
    "wrapper_failure" => wrapper_failure,
    "success" => success,
    "failed" => fails,
    "average_runtime" => runtime / (running + completed),
    "noresponses" => rpcstats.noresponsefrom
  }

  summary = {
    "nodes" => rpcstats.discovered_nodes.size,
    "taskid" => taskid,
    "completed" => stats["completed"] == rpcstats.discovered_nodes.size,
    "success" => success == rpcstats.discovered_nodes.size
  }

  out.puts '"stats":'
  out.puts stats.to_json
  out.puts ","
  out.puts '"summary":'

  out.puts summary.to_json
  out.puts "}"
end

#startObject



19
20
21
22
23
24
25
26
# File 'lib/mcollective/util/tasks_support/json_formatter.rb', line 19

def start
  return if @started

  @started = true
  @in_results = true

  out.puts '{"items": ['
end