-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign.rb
199 lines (161 loc) · 3.85 KB
/
design.rb
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
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
module LogfileInterval
module ParsedLine
module Parser
def columns
@columns ||= {}
end
def set_regex
end
def add_column
agg = Aggregator::Base.klass(aggregator)
@columns[name] = { :pos => pos, :aggregator => agg, :conversion => conversion }
define_method(name)
end
def parse(line)
match_data = regex.match(line)
data = {}
data = f(match_data)
end
def each(&block)
columns.each(&block)
end
end
class Base
extend Parser
def initialize(line)
@data = self.class.parse(line)
end
end
end
class AggregatorSet
def initialize(parser_columns)
@aggregators = {}
parser_columns.each do |name, options|
@aggregators[name] = options[:aggregator].new(options)
end
end
def add(record)
@aggregators.each do |name, agg|
agg.add_record(record)
end
end
def each
@aggregators.each do |name, agg|
yield name, agg
end
end
end
class Interval
def initialize(end_time, length, parser_columns)
@aggregators = AggregatorSet.new(parser_columns)
end
def [](name)
@aggregators[name].value
end
def add(record)
@size += 1
@aggregators.add_record(record)
end
end
class IntervalBuilder
def initialize(parsed_lines_enum, parser_columns, length)
end
def each_interval
interval = Interval.new(now, length, parser_columns)
parsed_lines_enum.each do |record|
while record.time < interval.start_time do
yield interval
interval = Interval.new(interval.start_time, length, aggregators)
end
interval.add(record)
end
end
end
module Aggregator
class Base
def self.register(name, klass)
@aggregator_classes[name] = klass
end
def self.klass(name)
@aggregator_classes.fetch(name)
end
end
class Sum < Base
register :sum, self
def initialize
@val = 0
end
def add(value)
@val += value
end
end
class Count < Base
def initialize
@val = Counter.new
end
def add(value)
@val.increment(value)
end
end
end
class Logfile
def initialize(filename, parser)
end
def each_line
return enum_for(:each_line) unless block_given?
...
end
def each_parsed_line
each_line do |line|
record = parser.create_record(line)
yield record if record
end
end
end
class LogfileSet
def initialize(filenames_array, parser)
end
def ordered_filenames
end
def each_line
end
def each_parsed_line
end
end
class Counter < Hash
def increment(key)
self[key] = self[key] ? self[key] + 1 : 1
end
end
end
class AccessLogParsedLine < LogfileInterval::Parse::Base
set_regex /blah/
add_column :name => :foo, :pos => 1, :conversion => integer, :aggregator => :average
end
logfiles = [ 'access.log', 'access.log.1', 'access.log.2' ]
logfile = logfiles.first
parser = AccessLogParsedLine
logfile_iterator = LogfileInterval::Logfile.new(logfile, parser)
logfile_iterator.each_line do |line|
puts line.class # String
puts line
end
logfile_iterator.each_parsed_line do |record|
puts record.class # ParsedLine::AccessLog
puts record.ip
puts record.time
end
set = LogfileInterval::LogfileSet.new(logfiles, parser)
set.each_parsed_line do |record|
puts record.class # ParsedLine::AccessLog
end
length = 5.minutes
interval_builder = LogfileInterval::IntervalBuilder.new(set.each_parsed_line, parser, length)
interval_builder.each_interval do |interval|
puts interval.class # LogfileInterval::Interval
puts interval.start_time
puts interval.length
interval[:ip].each do |ip, count|
puts "#{ip}, #{count}"
end
end