Commit c30bae52 authored by 汪洋's avatar 汪洋

Merge branch 'jql/phobos' into 'master'

埋点库优化

See merge request !51
parents 2fb8eefb 1367b35b
...@@ -66,20 +66,10 @@ NSString *const MockCityId = @"beijing"; ...@@ -66,20 +66,10 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict]; // [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[dict setObject:[PhobosUtil currentTime] forKey:@"in"]; for (int i = 0; i < 10; i++) {
[dict setObject:[PhobosUtil currentTime] forKey:@"out"]; [Phobos track:[NSString stringWithFormat:@"tt-%d", i] attributes:dict sendNow:YES];
[Phobos track:@"page_view" attributes:dict sendNow:YES]; [Phobos track:[NSString stringWithFormat:@"pv-%d", i] attributes:dict sendNow:YES];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; }
[Phobos track:@"test" attributes:dict sendNow:YES];
[Phobos track:@"test2" attributes:dict sendNow:YES];
[Phobos track:@"test3" attributes:dict sendNow:YES];
[Phobos track:@"test4" attributes:dict sendNow:YES];
[Phobos track:@"test5" attributes:dict sendNow:YES];
[Phobos track:@"test6" attributes:dict sendNow:YES];
[Phobos track:@"test7" attributes:dict sendNow:YES];
[Phobos track:@"test8" attributes:dict sendNow:YES];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
} }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
......
PODS: PODS:
- GMCache (1.0.1): - GMCache (1.0.1):
- TMCache (= 2.1.0) - TMCache (= 2.1.0)
- GMPhobos (2.0.0): - GMPhobos (2.0.3):
- GMCache - GMCache
- MagicalRecord - MagicalRecord
- MJExtension - MJExtension
...@@ -28,11 +28,11 @@ EXTERNAL SOURCES: ...@@ -28,11 +28,11 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS: SPEC CHECKSUMS:
GMCache: b78d8e46db864405e91d226ce640cc80d966c611 GMCache: b78d8e46db864405e91d226ce640cc80d966c611
GMPhobos: 3cad92cfa28da2b719ccef040a8fb4e33012b1ad GMPhobos: 6bd14d7b98841b6a3e35c5aa8b1f091ad07bbbf7
MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755 MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545 MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad
COCOAPODS: 1.9.0 COCOAPODS: 1.9.1
...@@ -117,12 +117,14 @@ NSString *const MockCityId = @"beijing"; ...@@ -117,12 +117,14 @@ NSString *const MockCityId = @"beijing";
*/ */
- (void)testTrackEventWithAttrAndSendNowDonotClearNormal { - (void)testTrackEventWithAttrAndSendNowDonotClearNormal {
// Given // Given
[Phobos removeAllPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"}; NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"}; NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
// When // When
[Phobos track:MockEventId attributes:attr sendNow:NO]; [Phobos track:MockEventId attributes:attr sendNow:NO];
// When // When
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES]; [Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array should be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array should be empty");
} }
...@@ -132,6 +134,7 @@ NSString *const MockCityId = @"beijing"; ...@@ -132,6 +134,7 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4 * @since 1.1.4
*/ */
- (void)testTrackEventWithDoubleAttrAndSendNow { - (void)testTrackEventWithDoubleAttrAndSendNow {
[Phobos removeAllPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"}; NSDictionary *attr = @{@"attr":@"track_attr"};
[Phobos track:MockEventId attributes:attr sendNow:YES]; [Phobos track:MockEventId attributes:attr sendNow:YES];
...@@ -160,9 +163,9 @@ NSString *const MockCityId = @"beijing"; ...@@ -160,9 +163,9 @@ NSString *const MockCityId = @"beijing";
[Phobos track:MockEventId attributes:attr]; [Phobos track:MockEventId attributes:attr];
} }
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 2, @"array shouldn't be empty");
} }
/** /**
...@@ -221,10 +224,10 @@ NSString *const MockCityId = @"beijing"; ...@@ -221,10 +224,10 @@ NSString *const MockCityId = @"beijing";
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
[Phobos track:MockEventId attributes:attr]; [Phobos track:MockEventId attributes:attr];
} }
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array shouldn't be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr]; [Phobos track:MockEventId attributes:attr];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 2, @"array shouldn't be empty");
} }
- (void)verfiyDict:(NSDictionary *)dict{ - (void)verfiyDict:(NSDictionary *)dict{
......
...@@ -32,6 +32,8 @@ static NewPhobos *_sharedClient; ...@@ -32,6 +32,8 @@ static NewPhobos *_sharedClient;
@implementation NewPhobos @implementation NewPhobos
static dispatch_semaphore_t _phobos_semaphore;
+ (NewPhobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{ + (NewPhobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{
NewPhobos.sharedClient.appName = appName; NewPhobos.sharedClient.appName = appName;
NewPhobos.sharedClient.channelId = channelId; NewPhobos.sharedClient.channelId = channelId;
...@@ -40,6 +42,7 @@ static NewPhobos *_sharedClient; ...@@ -40,6 +42,7 @@ static NewPhobos *_sharedClient;
- (instancetype)init { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_phobos_semaphore = dispatch_semaphore_create(1);
_appName = @""; _appName = @"";
_channelId = @""; _channelId = @"";
_logEnabled = NO; _logEnabled = NO;
...@@ -420,16 +423,14 @@ static NewPhobos *_sharedClient; ...@@ -420,16 +423,14 @@ static NewPhobos *_sharedClient;
* 处理发送数据 * 处理发送数据
*/ */
+ (void)disposeSendDataWithImmediately:(BOOL)immediately { + (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities]; NSInteger count = [PhobosDataManager fetchCountOfToBeSendEntities];
if (immediately || entities.count > PhobosShardCount) { if (immediately || count >= PhobosShardCount) {
dispatch_semaphore_wait(_phobos_semaphore, DISPATCH_TIME_FOREVER);
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending]; [PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending];
dispatch_semaphore_signal(_phobos_semaphore);
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) { [PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
if (code == 200) { [PhobosDataManager updateDataEntities:finishEntities sendStatus:(code == 200 ? PhobosDataSendStatusFinish : PhobosDataSendStatusError)];
[PhobosDataManager updateDataEntities:finishEntities sendStatus:PhobosDataSendStatusFinish];
[PhobosDataManager deleteDataEntities:finishEntities];
} else {
[PhobosDataManager updateDataEntities:finishEntities sendStatus:PhobosDataSendStatusError];
}
}]; }];
} }
} }
...@@ -443,7 +444,7 @@ static NewPhobos *_sharedClient; ...@@ -443,7 +444,7 @@ static NewPhobos *_sharedClient;
return [PhobosDataManager fetchCountOfToBeSendEntities]; return [PhobosDataManager fetchCountOfToBeSendEntities];
} }
/** 获取待发送埋点数据 */ /** 获取待发送埋点数据, 用同步来保障异步获取数据 */
+ (NSArray *)fetchToBeSendPhobosData { + (NSArray *)fetchToBeSendPhobosData {
return [PhobosDataManager fetchToBeSendDataEntities]; return [PhobosDataManager fetchToBeSendDataEntities];
} }
......
...@@ -16,7 +16,7 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) { ...@@ -16,7 +16,7 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
PhobosSigningTypeDebug PhobosSigningTypeDebug
}; };
static NSString *sdkVersion = @"2.0.2"; static NSString *sdkVersion = @"2.0.3";
#ifdef DEBUG #ifdef DEBUG
#define phobosLog(...) NSLog(@"[Phobos] %@",__VA_ARGS__) #define phobosLog(...) NSLog(@"[Phobos] %@",__VA_ARGS__)
......
...@@ -14,13 +14,16 @@ ...@@ -14,13 +14,16 @@
@implementation PhobosDataManager @implementation PhobosDataManager
static NSManagedObjectContext *Phobos_defaultContext; static NSManagedObjectContext *PhobosDefaultContext;
static dispatch_semaphore_t phobos_semaphore_t;
+ (void)initialize { + (void)initialize {
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
phobos_semaphore_t = dispatch_semaphore_create(1);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用 // 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
Phobos_defaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; PhobosDefaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSBundle *bundle = [NSBundle bundleForClass:[self class]];
// 获取埋点数据的实体 // 获取埋点数据的实体
NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"]; NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"];
...@@ -36,7 +39,7 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -36,7 +39,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:nil]; [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:nil];
// 设置storeCoordinator // 设置storeCoordinator
Phobos_defaultContext.persistentStoreCoordinator = coordinator; PhobosDefaultContext.persistentStoreCoordinator = coordinator;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */ /** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
...@@ -56,7 +59,7 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -56,7 +59,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
} }
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter { + (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:Phobos_defaultContext]; return [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext];
} }
+ (NSUInteger)fetchCountOfToBeSendEntities { + (NSUInteger)fetchCountOfToBeSendEntities {
...@@ -65,7 +68,7 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -65,7 +68,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
} }
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter { + (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:Phobos_defaultContext] integerValue]; return [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:PhobosDefaultContext] integerValue];
} }
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI { + (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI {
...@@ -76,11 +79,13 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -76,11 +79,13 @@ static NSManagedObjectContext *Phobos_defaultContext;
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) { if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return; return;
} }
PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:Phobos_defaultContext]; dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER);
PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:PhobosDefaultContext];
entity.data = [data mj_JSONData]; entity.data = [data mj_JSONData];
entity.api = sendAPI; entity.api = sendAPI;
entity.status = PhobosDataSendStatusToBeSend; entity.status = PhobosDataSendStatusToBeSend;
entity.id = mach_absolute_time(); entity.id = mach_absolute_time();
dispatch_semaphore_signal(phobos_semaphore_t);
[self saveWithCompletion:completion]; [self saveWithCompletion:completion];
} }
...@@ -90,24 +95,30 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -90,24 +95,30 @@ static NSManagedObjectContext *Phobos_defaultContext;
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion { + (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion {
if (entities.count > 0) { if (entities.count > 0) {
dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER);
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) { [entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.status = sendStatus; obj.status = sendStatus;
}]; }];
dispatch_semaphore_signal(phobos_semaphore_t);
[self saveWithCompletion:completion]; [self saveWithCompletion:completion];
} }
} }
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities { + (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities {
if (entities.count > 0) { if (entities.count > 0) {
dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER);
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) { [entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj MR_deleteEntityInContext:Phobos_defaultContext]; [obj MR_deleteEntityInContext:PhobosDefaultContext];
}]; }];
dispatch_semaphore_signal(phobos_semaphore_t);
[self saveWithCompletion:nil]; [self saveWithCompletion:nil];
} }
} }
+ (void)deleteAllEntities { + (void)deleteAllEntities {
[PhobosSendDataEntity MR_truncateAllInContext:Phobos_defaultContext]; dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER);
[PhobosSendDataEntity MR_truncateAllInContext:PhobosDefaultContext];
dispatch_semaphore_signal(phobos_semaphore_t);
[self saveWithCompletion:nil]; [self saveWithCompletion:nil];
} }
...@@ -115,7 +126,7 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -115,7 +126,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
* 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。 * 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
*/ */
+ (void)saveWithCompletion:(MRSaveCompletionHandler)completion { + (void)saveWithCompletion:(MRSaveCompletionHandler)completion {
[Phobos_defaultContext MR_saveOnlySelfWithCompletion:completion]; [PhobosDefaultContext MR_saveOnlySelfWithCompletion:completion];
} }
@end @end
...@@ -84,7 +84,10 @@ ...@@ -84,7 +84,10 @@
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<PhobosSendDataEntity *> *entities, BOOL * _Nonnull stop) { [sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<PhobosSendDataEntity *> *entities, BOOL * _Nonnull stop) {
NSMutableArray *sendDatas = [NSMutableArray new]; NSMutableArray *sendDatas = [NSMutableArray new];
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) { [entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDatas addObject:[obj.data mj_JSONObject]]; id data = [obj.data mj_JSONObject];
if (data) {
[sendDatas addObject:data];
}
}]; }];
if (sendDatas.count > 0) { if (sendDatas.count > 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment