SDWebImageDownloaderOperation+Performance.m 3.69 KB
//
//  SDWebImageDownloaderOperation+Performance.m
//  SDWebImage
//
//  Created by 王钱钧 on 15/12/24.
//  Copyright © 2015年 Dailymotion. All rights reserved.
//

#import "SDWebImageDownloaderOperation+Performance.h"
#import "Aspects.h"

@implementation SDWebImageDownloaderOperation (Performance)

+ (void)addOperationMonitor
{
    //  start
    [SDWebImageDownloaderOperation aspect_hookSelector:@selector(start) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo>aspectInfo){
        SDWebImageDownloaderOperation *op = [aspectInfo instance];
        op.begainLoadImageTimestamp = [[NSDate date]timeIntervalSince1970];
    } error:NULL];
    
    // finish
    [SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationDidFinish) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
        SDWebImageDownloaderOperation *op = [aspectInfo instance];
        NSInteger status = -1;
        if (op.response) {
            if ([op.response isKindOfClass:[NSHTTPURLResponse class]]) {
                NSHTTPURLResponse *response = (NSHTTPURLResponse *)op.response;
                status = [response statusCode];
            }
            if (status == 200) {
                // success
                status = 1;
            }
        }
        
    } error:NULL];
    
    
    // error
    [SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationError:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
        SDWebImageDownloaderOperation *op = [aspectInfo instance];
        // error code default is `-1`
        NSInteger errorCode = -1;
        if (op.error) {
            errorCode = [op.error code];
        }
    } error:NULL];
    
    // cancel
    //    [SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationCancel) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
    //        SDWebImageDownloaderOperation *op = [aspectInfo instance];
    //
    //        NSTimeInterval currentTS = [[NSDate date]timeIntervalSince1970];
    //
    //        long deltaTS = (currentTS - op.begainLoadImageTimestamp) * 1000;
    //        NSString *url = [SDWebImageDownloaderOperation urlWithOperation:op];
    //        NSString *opAddr = [NSString stringWithFormat:@"%p", op];
    //        NSLog(@"%p \n url = %@ ====cancel===> %ld",op,url,deltaTS);
    //
    ////        [[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeAPICall
    ////                                                  parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
    ////                                                               JsonKeyPerformanceURL: url?:@"",
    ////                                                               JsonKeyPerformanceTS: @((long)(op.begainLoadImageTimestamp*1000)),
    ////                                                               JsonKeyPerformanceCTS:@(deltaTS),
    ////                                                               JsonKeyPerformanceSTATUS : @(0)
    ////                                                               }];
    //    } error:NULL];
    
}

+ (NSString *)urlWithOperation:(SDWebImageDownloaderOperation *)op
{
    NSString *url = @"";
    if (op && op.request) {
        url = [op.request.URL absoluteString];
        
        BOOL containsAnanlyticURL = ([url rangeOfString:@"yas_mobile"].location == NSNotFound) ? NO : YES;
        
        if ([op.request.HTTPMethod isEqualToString:@"POST"] && !containsAnanlyticURL) {
            NSString *parmeter = [[NSString alloc]initWithData:op.request.HTTPBody encoding:NSUTF8StringEncoding];
            url = [url stringByAppendingString:[NSString stringWithFormat:@"/?%@",parmeter]];
        }
    }
    return url;

}



@end