Authored by 王钱钧

update code

... ... @@ -8,6 +8,9 @@
#import "AFURLConnectionOperation+Performance.h"
#import "Aspects.h"
#import "YH_Analytics.h"
@implementation AFURLConnectionOperation (Performance)
+ (void)addOperationMonitor
... ... @@ -15,22 +18,37 @@
// start
[AFURLConnectionOperation aspect_hookSelector:@selector(operationDidStart) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo>aspectInfo){
AFURLConnectionOperation *op = [aspectInfo instance];
op.startTS = [[NSDate date]timeIntervalSince1970];
NSLog(@"%@ \n did start========> %f",op.request.URL, op.startTS);
} error:NULL];
// finish
[AFURLConnectionOperation aspect_hookSelector:@selector(finish) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
[AFURLConnectionOperation aspect_hookSelector:@selector(connectionDidFinishLoading:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
AFURLConnectionOperation *op = [aspectInfo instance];
op.finishTS = [[NSDate date]timeIntervalSince1970];
NSInteger status = -1;
long deltaTS = (op.finishTS - op.startTS) * 1000;
NSString *url = [AFURLConnectionOperation urlWithOperation:op];
NSString *opAddr = [NSString stringWithFormat:@"%p", op];
NSLog(@"%@ \n finsh =======>%ld",op.request.URL, deltaTS);
if (op.response) {
if ([op.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)op.response;
status = [response statusCode];
}
if (status == 200) {
// success
status = 1;
}
[[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeAPICall
parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
JsonKeyPerformanceURL: url?:@"",
JsonKeyPerformanceTS: @((long)(op.startTS*1000)),
JsonKeyPerformanceCTS:@(deltaTS),
JsonKeyPerformanceSTATUS : @(status)
}];
}
} error:NULL];
... ... @@ -40,11 +58,22 @@
AFURLConnectionOperation *op = [aspectInfo instance];
op.errorTS = [[NSDate date]timeIntervalSince1970];
long deltaTS = (op.errorTS - op.startTS) * 1000;
NSString *url = [AFURLConnectionOperation urlWithOperation:op];
NSLog(@"%@ \n error =======>%ld",op.request.URL, deltaTS);
// error code default is `-1`
NSInteger errorCode = -1;
if (op.error) {
errorCode = [op.error code];
}
NSString *opAddr = [NSString stringWithFormat:@"%p", op];
[[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeAPICall
parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
JsonKeyPerformanceURL: url?:@"",
JsonKeyPerformanceTS: @((long)(op.startTS*1000)),
JsonKeyPerformanceCTS:@(deltaTS),
JsonKeyPerformanceSTATUS : @(errorCode)
}];
} error:NULL];
... ... @@ -55,11 +84,30 @@
op.cancelTS = [[NSDate date]timeIntervalSince1970];
long deltaTS = (op.cancelTS - op.startTS) * 1000;
NSLog(@"%@ \n cancel =======>%ld",op.request.URL, deltaTS);
NSString *url = [AFURLConnectionOperation urlWithOperation:op];
NSString *opAddr = [NSString stringWithFormat:@"%p", op];
[[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeAPICall
parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
JsonKeyPerformanceURL: url?:@"",
JsonKeyPerformanceTS: @((long)(op.startTS*1000)),
JsonKeyPerformanceCTS:@(deltaTS),
JsonKeyPerformanceSTATUS : @(0)
}];
} error:NULL];
}
+ (NSString *)urlWithOperation:(AFURLConnectionOperation *)op
{
NSString *url = @"";
if (op && op.request) {
url = [op.request.URL absoluteString];
if ([op.request.HTTPMethod isEqualToString:@"POST"] && ![url containsString:@"http://analytics.yhurl.com/yas_mobile"]) {
NSString *parmeter = [[NSString alloc]initWithData:op.request.HTTPBody encoding:NSUTF8StringEncoding];
url = [url stringByAppendingString:[NSString stringWithFormat:@"/?%@",parmeter]];
}
}
return url;
}
@end
... ...