-
Notifications
You must be signed in to change notification settings - Fork 5
/
LogParser.m
372 lines (284 loc) · 11.6 KB
/
LogParser.m
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// LogParser.m
// Spike
//
// Created by Matt Mower on 13/02/2009.
// Copyright 2009 LucidMac Software. All rights reserved.
//
#import "LogParser.h"
#import <TDParseKit/TDParseKit.h>
#import "LogDocument.h"
#import "RailsRequest.h"
#import "ParamParser.h"
#import "Parameter.h"
#import "ParsingProgressController.h"
#import "NSScanner+SpikeAdditions.h"
#import "NSData+ZlibAdditions.h"
#import "NSString+SpikeAdditions.h"
@interface LogParser (PrivateMethods)
- (RailsRequest *)parseRequest:(NSArray *)lines;
- (NSArray *)parseLogLines:(NSArray *)lines;
- (void)scanProcessing:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanParameters:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanSession:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanCompleted:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanCompletedOldFormat:(NSString *)line scanner:(NSScanner *)scanner intoRequest:(RailsRequest *)request;
- (void)scanCompletedNewFormat:(NSString *)line scanner:(NSScanner *)scanner intoRequest:(RailsRequest *)request;
- (void)scanRendering:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanRendered:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanFilter:(NSString *)line intoRequest:(RailsRequest *)request;
- (void)scanRedirect:(NSString *)line intoRequest:(RailsRequest *)request;
@end
@implementation LogParser
- (id)initWithDocument:(LogDocument *)theDocument {
if( ( self = [super init] ) ) {
document = theDocument;
dateParser = [[NSDateFormatter alloc] init];
[dateParser setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
paramParser = [[ParamParser alloc] init];
highWaterMark = 0;
}
return self;
}
- (void)parseLogData:(NSData *)data {
progressController = [[ParsingProgressController alloc] init];
[progressController showWindow:self];
[progressController setAnimated:YES];
if( [document compressedLog] ) {
[progressController setStatus:@"Decompressing log data"];
data = [data gzipInflate];
}
// Take only new data
data = [data subdataWithRange:NSMakeRange( highWaterMark, [data length] - highWaterMark )];
[progressController setStatus:@"Scanning log data"];
//NSString *content = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *lines = [content componentsSeparatedByString:@"\n"];
[progressController setStatus:@"Parsing log data"];
[progressController setMin:0 max:[lines count]];
NSLog( @"%d lines to parse.", [lines count] );
NSArray *requests = [self parseLogLines:lines];
NSLog( @"%d requests parsed.", [requests count] );
highWaterMark += [data length];
[document performSelectorOnMainThread:@selector(setRequests:) withObject:requests waitUntilDone:YES];
[progressController close];
}
- (NSArray *)parseLogLines:(NSArray *)lines {
NSMutableArray *requests = [[NSMutableArray alloc] init];
double linesProcessed = 0.0;
BOOL hitFirstValidEntry = NO;
NSMutableArray *lineGroup = [[NSMutableArray alloc] init];
NSDate *start = [NSDate date];
for( NSString *line in lines ) {
line = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
line = [line stringByRemovingANSIEscapeSequences];
if( ![line isEqualToString:@""] ) {
if( [line hasPrefix:@"Processing"] ) {
if( [lineGroup count] > 0 ) {
[requests addObject:[self parseRequest:lineGroup]];
[lineGroup removeAllObjects];
// break;
}
hitFirstValidEntry = YES;
}
if( hitFirstValidEntry ) {
[lineGroup addObject:line];
}
}
linesProcessed += 1;
if ((int)linesProcessed % 10000 == 0){
NSLog(@"%f processed in %f seconds", linesProcessed, [start timeIntervalSinceNow]);
}
[progressController update:linesProcessed];
}
// Let's not lose the last request
if( [lineGroup count] > 0 ) {
[requests addObject:[self parseRequest:lineGroup]];
}
return requests;
}
- (RailsRequest *)parseRequest:(NSArray *)lines {
RailsRequest *request = [[RailsRequest alloc] init];
for( NSString *line in lines ) {
if( [line hasPrefix:@"Processing"] ) {
[self scanProcessing:line intoRequest:request];
} else if( [line hasPrefix:@"Parameters"] ) {
[self scanParameters:line intoRequest:request];
} else if( [line hasPrefix:@"Session ID:"] ) {
[self scanSession:line intoRequest:request];
} else if( [line hasPrefix:@"Completed in"] ) {
[self scanCompleted:line intoRequest:request];
} else if( [line hasPrefix:@"Rendering"] ) {
[self scanRendering:line intoRequest:request];
} else if( [line hasPrefix:@"Rendered"] ) {
[self scanRendered:line intoRequest:request];
} else if( [line hasPrefix:@"Filter"] ) {
[self scanFilter:line intoRequest:request];
} else if( [line hasPrefix:@"Redirected to"] ) {
[self scanRedirect:line intoRequest:request];
}
}
[request setSourceLog:[[NSAttributedString alloc] initWithString:[lines description]]];
[request postProcess];
return request;
}
- (void)scanProcessing:(NSString *)line intoRequest:(RailsRequest *)request {
NSScanner *scanner = [NSScanner scannerWithString:line];
NSString *buffer;
[scanner scanString:@"Processing " intoString:nil];
[scanner scanUpToString:@"#" intoString:&buffer];
[request setController:buffer];
[scanner scanString:@"#" intoString:nil];
[scanner scanUpToString:@" " intoString:&buffer];
[request setAction:buffer];
[scanner scanString:@"(for " intoString:nil];
[scanner scanUpToString:@" " intoString:&buffer];
[request setClient:[NSHost hostWithAddress:buffer]];
[scanner scanString:@"at " intoString:nil];
[scanner scanUpToString:@")" intoString:&buffer];
[request setWhen:[dateParser dateFromString:buffer]];
[scanner scanString:@") [" intoString:nil];
[scanner scanUpToString:@"]" intoString:&buffer];
[request setMethod:buffer];
}
- (void)scanParameters:(NSString *)line intoRequest:(RailsRequest *)request {
@try {
[request setParams:[paramParser parseParams:[line substringFromIndex:12]]];
}
@catch( TDTrackException *trackException ) {
NSLog( @"Parameter parser could not finish: %@", [trackException reason] );
NSLog( @"\t%@", line );
}
}
- (void)scanSession:(NSString *)line intoRequest:(RailsRequest *)request {
NSScanner *scanner = [NSScanner scannerWithString:line];
NSString *buffer;
[scanner scanString:@"Session ID: " intoString:nil];
[scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&buffer];
[request setSession:buffer];
}
- (void)scanCompleted:(NSString *)line intoRequest:(RailsRequest *)request {
NSScanner *scanner = [NSScanner scannerWithString:line];
float t;
[scanner scanString:@"Completed in " intoString:nil];
if( ![scanner scanFloat:&t] ) {
NSLog( @"Bail scanning for realTime!" );
return;
}
[request setRealTime:t];
if( [scanner scanString:@"ms" intoString:nil] ) {
[self scanCompletedNewFormat:line scanner:scanner intoRequest:request];
} else {
[self scanCompletedOldFormat:line scanner:scanner intoRequest:request];
}
}
- (void)scanCompletedOldFormat:(NSString *)line scanner:(NSScanner *)scanner intoRequest:(RailsRequest *)request {
float t;
int n;
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:nil];
[scanner scanString:@"(" intoString:nil];
if( ![scanner scanInt:&n] ) {
NSLog( @"Bail scanning for requests!" );
return;
}
[request setRps:n];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:nil];
[scanner scanString:@"reqs/sec) |" intoString:nil];
if( [line rangeOfString:@"Rendering" options:NSCaseInsensitiveSearch].location != NSNotFound ) {
[scanner scanString:@"Rendering: " intoString:nil];
if( ![scanner scanFloat:&t] ) {
NSLog( @"Bail scanning for render time! in (%@)", line );
return;
}
[request setRenderTime:t];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:nil];
[scanner scanString:@"(" intoString:nil];
[scanner scanInt:&n];
[scanner scanString:@"%) | " intoString:nil];
}
[scanner scanString:@"DB: " intoString:nil];
if( ![scanner scanFloat:&t] ) {
NSLog( @"Bail scanning for DB time! [%@]", [line substringFromIndex:[scanner scanLocation]] );
return;
}
[request setDbTime:t];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:nil];
[scanner scanString:@"(" intoString:nil];
[scanner scanInt:&n];
[scanner scanString:@"%) | " intoString:nil];
if( ![scanner scanInt:&n] ) {
NSLog( @"Bail scanning for status! [%@]", [line substringFromIndex:[scanner scanLocation]] );
return;
}
[request setStatus:n];
NSRange r1, r2;
r1 = [line rangeOfString:@"[" options:NSBackwardsSearch];
r2 = [line rangeOfString:@"]" options:NSBackwardsSearch];
if( r1.location != NSNotFound && r2.location != NSNotFound ) {
[request setUrl:[line substringWithRange:NSMakeRange( r1.location+1, r2.location - r1.location - 1)]];
}
}
- (void)scanCompletedNewFormat:(NSString *)line scanner:(NSScanner *)scanner intoRequest:(RailsRequest *)request {
float t;
int n;
[scanner eatWS];
[scanner eat:@"("];
// "View: 8, "
if( [scanner detect:@"View:"] ) {
[scanner eatWS];
[scanner scanFloat:&t];
[request setRenderTime:t];
[scanner eat:@","];
[scanner eatWS];
}
if( [scanner detect:@"DB:"] ) {
[scanner eatWS];
[scanner scanFloat:&t];
[request setDbTime:t];
}
// New format is given in millis, convert to (s)
[request setRealTime:[request realTime]/1000];
[request setRenderTime:[request renderTime]/1000];
[request setDbTime:[request dbTime]/1000];
// Calculate RPS not given in 2.2 format
[request setRps:( 1.0 / [request realTime] )];
[scanner eat:@")"];
[scanner eatWS];
if( [scanner detect:@"|"] ) {
[scanner eatWS];
[scanner scanInt:&n];
[request setStatus:n];
}
NSRange r1, r2;
r1 = [line rangeOfString:@"[" options:NSBackwardsSearch];
r2 = [line rangeOfString:@"]" options:NSBackwardsSearch];
if( r1.location != NSNotFound && r2.location != NSNotFound ) {
[request setUrl:[line substringWithRange:NSMakeRange( r1.location+1, r2.location - r1.location - 1)]];
}
}
- (void)scanRendering:(NSString *)line intoRequest:(RailsRequest *)request {
NSString *render = [line substringFromIndex:10];
if( [render rangeOfString:@"(internal_server_error)"].location != NSNotFound || [render rangeOfString:@"(not_found)"].location != NSNotFound ) {
request.status = 500;
} else if( [render rangeOfString:@".html"].location != NSNotFound ) {
NSRange match = [render rangeOfString:@"public"];
[[request renders] addObject:[render substringFromIndex:match.location]];
} else {
[[request renders] addObject:render];
}
}
- (void)scanRendered:(NSString *)line intoRequest:(RailsRequest *)request {
[[request renders] addObject:[line substringFromIndex:9]];
}
- (void)scanFilter:(NSString *)line intoRequest:(RailsRequest *)request {
NSScanner *scanner = [NSScanner scannerWithString:line];
NSString *buffer;
[scanner scanString:@"Filter chain halted as [:" intoString:nil];
[scanner scanUpToString:@"]" intoString:&buffer];
[request setHalted:YES];
[request setFilter:buffer];
}
- (void)scanRedirect:(NSString *)line intoRequest:(RailsRequest *)request {
[request setRedirect:[line substringFromIndex:14]];
}
@end