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