Project

General

Profile

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

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

1
module HammerCLICsv
2
  class CsvCommand
3
    class ReportsCommand < BaseCommand
4
      command_name 'reports'
5
      desc         'import or export reports'
6

    
7
      TIME = 'Time'
8
      APPLIED = 'Applied'
9
      RESTARTED = 'Restarted'
10
      FAILED = 'Failed'
11
      FAILED_RESTARTS = 'Failed Restarts'
12
      SKIPPED = 'Skipped'
13
      PENDING = 'Pending'
14
      METRICS = 'Metrics'
15

    
16
      def export(csv)
17
        csv << [NAME]
18
        @api.resource(:reports).call(:index, {
19
            'per_page' => 999999
20
        })['results'].each do |report|
21
          csv << [report['host_name'], report['metrics'].to_json]
22
        end
23
      end
24

    
25
      def import
26
        @existing_reports = {}
27
        @api.resource(:reports).call(:index, {
28
            'per_page' => 999999
29
        })['results'].each do |report|
30
          @existing_reports[report['name']] = report['id']
31
        end
32

    
33
        thread_import do |line|
34
          create_reports_from_csv(line)
35
        end
36
      end
37

    
38
      def create_reports_from_csv(line)
39
        count(line[COUNT]).times do |number|
40
          name = namify(line[NAME], number)
41

    
42
          if !@existing_reports[name]
43
            print "Creating report '#{name}'..." if option_verbose?
44
            reported_at = line[TIME] || Time.now
45
            report = @api.resource(:reports).call(:create, {
46
                'host' => name,
47
                'reported_at' => reported_at,
48
                'status' => {
49
                    'applied' => line[APPLIED],
50
                    'restarted' => line[RESTARTED],
51
                    'failed' => line[FAILED],
52
                    'failed_restarts' => line[FAILED_RESTARTS],
53
                    'skipped' => line[SKIPPED],
54
                    'pending' => line[PENDING]
55
                },
56
                'metrics' => JSON.parse(line[METRICS]),
57
                'logs' => []
58
            })
59
            @existing_reports[name] = report['id']
60
          else
61
            print "Updating report '#{name}'..." if option_verbose?
62
            @api.resource(:reports).call(:update, {
63
                'id' => @existing_reports[name]
64
            })
65
          end
66

    
67
          puts 'done' if option_verbose?
68
        end
69
      end
70
    end
71
  end
72
end