--

Ruby code posted
created at 17 Aug 13:22

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
# encoding: UTF-8

PRODUCT_TYPES = {1 => 'Web Filter', 8 => 'Antivirus', 9 => 'Web Filter + Antivirus', 125 => 'Security Suite'}

class Report
  def initialize(filename)
    @file = File.open(filename, 'r')
    @headers = {}
    
    begin
      line = @file.gets
      next if line.strip.empty? or line =~ /^Generated on /
      @headers = line.strip.split("\t")
      # for i in 0 ... hdrs.size
      #   headers[hdrs[i]] = i
      # end
    end while @headers.empty?
  end
  
  def line(&block)
    while line = @file.gets
      fields = line.strip.split("\t")
      data = {}
      for i in 0 ... fields.size
        data[@headers[i]] = fields[i]
      end
      block.call(data)
    end
  end
end

filename = ARGV.shift
active_licenses = {}

report = Report.new(filename)
report.line do |data|
  active_licenses[data['LicenseID']] = data['Product'].to_i if data['LicenseStatus'] == '1'
end

license_count_per_product = {}
active_licenses.each do |license_id, product_type|
  if license_count_per_product[product_type]
    license_count_per_product[product_type] += 1
  else
    license_count_per_product[product_type] = 1
  end
end

license_count_per_product = license_count_per_product.sort { |a, b| a[0] <=> b[0] }

license_count_per_product.each do |data|
  if PRODUCT_TYPES[data[0]]
    product_name = PRODUCT_TYPES[data[0]]
  else
    product_name = "Unknown (type %d)" % data[0]
  end
  puts "%24s  %d" % [product_name, data[1]]
end
1.49 KB in 6 ms with coderay