Class: Log2COUNTER::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/log2counter/printer.rb

Constant Summary collapse

COLUMNS =
%w[
  Jahr_Monat
  Konsortial-Mitglied
  Nutzer
  Einzelkunden-Subidentifier
  Sessions
  Searches
  Downloads
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_file, summarize = false) ⇒ Printer

Returns a new instance of Printer.



49
50
51
52
53
# File 'lib/log2counter/printer.rb', line 49

def initialize(csv_file, summarize = false)
  @csv = FasterCSV.new(csv_file)

  @summarize = summarize
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



47
48
49
# File 'lib/log2counter/printer.rb', line 47

def csv
  @csv
end

#summarizeObject (readonly)

Returns the value of attribute summarize.



47
48
49
# File 'lib/log2counter/printer.rb', line 47

def summarize
  @summarize
end

Instance Method Details



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/log2counter/printer.rb', line 55

def print(stats)
  # Output CSV header.
  csv << COLUMNS

  stats.sort.each { |month, licensees|
    licensees.sort.each { |licensee, names|
      names.sort.each { |name, addresses|
        if summarize
          total = Log2COUNTER::Parser::DEFAULT_STATS.dup

          addresses.each { |address, stat|
            total[:sessions]  += stat[:sessions]
            total[:searches]  += stat[:searches]
            total[:downloads] += stat[:downloads]
          }

          csv << [
            month,
            licensee,
            name,
            nil,
            total[:sessions],
            total[:searches],
            total[:downloads]
          ]
        else
          addresses.sort_by_ip_or_host.each { |address, stat|
            csv << [
              month,
              licensee,
              name,
              address,
              stat[:sessions],
              stat[:searches],
              stat[:downloads]
            ]
          }
        end
      }
    }
  }

  csv.flush
end