Class: ODDB::NotificationLogger

Inherits:
Object
  • Object
show all
Includes:
ODBA::Persistable
Defined in:
src/util/notification_logger.rb

Defined Under Namespace

Classes: LogEntry

Constant Summary

ODBA_SERIALIZABLE =
['@logs']

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (NotificationLogger) initialize

A new instance of NotificationLogger



33
34
35
36
# File 'src/util/notification_logger.rb', line 33

def initialize
  super
  @logs = {}
end

Instance Attribute Details

- (Object) lines (readonly)

Returns the value of attribute lines



32
33
34
# File 'src/util/notification_logger.rb', line 32

def lines
  @lines
end

- (Object) logs (readonly)

Returns the value of attribute logs



32
33
34
# File 'src/util/notification_logger.rb', line 32

def logs
  @logs
end

Instance Method Details

- (Object) create_csv(app)



60
61
62
63
64
# File 'src/util/notification_logger.rb', line 60

def create_csv(app)
  csv_lines(app).collect { |line|
    CSV.generate_line(line, ';')
  }.join("\n") << "\n"
end

- (Object) csv_line(month_range, entry, entries, arguments)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'src/util/notification_logger.rb', line 70

def csv_line(month_range, entry, entries, arguments)
  line = [
    arguments[:iksnr].to_s,
    arguments[:name].to_s,
    arguments[:size].to_s,
    entry.sender.to_s,
    entry.recipient.to_s,
    entries.size.to_s,
  ]
  month = month_range.first
  while(month <= month_range.last)
    count = entries.select { |val| 
      val.time.year == month.year && val.time.mon == month.mon
    }.size
    line.push(count.to_s)
    month = month >> 1
  end
  line
end

- (Object) csv_lines(app)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'src/util/notification_logger.rb', line 89

def csv_lines(app)
  header = [
    "Code",
    "Name",
    "Grösse",
    "Sender",
    "Empfänger",
    "Total",
  ]
  month_range = (first_month)..(last_month)
  month = month_range.first
  while(month <= month_range.last)
    header.push("#{month.strftime("%B")} #{month.year}")
    month = month >> 1
  end
  lines = []
  @logs.each { |combined_key, entries| 
    type, key = combined_key
    name, size = ''
    case type
    when :drugs
      iksnr = key[0,5]
      ikscd = key[5,3]
      if((reg = app.registration(iksnr)) \
        && (pac = reg.package(ikscd)))
        name = pac.name
        size = pac.size
      end
    when :migel
      if(product = app.migel_product(key))
        if(txt = product.product_text)
          name = txt.de
          size = product.de
        else
          name = product.de
        end
      end
    end
    arguments = {
      :name      => name,
      :size      => size,
      :iksnr   => key, 
      :entries => entries, 
    }
    lines += range_lines(month_range, entries, arguments)
  }
  lines.sort.unshift(header)
end

- (Object) first_month



48
49
50
51
52
53
# File 'src/util/notification_logger.rb', line 48

def first_month
  entry = @logs.collect { |key, entries|
    entries.first
  }.compact.min || Time.now
  Date.new(entry.year, entry.month)
end

- (Object) last_month



54
55
56
57
58
59
# File 'src/util/notification_logger.rb', line 54

def last_month
  entry = @logs.collect { |key, entries|
    entries.last
  }.compact.max || Time.now
  Date.new(entry.year, entry.month)
end

- (Object) log(key, sender, recipient, time)



37
38
39
40
41
# File 'src/util/notification_logger.rb', line 37

def log(key, sender, recipient, time)
  entry = LogEntry.new(sender, recipient, time)
  (@logs[key] ||= []).push(entry)
  entry
end

- (Object) range_lines(month_range, entries, arguments)



65
66
67
68
69
# File 'src/util/notification_logger.rb', line 65

def range_lines(month_range, entries, arguments)
  entries.collect { |entry| 
    csv_line(month_range, entry, entries, arguments)
  }
end

- (Object) total_count



42
43
44
# File 'src/util/notification_logger.rb', line 42

def total_count
  @logs.size
end

- (Object) total_count_iksnr(iksnr)



45
46
47
# File 'src/util/notification_logger.rb', line 45

def total_count_iksnr(iksnr)
  (@logs[iksnr.to_s] ||= []).size
end