...
|
...
|
@@ -14,7 +14,7 @@ |
|
|
#import "YHEvent.h"
|
|
|
#import "YHCrashReporter.h"
|
|
|
|
|
|
#define kAnalizedURL @"http://test.analy.yoho.cn/log.php"
|
|
|
#define kAnalizedURL @"http://analytics.yhurl.com/mobile" // analytics.yhurl.com/mobile //test.analy.yoho.cn/log.php
|
|
|
#define kPathCaches [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
|
|
|
#define kPathLogSystem [kPathCaches stringByAppendingPathComponent:@"YHLogSystem"]
|
|
|
|
...
|
...
|
@@ -32,6 +32,7 @@ |
|
|
|
|
|
//#define kMaxInterval 10
|
|
|
|
|
|
NSString * const JsonKeyData = @"data";
|
|
|
NSString * const JsonKeyDataTypeDevice = @"device";
|
|
|
NSString * const JsonKeyDataTypeEvents = @"events";
|
|
|
NSString * const JsonKeyDataTypeErrors = @"errors";
|
...
|
...
|
@@ -47,7 +48,8 @@ NSString * const JsonKeyDataTypeErrors = @"errors"; |
|
|
@property (strong, nonatomic) NSString *uid;
|
|
|
@property (strong, nonatomic) NSTimer *timer;
|
|
|
|
|
|
@property (strong, nonatomic) NSMutableDictionary *immediUploadDataDic; // 用于立即发送策略下(device信息是相同的)
|
|
|
@property (strong, nonatomic) NSMutableDictionary *immediUploadDataDic;
|
|
|
@property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic; // 用于立即发送策略下(device信息是相同的)
|
|
|
@property (strong, nonatomic) Reachability *reachability;
|
|
|
@property (strong, nonatomic) CLLocationManager *locationManager;
|
|
|
|
...
|
...
|
@@ -90,16 +92,15 @@ static dispatch_queue_t persisitingQueue; |
|
|
|
|
|
self.reachability = [Reachability reachabilityWithHostName:kAnalizedURL];
|
|
|
|
|
|
self.immediUploadDataDic = [[NSMutableDictionary alloc]initWithCapacity:3];
|
|
|
self.immediUploadDataDic = [[NSMutableDictionary alloc]initWithCapacity:1];
|
|
|
self.immediUploadItemDic = [[NSMutableDictionary alloc]initWithCapacity:3];
|
|
|
persisitingQueue = dispatch_queue_create("com.yoho.analytics.persisiting", NULL);
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
selector:@selector(reachabilityChanged:)
|
|
|
name:kReachabilityChangedNotification
|
|
|
object:nil];
|
|
|
|
|
|
|
|
|
|
|
|
[self.reachability startNotifier];
|
|
|
}
|
|
|
|
...
|
...
|
@@ -153,7 +154,8 @@ static dispatch_queue_t persisitingQueue; |
|
|
self.device.sid = self.session;
|
|
|
self.device.ak = appId;
|
|
|
|
|
|
[self.immediUploadDataDic setObject:self.device.jsonDictionary forKey:JsonKeyDataTypeDevice];
|
|
|
[self.immediUploadItemDic setObject:self.device.jsonDictionary forKey:JsonKeyDataTypeDevice];
|
|
|
[self.immediUploadDataDic setObject:self.immediUploadItemDic forKey:JsonKeyData];
|
|
|
// 启动是发送
|
|
|
if (self.logStrategy == LogStrategyAppLaunch) {
|
|
|
[self uploadDiskData];
|
...
|
...
|
@@ -229,39 +231,47 @@ static dispatch_queue_t persisitingQueue; |
|
|
NSMutableDictionary *collectData = [[NSMutableDictionary alloc]initWithContentsOfFile:self.eventFileName];
|
|
|
if (!collectData) {
|
|
|
collectData = [[NSMutableDictionary alloc]init];
|
|
|
NSMutableDictionary *items = [[NSMutableDictionary alloc]init];
|
|
|
|
|
|
//直接添加 immediUploadDataDic 内容(里面包含 device 信息)
|
|
|
[collectData addEntriesFromDictionary:self.immediUploadDataDic];
|
|
|
[items addEntriesFromDictionary:self.immediUploadItemDic];
|
|
|
|
|
|
//组合event信息
|
|
|
if (event.jsonDictionary) {
|
|
|
NSMutableArray *events = [[NSMutableArray alloc]initWithObjects:event.jsonDictionary,nil];
|
|
|
self.allEventsCount = [events count];
|
|
|
[collectData setObject:events forKey:JsonKeyDataTypeEvents];
|
|
|
[items setObject:events forKey:JsonKeyDataTypeEvents];
|
|
|
}
|
|
|
|
|
|
[collectData setObject:items forKey:JsonKeyData];
|
|
|
|
|
|
} else { // 持久化文件不为空
|
|
|
// 含有event信息
|
|
|
if ([collectData.allKeys containsObject:JsonKeyDataTypeEvents]) {
|
|
|
NSMutableArray *oldEvents = [collectData objectForKey:JsonKeyDataTypeEvents];
|
|
|
NSMutableDictionary *items = [collectData objectForKey:JsonKeyData];
|
|
|
|
|
|
if ([items.allKeys containsObject:JsonKeyDataTypeEvents]) {
|
|
|
NSMutableArray *oldEvents = [items objectForKey:JsonKeyDataTypeEvents];
|
|
|
if (!oldEvents) {
|
|
|
oldEvents = [[NSMutableArray alloc]init];
|
|
|
}
|
|
|
[oldEvents addObject:event.jsonDictionary];
|
|
|
self.allEventsCount = [oldEvents count];
|
|
|
[collectData setObject:oldEvents forKey:JsonKeyDataTypeEvents];
|
|
|
[items setObject:oldEvents forKey:JsonKeyDataTypeEvents];
|
|
|
|
|
|
} else {
|
|
|
NSMutableArray *newEvents = [[NSMutableArray alloc]initWithObjects:event.jsonDictionary, nil];
|
|
|
self.allEventsCount = [newEvents count];
|
|
|
[collectData setObject:newEvents forKey:JsonKeyDataTypeEvents];
|
|
|
[items setObject:newEvents forKey:JsonKeyDataTypeEvents];
|
|
|
}
|
|
|
|
|
|
[collectData setObject:items forKey:JsonKeyData];
|
|
|
}
|
|
|
|
|
|
BOOL success = [collectData writeToFile:self.eventFileName atomically:YES];
|
|
|
|
|
|
if (success) {
|
|
|
// 当event数量大于阀值或者文件超过指定大小
|
|
|
|
|
|
// 当event数量大于阀值或者文件超过指定大小
|
|
|
unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.eventFileName error:NULL] fileSize];
|
|
|
|
|
|
if (self.allEventsCount > kMaxLocalEventsCount || fileSize > kMaxLocalRecoderFileSize) {
|
...
|
...
|
@@ -275,8 +285,7 @@ static dispatch_queue_t persisitingQueue; |
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// 持久化 error 数据
|
...
|
...
|
@@ -306,33 +315,36 @@ static dispatch_queue_t persisitingQueue; |
|
|
- (void)writeToFile:(YHError *)errorData
|
|
|
{
|
|
|
NSMutableDictionary *collectData = [[NSMutableDictionary alloc]initWithContentsOfFile:self.eventFileName];
|
|
|
NSMutableDictionary *items = nil;
|
|
|
if (!collectData) {
|
|
|
collectData = [[NSMutableDictionary alloc]init];
|
|
|
|
|
|
items = [[NSMutableDictionary alloc]init];
|
|
|
//直接添加 immediUploadDataDic 内容(里面包含 device 信息)
|
|
|
[collectData addEntriesFromDictionary:self.immediUploadDataDic];
|
|
|
[items addEntriesFromDictionary:self.immediUploadItemDic];
|
|
|
|
|
|
//组合error信息
|
|
|
if (errorData.jsonDictionary) {
|
|
|
NSMutableArray *errors = [[NSMutableArray alloc]initWithObjects:errorData.jsonDictionary,nil];
|
|
|
[collectData setObject:errors forKey:JsonKeyDataTypeErrors];
|
|
|
[items setObject:errors forKey:JsonKeyDataTypeErrors];
|
|
|
}
|
|
|
|
|
|
|
|
|
} else { // 持久化文件不为空
|
|
|
// 含有error信息
|
|
|
if ([collectData.allKeys containsObject:JsonKeyDataTypeErrors]) {
|
|
|
NSMutableArray *oldErrors = [collectData objectForKey:JsonKeyDataTypeErrors];
|
|
|
items = [collectData objectForKey:JsonKeyData];
|
|
|
if ([items.allKeys containsObject:JsonKeyDataTypeErrors]) {
|
|
|
NSMutableArray *oldErrors = [items objectForKey:JsonKeyDataTypeErrors];
|
|
|
if (!oldErrors) {
|
|
|
oldErrors = [[NSMutableArray alloc]init];
|
|
|
}
|
|
|
[oldErrors addObject:errorData.jsonDictionary];
|
|
|
[collectData setObject:oldErrors forKey:JsonKeyDataTypeErrors];
|
|
|
[items setObject:oldErrors forKey:JsonKeyDataTypeErrors];
|
|
|
} else {
|
|
|
NSMutableArray *newErrors = [[NSMutableArray alloc]initWithObjects:errorData.jsonDictionary, nil];
|
|
|
[collectData setObject:newErrors forKey:JsonKeyDataTypeErrors];
|
|
|
[items setObject:newErrors forKey:JsonKeyDataTypeErrors];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[collectData setObject:items forKey:JsonKeyData];
|
|
|
[collectData writeToFile:self.eventFileName atomically:YES];
|
|
|
|
|
|
}
|
...
|
...
|
@@ -388,8 +400,8 @@ static dispatch_queue_t persisitingQueue; |
|
|
- (void)uploadImmedilyWhithEvent:(YHEvent *)event
|
|
|
{
|
|
|
if (event) {
|
|
|
[self.immediUploadDataDic removeObjectForKey:JsonKeyDataTypeErrors];
|
|
|
[self.immediUploadDataDic setObject:[[NSMutableArray alloc]initWithObjects:event.jsonDictionary, nil] forKey:JsonKeyDataTypeEvents];
|
|
|
[self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];
|
|
|
[self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:event.jsonDictionary, nil] forKey:JsonKeyDataTypeEvents];
|
|
|
[self postWithJsonData:self.immediUploadDataDic];
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -397,8 +409,8 @@ static dispatch_queue_t persisitingQueue; |
|
|
- (void)uploadImmedilyWhithErrorData:(YHError *)error
|
|
|
{
|
|
|
if (error) {
|
|
|
[self.immediUploadDataDic removeObjectForKey:JsonKeyDataTypeEvents];
|
|
|
[self.immediUploadDataDic setObject:[[NSMutableArray alloc]initWithObjects:error.jsonDictionary, nil] forKey:JsonKeyDataTypeErrors];
|
|
|
[self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];
|
|
|
[self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:error.jsonDictionary, nil] forKey:JsonKeyDataTypeErrors];
|
|
|
[self postWithJsonData:self.immediUploadDataDic];
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -537,7 +549,7 @@ static dispatch_queue_t persisitingQueue; |
|
|
self.device.la = [NSString stringWithFormat:@"%f",latitude];
|
|
|
|
|
|
// 更新经纬度
|
|
|
[self.immediUploadDataDic setObject:self.device.jsonDictionary forKey:JsonKeyDataTypeDevice];
|
|
|
[self.immediUploadItemDic setObject:self.device.jsonDictionary forKey:JsonKeyDataTypeDevice];
|
|
|
|
|
|
}
|
|
|
|
...
|
...
|
|