Project

General

Profile

Download (4.14 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / reports.rb @ f8ecc788

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

    
13
module HammerCLICsv
14
  class ReportsCommand < BaseCommand
15

    
16
    TIME = 'Time'
17
    APPLIED = 'Applied'
18
    RESTARTED = 'Restarted'
19
    FAILED = 'Failed'
20
    FAILED_RESTARTS = 'Failed Restarts'
21
    SKIPPED = 'Skipped'
22
    PENDING = 'Pending'
23
    METRICS = 'Metrics'
24

    
25
    def export
26
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
27
        csv << [NAME, COUNT]
28
        @api.resource(:reports).call(:index, {'per_page' => 999999})['results'].each do |report|
29
          csv << [report['host_name'], 1, report['metrics'].to_json]
30
        end
31
      end
32

    
33
      HammerCLI::EX_OK
34
    end
35

    
36
    def import
37
      @existing_reports = {}
38
      @api.resource(:reports).call(:index, {'per_page' => 999999})['results'].each do |report|
39
        @existing_reports[report['name']] = report['id']
40
      end
41

    
42
      thread_import do |line|
43
        create_reports_from_csv(line)
44
      end
45
    end
46

    
47
    def create_reports_from_csv(line)
48
      line[COUNT].to_i.times do |number|
49
        name = namify(line[NAME], number)
50

    
51
        if !@existing_reports[name]
52
          print "Creating report '#{name}'..." if option_verbose?
53
          report = @api.resource(:reports).call(:create, {
54
                                          'host' => name,
55
                                          'reported_at' => line[TIME],
56
                                          'status' => {
57
                                            'applied' => line[APPLIED],
58
                                            'restarted' => line[RESTARTED],
59
                                            'failed' => line[FAILED],
60
                                            'failed_restarts' => line[FAILED_RESTARTS],
61
                                            'skipped' => line[SKIPPED],
62
                                            'pending' => line[PENDING]
63
                                          },
64
                                          'metrics' => JSON.parse(line[METRICS]),
65
                                          'logs' => []
66
                                        })
67
=begin
68
                                          'metrics' => {
69
                                            'time' => {
70
                                              'config_retrieval' => line[CONFIG_RETRIEVAL]
71
                                            },
72
                                            'resources' => {
73
                                              'applied' => 0,
74
                                              'failed' => 0,
75
                                              'failed_restarts' => 0,
76
                                              'out_of_sync' => 0,
77
                                              'restarted' => 0,
78
                                              'scheduled' => 1368,
79
                                              'skipped' => 1,
80
                                              'total' => 1450
81
                                            },
82
                                            'changes' => {
83
                                              'total' => 0
84
                                            }
85
                                          },
86
=end
87
          @existing_reports[name] = report['id']
88
        else
89
          print "Updating report '#{name}'..." if option_verbose?
90
          @api.resource(:reports).call(:update, {
91
                                 'id' => @existing_reports[name]
92
                               })
93
        end
94

    
95
        puts "done" if option_verbose?
96
      end
97
    end
98
  end
99

    
100
  HammerCLICsv::CsvCommand.subcommand("reports",
101
                                      "import or export reports",
102
                                      HammerCLICsv::ReportsCommand)
103
end