Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / reports.rb @ 561a8ac9

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)\
31
            .call(:index, {
32
                    'per_page' => 999999
33
                  })['results'].each do |report|
34
            csv << [report['host_name'], 1, report['metrics'].to_json]
35
          end
36
        end
37

    
38
        HammerCLI::EX_OK
39
      end
40

    
41
      def import
42
        @existing_reports = {}
43
        @api.resource(:reports)\
44
          .call(:index, {
45
                  'per_page' => 999999
46
                })['results'].each do |report|
47
          @existing_reports[report['name']] = report['id']
48
        end
49

    
50
        thread_import do |line|
51
          create_reports_from_csv(line)
52
        end
53
      end
54

    
55
      def create_reports_from_csv(line)
56
        line[COUNT].to_i.times do |number|
57
          name = namify(line[NAME], number)
58

    
59
          if !@existing_reports[name]
60
            print "Creating report '#{name}'..." if option_verbose?
61
            reported_at = line[TIME] || Time.now
62
            report = @api.resource(:reports)\
63
              .call(:create, {
64
                      'host' => name,
65
                      'reported_at' => reported_at,
66
                      'status' => {
67
                        'applied' => line[APPLIED],
68
                        'restarted' => line[RESTARTED],
69
                        'failed' => line[FAILED],
70
                        'failed_restarts' => line[FAILED_RESTARTS],
71
                        'skipped' => line[SKIPPED],
72
                        'pending' => line[PENDING]
73
                      },
74
                      'metrics' => JSON.parse(line[METRICS]),
75
                      'logs' => []
76
                    })
77
            @existing_reports[name] = report['id']
78
          else
79
            print "Updating report '#{name}'..." if option_verbose?
80
            @api.resource(:reports)\
81
              .call(:update, {
82
                      'id' => @existing_reports[name]
83
                    })
84
          end
85

    
86
          puts 'done' if option_verbose?
87
        end
88
      end
89
    end
90
  end
91
end