hammer-cli-csv / lib / hammer_cli_csv / reports.rb @ df47dd3c
1 |
# Copyright 2013-2014 Red Hat, Inc.
|
---|---|
2 |
#
|
3 |
# This software is licensed to you under the GNU General Public
|
4 |
# License as published by the Free Software Foundation; either version
|
5 |
# 2 of the License (GPLv2) or (at your option) any later version.
|
6 |
# There is NO WARRANTY for this software, express or implied,
|
7 |
# including the implied warranties of MERCHANTABILITY,
|
8 |
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
|
9 |
# have received a copy of GPLv2 along with this software; if not, see
|
10 |
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
|
11 |
|
12 |
module HammerCLICsv |
13 |
class CsvCommand |
14 |
class ReportsCommand < BaseCommand |
15 |
command_name 'reports'
|
16 |
desc 'import or export reports'
|
17 |
|
18 |
TIME = 'Time' |
19 |
APPLIED = 'Applied' |
20 |
RESTARTED = 'Restarted' |
21 |
FAILED = 'Failed' |
22 |
FAILED_RESTARTS = 'Failed Restarts' |
23 |
SKIPPED = 'Skipped' |
24 |
PENDING = 'Pending' |
25 |
METRICS = 'Metrics' |
26 |
|
27 |
def export |
28 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| |
29 |
csv << [NAME, COUNT] |
30 |
@api.resource(:reports).call(:index, {'per_page' => 999999})['results'].each do |report| |
31 |
csv << [report['host_name'], 1, report['metrics'].to_json] |
32 |
end
|
33 |
end
|
34 |
|
35 |
HammerCLI::EX_OK |
36 |
end
|
37 |
|
38 |
def import |
39 |
@existing_reports = {}
|
40 |
@api.resource(:reports).call(:index, {'per_page' => 999999})['results'].each do |report| |
41 |
@existing_reports[report['name']] = report['id'] |
42 |
end
|
43 |
|
44 |
thread_import do |line|
|
45 |
create_reports_from_csv(line) |
46 |
end
|
47 |
end
|
48 |
|
49 |
def create_reports_from_csv(line) |
50 |
line[COUNT].to_i.times do |number| |
51 |
name = namify(line[NAME], number)
|
52 |
|
53 |
if !@existing_reports[name] |
54 |
print "Creating report '#{name}'..." if option_verbose? |
55 |
reported_at = line[TIME] || Time.now |
56 |
report = @api.resource(:reports).call(:create, { |
57 |
'host' => name,
|
58 |
'reported_at' => reported_at,
|
59 |
'status' => {
|
60 |
'applied' => line[APPLIED], |
61 |
'restarted' => line[RESTARTED], |
62 |
'failed' => line[FAILED], |
63 |
'failed_restarts' => line[FAILED_RESTARTS], |
64 |
'skipped' => line[SKIPPED], |
65 |
'pending' => line[PENDING] |
66 |
}, |
67 |
'metrics' => JSON.parse(line[METRICS]), |
68 |
'logs' => []
|
69 |
}) |
70 |
=begin
|
71 |
'metrics' => {
|
72 |
'time' => {
|
73 |
'config_retrieval' => line[CONFIG_RETRIEVAL]
|
74 |
},
|
75 |
'resources' => {
|
76 |
'applied' => 0,
|
77 |
'failed' => 0,
|
78 |
'failed_restarts' => 0,
|
79 |
'out_of_sync' => 0,
|
80 |
'restarted' => 0,
|
81 |
'scheduled' => 1368,
|
82 |
'skipped' => 1,
|
83 |
'total' => 1450
|
84 |
},
|
85 |
'changes' => {
|
86 |
'total' => 0
|
87 |
}
|
88 |
},
|
89 |
=end
|
90 |
@existing_reports[name] = report['id'] |
91 |
else
|
92 |
print "Updating report '#{name}'..." if option_verbose? |
93 |
@api.resource(:reports).call(:update, { |
94 |
'id' => @existing_reports[name] |
95 |
}) |
96 |
end
|
97 |
|
98 |
puts 'done' if option_verbose? |
99 |
end
|
100 |
end
|
101 |
end
|
102 |
end
|
103 |
end
|