Project

General

Profile

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

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

1
# Copyright (c) 2013-2014 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24

    
25
module HammerCLICsv
26
  class ReportsCommand < BaseCommand
27

    
28
    TIME = 'Time'
29
    APPLIED = 'Applied'
30
    RESTARTED = 'Restarted'
31
    FAILED = 'Failed'
32
    FAILED_RESTARTS = 'Failed Restarts'
33
    SKIPPED = 'Skipped'
34
    PENDING = 'Pending'
35
    METRICS = 'Metrics'
36

    
37
    def export
38
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
39
        csv << [NAME, COUNT]
40
        @f_report_api.index({'per_page' => 999999})[0]['results'].each do |report|
41
          csv << [report['host_name'], 1, report['metrics'].to_json]
42
        end
43
      end
44

    
45
      HammerCLI::EX_OK
46
    end
47

    
48
    def import
49
      @existing_reports = {}
50
      @f_report_api.index({'per_page' => 999999})[0]['results'].each do |report|
51
        @existing_reports[report['name']] = report['id']
52
      end
53

    
54
      thread_import do |line|
55
        create_reports_from_csv(line)
56
      end
57
    end
58

    
59
    def create_reports_from_csv(line)
60
      line[COUNT].to_i.times do |number|
61
        name = namify(line[NAME], number)
62

    
63
        if !@existing_reports[name]
64
          print "Creating report '#{name}'..." if option_verbose?
65
          report = @f_report_api.create({
66
                                          'host' => name,
67
                                          'reported_at' => line[TIME],
68
                                          'status' => {
69
                                            'applied' => line[APPLIED],
70
                                            'restarted' => line[RESTARTED],
71
                                            'failed' => line[FAILED],
72
                                            'failed_restarts' => line[FAILED_RESTARTS],
73
                                            'skipped' => line[SKIPPED],
74
                                            'pending' => line[PENDING]
75
                                          },
76
                                          'metrics' => JSON.parse(line[METRICS]),
77
                                          'logs' => []
78
                                        })[0]
79
=begin
80
                                          'metrics' => {
81
                                            'time' => {
82
                                              'config_retrieval' => line[CONFIG_RETRIEVAL]
83
                                            },
84
                                            'resources' => {
85
                                              'applied' => 0,
86
                                              'failed' => 0,
87
                                              'failed_restarts' => 0,
88
                                              'out_of_sync' => 0,
89
                                              'restarted' => 0,
90
                                              'scheduled' => 1368,
91
                                              'skipped' => 1,
92
                                              'total' => 1450
93
                                            },
94
                                            'changes' => {
95
                                              'total' => 0
96
                                            }
97
                                          },
98
=end
99
          @existing_reports[name] = report['id']
100
        else
101
          print "Updating report '#{name}'..." if option_verbose?
102
          @f_report_api.update({
103
                                 'id' => @existing_reports[name]
104
                               })
105
        end
106

    
107
        puts "done" if option_verbose?
108
      end
109
    end
110
  end
111

    
112
  HammerCLI::MainCommand.subcommand("csv:reports", "import / export reports", HammerCLICsv::ReportsCommand)
113
end