Commit 7b79a83e authored by 乔金柱's avatar 乔金柱

Merge branch 'jql/phobos' into 'master'

重构埋点库

See merge request !42
parents 3d867177 9adf3bfc
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>classNames</key>
<dict>
<key>GMPhobosUtilTest</key>
<dict>
<key>testDataCompressPerformance</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.000579</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
</dict>
</dict>
</dict>
</dict>
</plist>
......@@ -35,6 +35,37 @@
<string>com.apple.platform.iphonesimulator</string>
</dict>
</dict>
<key>D343427D-396A-4DA4-A74B-7C92AD89F7A7</key>
<dict>
<key>localComputer</key>
<dict>
<key>busSpeedInMHz</key>
<integer>100</integer>
<key>cpuCount</key>
<integer>1</integer>
<key>cpuKind</key>
<string>Intel Core i7</string>
<key>cpuSpeedInMHz</key>
<integer>2300</integer>
<key>logicalCPUCoresPerPackage</key>
<integer>8</integer>
<key>modelCode</key>
<string>MacBookPro11,2</string>
<key>physicalCPUCoresPerPackage</key>
<integer>4</integer>
<key>platformIdentifier</key>
<string>com.apple.platform.macosx</string>
</dict>
<key>targetArchitecture</key>
<string>x86_64</string>
<key>targetDevice</key>
<dict>
<key>modelCode</key>
<string>iPhone10,5</string>
<key>platformIdentifier</key>
<string>com.apple.platform.iphonesimulator</string>
</dict>
</dict>
</dict>
</dict>
</plist>
......@@ -36,12 +36,11 @@ NSString *const MockCityId = @"beijing";
NSString *url = @"http://log.test.gengmei.cc/log/collect";
#endif
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
Phobos *phobos = [Phobos clientWithAppName:MockAppName channelId:MockChannelId];
phobos.serverAPI = url;
[phobos setLogEnabled:NO]; // 调试打Log模式,看情况开启
phobos.signingType = PhobosSigningTypeDebug;
phobos.userId = @"";
[Phobos setSharedClient:phobos];
[Phobos clientWithAppName:MockAppName channelId:MockChannelId];
Phobos.sharedClient.serverAPI = url;
[Phobos.sharedClient setLogEnabled:NO]; // 调试打Log模式,看情况开启
Phobos.sharedClient.signingType = PhobosSigningTypeDebug;
Phobos.sharedClient.userId = @"";
NSString *inDate = [PhobosUtil currentTime];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
......
......@@ -35,9 +35,9 @@ PODS:
- GMKit/Protocol (1.1.3):
- Masonry (= 1.1.0)
- SDWebImage (= 3.7.6)
- GMPhobos (1.2.9):
- GMCache (= 0.2.3)
- GMKit (= 1.1.3)
- GMPhobos (1.3.0):
- GMCache
- GMKit
- Masonry (1.1.0)
- SDWebImage (3.7.6):
- SDWebImage/Core (= 3.7.6)
......@@ -63,7 +63,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
GMCache: 09a3029c96fe130e3a21faef70b3d9d2ce92d639
GMKit: 35f788243cceeddf3e13c5226b3ea0b5e08e2117
GMPhobos: 89d9654a951819abc9285b392e055278d10877f2
GMPhobos: c1dd33760f8d4243e6bf562a115899e66c6ec7d2
Masonry: 678fab65091a9290e40e2832a55e7ab731aad201
SDWebImage: c325cf02c30337336b95beff20a13df489ec0ec9
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
......
......@@ -7,8 +7,8 @@
//
#import <XCTest/XCTest.h>
#import <GMPhobos/Phobos.h>
#import "PhobosUtil.h"
@import GMPhobos;
@interface GMPhobosUtilTest : XCTestCase
@property (nonatomic, retain) NSMutableArray *mockArray;
......@@ -19,7 +19,7 @@
- (void)setUp {
[super setUp];
NSString *url = @"http://log.test.gengmei.cc/log/collect";
[Phobos setSharedClient:[Phobos clientWithAppName:@"gengmei_test" channelId:@"AppStore"]];
[Phobos clientWithAppName:@"gengmei_test" channelId:@"AppStore"];
[Phobos sharedClient].serverAPI = url;
_mockArray = [[NSMutableArray alloc] init];
for (int i=0; i<50; i++) {
......
......@@ -9,8 +9,7 @@
#import <XCTest/XCTest.h>
#import <GMPhobos/Phobos.h>
#import "GMPhobosController.h"
#define PhobosCacheKey @"PhobosCacheKey"
@import GMCache;
#import <GMCache/GMCache.h>
NSString *const MockAppName = @"gengmei_test";
NSString *const MockChannelId = @"AppStore";
......@@ -19,26 +18,25 @@ NSString *const MockUserId = @"1";
NSString *const MockCityId = @"beijing";
@interface GMPhotoTest : XCTestCase
@property Phobos *client;
@end
///[NSThread sleepForTimeInterval:0.2]; 因为是异步处理数据,所以需要延迟获取,进行单元测试
@implementation GMPhotoTest
- (void)setUp {
[super setUp];
_client = [Phobos clientWithAppName:MockAppName channelId:MockChannelId];
_client.serverAPI = @"http://log.test.igengmei.com/log/collect";
[_client setLogEnabled:NO]; // 调试打Log模式,看情况开启
_client.signingType = PhobosSigningTypeDebug;
_client.userId = @"";
[Phobos setSharedClient:_client];
[Phobos clientWithAppName:MockAppName channelId:MockChannelId];
Phobos.sharedClient.serverAPI = @"http://log.test.igengmei.com/log/collect";
Phobos.sharedClient.logEnabled = NO; // 调试打Log模式,看情况开启
Phobos.sharedClient.signingType = PhobosSigningTypeDebug;
Phobos.sharedClient.userId = @"";
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
[GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKey];
[Phobos removeAllNormalPhobosData];
[Phobos removeAllImmediatelyPhobosData];
}
/**
......@@ -47,10 +45,12 @@ NSString *const MockCityId = @"beijing";
* @since <#version number#>
*/
- (void)testClientWithUserId{
[_client setUserId:MockUserId];
[_client setCurrentCityId:MockCityId];
[Phobos removeAllNormalPhobosData];
[Phobos.sharedClient setUserId:MockUserId];
[Phobos.sharedClient setCurrentCityId:MockCityId];
[Phobos track:MockEventId];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[NSThread sleepForTimeInterval:0.2];
NSArray *array = [Phobos normalPhobosDataForServerAPI];
XCTAssertTrue(array.count == 1, @"array is empty");
NSDictionary *dict = [array objectAtIndex:0];
[self verfiyDict:dict];
......@@ -64,7 +64,8 @@ NSString *const MockCityId = @"beijing";
*/
- (void)testTrackEventWithoutAttr{
[Phobos track:MockEventId];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[NSThread sleepForTimeInterval:0.2];
NSArray *array = [Phobos normalPhobosDataForServerAPI];
XCTAssertTrue(array.count == 1, @"array is empty");
NSDictionary *dict = [array objectAtIndex:0];
[self verfiyDict:dict];
......@@ -79,7 +80,8 @@ NSString *const MockCityId = @"beijing";
- (void)testTrackEventWithAttr{
NSDictionary *attr = @{@"attr":@"track_attr"};
[Phobos track:MockEventId attributes:attr];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[NSThread sleepForTimeInterval:0.2];
NSArray *array = [Phobos normalPhobosDataForServerAPI];
XCTAssertTrue(array.count == 1, @"array is empty");
NSDictionary *dict = [array objectAtIndex:0];
[self verfiyDict:dict];
......@@ -95,13 +97,13 @@ NSString *const MockCityId = @"beijing";
* @since <#version number#>
*/
- (void)testTrackEventWithAttrAndSendNow{
[Phobos removeAllNormalPhobosData];
// Given
NSDictionary *attr = @{@"attr":@"track_attr"};
// When
[Phobos track:MockEventId attributes:attr sendNow:YES];
// Then
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array should be empty");
XCTAssertTrue([Phobos normalPhobosCount] == 0, @"array should be empty");
}
/**
......@@ -118,8 +120,7 @@ NSString *const MockCityId = @"beijing";
// When
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count != 0, @"array shouldn't be empty");
XCTAssertTrue([Phobos normalPhobosCount] != 0, @"array shouldn't be empty");
}
/**
......@@ -133,16 +134,16 @@ NSString *const MockCityId = @"beijing";
// 因为实时埋点是异步删除,所以这个位置暂时延时取数据,待优化 TODO
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
NSArray *array = [Phobos immediatelyPhobosForServerAPI];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr sendNow:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
NSArray *array = [Phobos immediatelyPhobosForServerAPI];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
array = [Phobos normalPhobosDataForServerAPI];
XCTAssertTrue([Phobos normalPhobosCount] == 0, @"array shouldn't be empty");
});
});
}
......@@ -153,23 +154,27 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
- (void)testTrackEventWithDoubleAttrAndNoSendNow {
[Phobos removeAllNormalPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"};
//
[NSThread sleepForTimeInterval:0.2];
[Phobos track:MockEventId attributes:attr];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 1, @"array shouldn't be empty");
// PhobosCacheKey超过50条数据会自动上报,模拟此情况
for (int i = 0; i < 50; i++) {
[Phobos track:MockEventId attributes:attr];
}
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 1, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 2, @"array shouldn't be empty");
}
/**
......@@ -178,35 +183,36 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
- (void)testTrackEventWithSendNowAndNoSendNowOne {
[Phobos removeAllNormalPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
for (int i = 0; i < 30; i++) {
[Phobos track:MockEventId attributes:attr];
}
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 30, @"array shouldn't be empty");
// PhobosCacheKey超过50条数据会自动上报,模拟此情况
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 30, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 30, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 30, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 31, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 31, @"array shouldn't be empty");
}
/**
......@@ -215,33 +221,34 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
- (void)testTrackEventWithSendNowAndNoSendNowTwo {
[Phobos removeAllNormalPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 2, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 2, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 3, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 3, @"array shouldn't be empty");
}
/**
......@@ -250,23 +257,24 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
- (void)testTrackEventWithSendNowAndNoSendNowThree {
[Phobos removeAllNormalPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"};
//
[Phobos track:MockEventId attributes:attr];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 1, @"array shouldn't be empty");
// PhobosCacheKey超过50条数据会自动上报,模拟此情况
for (int i = 0; i < 50; i++) {
[Phobos track:MockEventId attributes:attr];
}
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 2, @"array shouldn't be empty");
}
- (void)verfiyDict:(NSDictionary *)dict{
......@@ -281,7 +289,7 @@ NSString *const MockCityId = @"beijing";
}
- (void)testCatchNullInAttributes {
_client.captureNullExpection = ^(NSString *eventId, NSDictionary *att) {
Phobos.sharedClient.captureNullExpection = ^(NSString *eventId, NSDictionary *att) {
NSCAssert([att[@"bussness_id"] integerValue] == 1244, @"testCatchNullInAttributes 没有捕获到有用信息");
};
NSDictionary *attributes = @{@"key": [NSNull null],
......@@ -309,7 +317,8 @@ NSString *const MockCityId = @"beijing";
[controller viewWillAppear:true];
[controller viewWillDisappear:true];
[self paramUnNilCheck];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[NSThread sleepForTimeInterval:0.2];
NSArray *array = [Phobos normalPhobosDataForServerAPI];
XCTAssertTrue(array.count != 0, @"PhobosCacheKey 下面应该有数据");
NSDictionary *dic = array[0][@"params"];
......@@ -318,12 +327,14 @@ NSString *const MockCityId = @"beijing";
}
- (void)testSimulativePageViewEvent {
[_client simulativePV:nil businessId:nil referer:nil];
[NSThread sleepForTimeInterval:0.2];
[Phobos.sharedClient simulativePV:nil businessId:nil referer:nil];
[self paramUnNilCheck];
}
- (void)paramUnNilCheck {
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[NSThread sleepForTimeInterval:0.2];
NSArray *array = [Phobos normalPhobosDataForServerAPI];
XCTAssertTrue(array.count != 0, @"PhobosCacheKey 下面应该有数据");
NSDictionary *dic = array[0][@"params"];
......@@ -341,8 +352,9 @@ NSString *const MockCityId = @"beijing";
XCTAssertFalse(controller.needLogPV, @"needLogPV赋值为NO时应该NO");
}
// 该单元测试暂时不用,因为过滤代码暂时不用,所以不需要跑通
- (void)testCheckPVPhobos {
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
[Phobos removeAllNormalPhobosData];
NSString *inDate = [PhobosUtil currentTime];
[NSThread sleepForTimeInterval:1];//模拟浏览页面,让out和in时间相差1s
......@@ -358,12 +370,12 @@ NSString *const MockCityId = @"beijing";
[dict setObject:@"" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"];
[Phobos track:@"page_view" attributes:dict];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
NSArray *array = [Phobos normalPhobosDataForServerAPI];
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue(array.count == 1, @"PhobosCacheKey 下面应该有数据");
[Phobos track:@"page_view" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"PhobosCacheKey 上条数据不应该发送");
XCTAssertTrue([Phobos normalPhobosCount] == 1, @"PhobosCacheKey 上条数据不应该发送");
[NSThread sleepForTimeInterval:2];//模拟浏览页面,让第二次浏览和上一次时间相差2s
......@@ -371,14 +383,14 @@ NSString *const MockCityId = @"beijing";
[NSThread sleepForTimeInterval:1];//模拟浏览页面,让out和in时间相差1s
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
[Phobos track:@"page_view" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"PhobosCacheKey 上条数据应该发送");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount]== 2, @"PhobosCacheKey 上条数据应该发送");
[Phobos track:@"page_view" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"PhobosCacheKey 上条数据不应该发送");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 2, @"PhobosCacheKey 上条数据不应该发送");
[Phobos track:@"test" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 3, @"PhobosCacheKey 上条数据应该发送");
[NSThread sleepForTimeInterval:0.2];
XCTAssertTrue([Phobos normalPhobosCount] == 3, @"PhobosCacheKey 上条数据应该发送");
}
......
......@@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = "GMPhobos"
s.version = "1.3.0"
s.version = "1.3.1"
s.summary = "GM statistic data sdk"
s.description = <<-DESC
......
......@@ -33,13 +33,14 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
*/
+ (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId;
+ (instancetype)sharedClient;
+ (void)setSharedClient:(Phobos *)client;
+ (instancetype) alloc __attribute__((deprecated));
- (instancetype) init __attribute__((deprecated));
+ (instancetype) new __attribute__((deprecated));
#pragma mark - SDK配置
@property (class, readonly, strong) Phobos *sharedClient;
// Phobos在处理业务端传递来的参数时会检查是否某个value为空,如果为空会调用这个block以通知业务层,业务层可以上报这个异常,以助解决问题
@property(nonatomic, copy) void (^captureNullExpection) (NSString *eventId, NSDictionary *info);
......@@ -133,12 +134,12 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
*
* @since 0.0.1
*/
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes;
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow;
+ (void)track:(NSString *)eventId;
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes;
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow;
+ (void)track:(NSString *)eventName;
/**
* @brief 自定义事件,数量统计 7730 精准曝光.
* @brief 自定义事件,数量统计 7730 精准曝光/数据链路.
*
* @param eventId 事件Id
* @attributes 参数
......@@ -146,9 +147,9 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
* @currentAPI 当前传过来的API
* @
*/
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventId currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventName currentAPI:(NSString *)currentAPI;
/**
* @author 翟国钧, 16-02-03 16:02:30
......@@ -183,5 +184,48 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
*/
- (void)simulativePV:(NSString *)pageName businessId:(NSString *)bid referer:(NSString *)referer;
@end
@interface Phobos (UtilTest)
/** 获取所有非立即发送埋点数量 */
+ (NSInteger)normalPhobosCount;
/** 获取所有非立即发送埋点数据 */
+ (NSDictionary *)normalPhobosData;
/** 获取url的非立即发送埋点数量 */
+ (NSInteger)normalPhobosCountForURL:(NSString *)url;
/** 获取serverAPI的非立即发送埋点数据 */
+ (NSArray *)normalPhobosDataForServerAPI;
/** 获取url的非立即发送埋点数据 */
+ (NSArray *)normalPhobosDataForURL:(NSString *)url;
/** 获取所有立即发送埋点数据 */
+ (NSDictionary *)immediatelyPhobosData;
/** 获取serverAPI的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosForServerAPI;
/** 获取url的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosDataForURL:(NSString *)url;
/** 获取url的立即发送埋点数量 */
+ (NSUInteger)immediatelyPhobosCountForURL:(NSString *)url;
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData;
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData;
/** 获取将要发送的数据 */
@property (nonatomic, copy) void (^phobosSendDataBlock)(NSArray *datas);
@end
NS_ASSUME_NONNULL_END
......@@ -15,8 +15,9 @@
#import "PhobosCustomVisibleController.h"
#import "UIDevice+Resolutions.h"
#import <GMCache/GMCache.h>
static Phobos *sharedClient = nil;
static NSString *sdkVersion = @"110";
#import <objc/runtime.h>
static Phobos *_sharedClient;
static NSString *sdkVersion = @"1.3.1";
@interface Phobos ()
@property (strong, nonatomic) UIViewController *visibleController;
......@@ -28,48 +29,84 @@ static NSString *sdkVersion = @"110";
/* 每一条埋点数据的物理ID,自增,生命周期和sessionId相同。特别注意:在sessionOver的时候,要把他置为0 */
@property (assign, nonatomic) NSInteger serialId;
// 用来记录除serverAPI以外的API
@property (strong, nonatomic) NSMutableArray *APIArray;
//@property (nonatomic, weak) dispatch_semaphore_t immediatelySemaphore;// 保障需要立即发送的埋点数据安全
//@property (nonatomic, weak) dispatch_semaphore_t normalSemaphore;// 保障普通埋点数据安全
@property (nonatomic, assign) NSInteger normalCount;// 记录普通埋点数量
@end
@implementation Phobos
+ (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{
return [[self alloc] initWithAppName:appName channelId:channelId];
}
static dispatch_semaphore_t _immediatelySemaphore;
static dispatch_semaphore_t _normalSemaphore;
static dispatch_queue_t _immediatelyQueue;
static dispatch_queue_t _normalQueue;
+ (Phobos *)sharedClient{
return sharedClient;
}
+ (void)setSharedClient:(Phobos *)client{
sharedClient = client;
+ (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{
Phobos.sharedClient.appName = appName;
Phobos.sharedClient.channelId = channelId;
return Phobos.sharedClient;
}
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_immediatelySemaphore = dispatch_semaphore_create(1);
_normalSemaphore = dispatch_semaphore_create(1);
_immediatelyQueue = dispatch_queue_create("immdiately", DISPATCH_QUEUE_CONCURRENT);
_normalQueue = dispatch_queue_create("normal", DISPATCH_QUEUE_CONCURRENT);
});
}
- (instancetype)initWithAppName:(NSString *)appName channelId:(NSString *)channelId{
self = [super init];
if (self) {
_appName = appName;
_channelId = channelId;
_logEnabled = NO;
_userId = @"";
_netStatus = @"";
_currentCityId = @"";
_serverAPI = @"";
_greyType = @"";
_userType = [[NSMutableDictionary alloc] initWithCapacity:0];
_appVersion = [PhobosUtil getAppVersion];
_APIArray = [NSMutableArray array];
_signingType = PhobosSigningTypeUndefined;
- (instancetype)init {
if (self = [super init]) {
self.appName = @"";
self.channelId = @"";
self.logEnabled = NO;
self.userId = @"";
self.netStatus = @"";
self.currentCityId = @"";
self.serverAPI = @"";
self.greyType = @"";
self.userType = [[NSMutableDictionary alloc] initWithCapacity:0];
self.appVersion = [PhobosUtil getAppVersion];
self.signingType = PhobosSigningTypeUndefined;
// self.immediatelySemaphore = dispatch_semaphore_create(1);
// self.normalSemaphore = dispatch_semaphore_create(1);
NSMutableDictionary *dataDict = [[GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey] mutableCopy];
for (NSArray *data in dataDict.allValues) {
self.normalCount += data.count;
}
[self setupNotification];
[self handleSessionStart];
[self synchronizePhobosKey];
phobosLog(@"starts to orbit");
}
return self;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
return Phobos.sharedClient;
}
- (id)copyWithZone:(struct _NSZone *)zone {
return Phobos.sharedClient;
}
+ (Phobos *)sharedClient{// 使用单例设计模式,保证Phobos只有一个实例,并提供了全局访问点
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[super allocWithZone:NULL] init];
});
return _sharedClient;
}
- (void)setAppName:(NSString *)appName channelId:(NSString *)channelId {
self.appName = appName;
self.channelId = channelId;
}
/**
* disk下的PhobosHaveOpenApp只要存在,就把他取出来放到document下(注:当不支持7.6.16版本的时候,干掉这个方法)
*/
......@@ -188,7 +225,7 @@ static NSString *sdkVersion = @"110";
phobosLog(@"handleAppInForeground");
[self handleSessionStart];
[self handleEventDeviceOpened];
[self fetchDataAndSend];
[self sendImmediatelyNormalData];
[self handlePVEventAppInForeground];
}
......@@ -201,7 +238,7 @@ static NSString *sdkVersion = @"110";
phobosLog(@"handleAppInBackgound");
[self handlePVEventAppInBackgound];
[self handleSessionOver];
[self fetchDataAndSend];
[self sendImmediatelyNormalData];
}
/**
......@@ -256,95 +293,6 @@ static NSString *sdkVersion = @"110";
}
}
#pragma mark - track event handler
+ (void)track:(NSString *)eventId{
[Phobos track:eventId attributes:@{}];
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes{
[self track:eventId attributes:attributes sendNow:NO];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
//超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) {
[sharedClient sendArray:array cleanCacheRightNow:YES];
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dict];
if (sendNow) {
NSArray *array = @[dict];
// 实时发送的埋点,不能立即清楚缓存
[sharedClient sendArray:array cleanCacheRightNow:NO];
}else{
[sharedClient save:dict];
}
}
@catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI {
[self track:eventId attributes:attributes sendNow:NO currentAPI:currentAPI];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
//超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) {
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:YES];
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI {
[sharedClient addNewApi:currentAPI]; // 记录新的API
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dict];
if (sendNow) {
NSArray *array = @[dict];
// 实时发送的埋点,不能立即清楚缓存
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:NO];
}else{
[sharedClient save:dict currentAPI:currentAPI];
}
}
@catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
+ (void)track:(NSString *)eventId currentAPI:(NSString *)currentAPI {
[Phobos track:eventId attributes:@{} currentAPI:currentAPI];
}
+ (void)trackJsEvent:(NSString *)jsonString{
@try {
NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
id pa = dict[@"params"];
NSDictionary *json;
if ([pa isKindOfClass:[NSString class]]) {
NSError *jsonError;
NSData *objectData = [pa dataUsingEncoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
[Phobos track:dict[@"type"] attributes:json];
}else{
[Phobos track:dict[@"type"]];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
#pragma mark - PV
- (void)onPVStart:(UIResponder<PhobosPVProtocol> *)page {
// 必须在此处调用一下referer,因为onControllerStart
......@@ -358,7 +306,7 @@ static NSString *sdkVersion = @"110";
if (![PhobosUtil isNonEmpty:page.pageName] || !page.needLogPV) {
return;
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
......@@ -399,45 +347,44 @@ static NSString *sdkVersion = @"110";
}
}
#pragma mark - 事件存储、发送
/**
* @brief 将埋点时间封装成词典数据
*
* @since 0.0.1
*/
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{
[self catchNullForEvent:eventId attributes:attributes];
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventName attributes:(NSDictionary *)attributes{
[self catchNullForEvent:eventName attributes:attributes];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
NSString *currentTime = [PhobosUtil currentTime];
NSMutableDictionary *deviceParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString],@"idfa",
[[[UIDevice currentDevice] identifierForVendor] UUIDString],@"idfv",
[PhobosUtil deviceId],@"device_id",
@"ios",@"device_type",
@"Apple",@"manufacturer",
@(self.gps.coordinate.latitude),@"lat",
@(self.gps.coordinate.longitude),@"lng",
_netStatus,@"is_WiFi",
[PhobosUtil getIPAddress:YES],@"ip",nil];
NSMutableDictionary *deviceParams = [NSMutableDictionary new];
[deviceParams setValue:[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] forKey:@"idfa"];
[deviceParams setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"idfv"];
[deviceParams setValue:[PhobosUtil deviceId] forKey:@"device_id"];
[deviceParams setValue:@"ios" forKey:@"device_type"];
[deviceParams setValue:@"Apple" forKey:@"manufacturer"];
[deviceParams setValue:@(self.gps.coordinate.latitude) forKey:@"lat"];
[deviceParams setValue:@(self.gps.coordinate.longitude) forKey:@"lng"];
[deviceParams setValue:_netStatus forKey:@"is_WiFi"];
[deviceParams setValue:[PhobosUtil getIPAddress:YES] forKey:@"ip"];
[deviceParams setValue:_networkStatus forKey:@"net_type"];
[deviceParams setValue:[UIDevice platform] forKey:@"model"];
[deviceParams setValue:[UIDevice currentDevice].systemVersion forKey:@"sys_version"];
NSMutableDictionary *appParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
_greyType, @"grey_type",
self.appName, @"name",
self.appVersion, @"version",
self.channelId,@"channel",
_userType,@"user_type",
self.currentCityId,@"current_city_id",
@(_serialId++), @"serial_id",nil];
NSMutableDictionary *appParams = [NSMutableDictionary new];
[appParams setValue:_greyType forKey:@"grey_type"];
[appParams setValue:_appName forKey:@"name"];
[appParams setValue:_appVersion forKey:@"version"];
[appParams setValue:_channelId forKey:@"channel"];
[appParams setValue:_userType forKey:@"net_type"];
[appParams setValue:_currentCityId forKey:@"current_city_id"];
[appParams setValue:@(_serialId++) forKey:@"serial_id"];
if (_signingType == PhobosSigningTypeDebug || _signingType == PhobosSigningTypeRelease) {
[dict setObject:@(0) forKey:@"is_release"];
}
[dict setObject:eventId forKey:@"type"];
[dict setObject:eventName forKey:@"type"];
[dict setObject:appParams forKey:@"app"];
[dict setObject:sdkVersion forKey:@"version"];
[dict setObject:deviceParams forKey:@"device"];
......@@ -452,33 +399,190 @@ static NSString *sdkVersion = @"110";
return dict;
}
#pragma mark - helpers
- (void)catchNullForEvent:(NSString *)eventName attributes:(NSDictionary *)attributes {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@try {
for (NSString *key in attributes.allKeys) {
if ([attributes[key] isMemberOfClass:[NSNull class]]) {
if (self.captureNullExpection) {
self.captureNullExpection(eventName, attributes);
}
break;
}
}
} @catch (NSException *exception) {
}
});
}
#pragma mark - track event handler
+ (void)trackJsEvent:(NSString *)jsonString{
@try {
NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
id pa = dict[@"params"];
NSDictionary *json;
if ([pa isKindOfClass:[NSString class]]) {
NSError *jsonError;
NSData *objectData = [pa dataUsingEncoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
[Phobos track:dict[@"type"] attributes:json];
}else{
[Phobos track:dict[@"type"]];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
+ (void)track:(NSString *)eventName{
[self track:eventName attributes:@{} sendNow:NO currentAPI:_sharedClient.serverAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes{
[self track:eventName attributes:attributes sendNow:NO currentAPI:_sharedClient.serverAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{
[self track:eventName attributes:attributes sendNow:sendNow currentAPI:_sharedClient.serverAPI];
}
+ (void)track:(NSString *)eventName currentAPI:(NSString *)currentAPI {
[self track:eventName attributes:@{} sendNow:NO currentAPI:currentAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI {
[self track:eventName attributes:attributes sendNow:NO currentAPI:currentAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI {
NSDictionary *dataDict = [_sharedClient prepareDictionaryForEvent:eventName attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dataDict];
if (sendNow) {
[_sharedClient sendImmediatelyPhobosWithURL:currentAPI data:dataDict];
} else{
[_sharedClient trackPhobosWithURL:currentAPI data:dataDict];
}
}
@catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
#pragma mark - 事件存储、发送
/**
* @brief 保存数据到缓存层
*
* @param data 数据
*
* @since 0.0.1
* 普通埋点
* @param url 接口url
* @param dataArray 埋点数据
* @param nowSend 是否需要发送当前缓存数据(app进入到后台、前台等情况)
*/
- (void)save:(NSDictionary *)data
{
if (_logEnabled) {
phobosLog([NSString stringWithFormat:@"save dictionary: %@",data]);
- (void)sendImmediatelyNormalData {
[self trackPhobosWithURL:nil data:nil immediate:YES];
}
- (void)trackPhobosWithURL:(NSString *)url data:(NSDictionary *)data {
[self trackPhobosWithURL:url data:data immediate:NO];
}
- (void)trackPhobosWithURL:(NSString *)url data:(NSDictionary *)data immediate:(BOOL)immediate {
dispatch_async(_normalQueue, ^{
dispatch_semaphore_wait(_normalSemaphore, DISPATCH_TIME_FOREVER);
NSMutableDictionary *dataDict = [[GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey] mutableCopy];
_normalCount++;
if (!immediate) {
dataDict = [self dataDict:dataDict setObject:data forKey:url];
[GMCache storeObjectAtDocumentPathWithkey:PhobosNormalCacheKey object:dataDict];
}
if (immediate || self.normalCount >= 50) { // 数据超过一定数量 或 进入后台等逻辑,统一进行发送普通埋点数据
[dataDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSArray * _Nonnull obj, BOOL * _Nonnull stop) {
if (obj && obj.count > 0) {
[self sendImmediatelyPhobosWithURL:key data:obj];
}
}];
[Phobos removeAllNormalPhobosData];
}
dispatch_semaphore_signal(_normalSemaphore);
});
}
/**
* 即时发送埋点接口
* @param url : 接口url
* @param data : 使用id类型是因为直接调用实时埋点传过来的是Dict,而普通埋点过来的则是Array类型
*/
- (void)sendImmediatelyPhobosWithURL:(NSString *)url data:(id)data {
dispatch_async(_immediatelyQueue, ^{
dispatch_semaphore_wait(_immediatelySemaphore, DISPATCH_TIME_FOREVER);
__block NSMutableDictionary *dataDict = [[GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey] mutableCopy];
dataDict = [self dataDict:dataDict setObject:data forKey:url];
NSInteger allCount = dataDict.allKeys.count;// 请求个数
__block finishCount = 0;// 请求完成次数,不是请求成功次数,能在最后保证未发送成功的数据,能一次保存下来,等待下次发送
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);// 因为网络请求是异步回调,需要保证数据安全
[dataDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSArray * _Nonnull obj, BOOL * _Nonnull stop) {
[self sendDataWithAPI:key data:obj successBlock:^(NSInteger code) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
finishCount++;
if (code == 200) {// 如果数据请求成功,删除当前数据的缓存
[dataDict removeObjectForKey:key];
}
if (finishCount == allCount) {// 所有数据都请求完成,重新进行数据缓存
[GMCache storeObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey object:dataDict];
dispatch_semaphore_signal(_immediatelySemaphore);
}
dispatch_semaphore_signal(semaphore);
phobosLog(@"✈ ---------- ✈ data arrived Mars");
}];
}];
});
}
/**
* 将数据存入到字典中
* @param dataDict 需要存入的字典
* @param object 需要存入的数据
* @param key 需要将数据存入到dataDict的key所在的数据中
*/
- (NSMutableDictionary *)dataDict:(NSMutableDictionary *)dataDict setObject:(id)object forKey:(NSString *)key {
if (!dataDict) {
dataDict = [NSMutableDictionary new];
}
// 判断数据类型,统一转换成数组
NSMutableArray *dataArray = [dataDict.allKeys containsObject:key] ? [dataDict[key] mutableCopy] : [NSMutableArray new];
NSArray *data = [object isKindOfClass:[NSArray class]] ? object :@[object];
//#ifndef POD_CONFIGURATION_APP_STORE
// [self verifyPVPhobos:dataArray data:object];
//#endif
[dataArray addObjectsFromArray:data];
// 进行数据校验
@try {
NSData *json = [PhobosUtil encodeJSON:dataArray];
[dataDict setValue:dataArray forKey:key];
} @catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE只不能为NSObject ");
}
return dataDict;
}
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
if (dataArray) {
#ifdef POD_CONFIGURATION_APP_STORE
[dataArray addObject:data];
#else
if (data[@"type"] && [data[@"type"] isEqualToString:@"page_view"]) {
NSDictionary *pageParams = data[@"params"];
// 校验PV埋点是否有问题,默认返回NO
- (BOOL)verifyPVPhobos:(NSArray *)dataArray data:(id)data {
if ([data isKindOfClass:[NSDictionary class]]) {
NSDictionary *dataDict = (NSDictionary *)data;
if (dataDict[@"type"] && [dataDict[@"type"] isEqualToString:@"page_view"]) {
NSDictionary *pageParams = dataDict[@"params"];
long long pageInTime = [pageParams[@"in"] longLongValue];
long long pageOutTime = [pageParams[@"out"] longLongValue];
// phobosLog(@"pageInTime------%lld",pageInTime);
// phobosLog(@"pageOutTime------%lld",pageOutTime);
if (pageInTime && pageOutTime) {
__block BOOL checkTimeError;
BOOL checkTimeError = NO;
for (int i = 0; i < dataArray.count; i++) {
NSDictionary *obj = dataArray[i];
......@@ -500,77 +604,29 @@ static NSString *sdkVersion = @"110";
break;
}
}
}
return checkTimeError;
}
}
// 不管成功还是失败 给开发提示方便DEBUG
// phobosLog(@"%s____数据校验成功", __func__);
[dataArray addObject:data];
#endif
} else {
dataArray = [NSMutableArray arrayWithObject:data];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosCacheKey object:dataArray];
}
/**
* @brief 保存数据到缓存层
*
* @param data 数据
*
*/
- (void)save:(NSDictionary *)data currentAPI:(NSString *)currentAPI
{
if (_logEnabled) {
phobosLog([NSString stringWithFormat:@"save dictionary: %@",data]);
}
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
if (dataArray) {
[dataArray addObject:data];
}else{
dataArray = [NSMutableArray arrayWithObject:data];
}
[GMCache storeObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI] object:dataArray];
}
/**
* @brief 从缓存中获取数据,并发送
*
* @since 0.0.1
*/
- (void)fetchDataAndSend{
NSArray *paramsArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
if (paramsArray.count>0) {
[self sendArray:paramsArray cleanCacheRightNow:YES];
}
// 其他接口的埋点
for (NSString *newApi in self.APIArray) {
NSArray *paramsArray = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:newApi]];
if (paramsArray.count>0) {
[self sendArray:paramsArray currentAPI:newApi cleanCacheRightNow:YES];
}
}
return NO;
}
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
- (void)sendArray {
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
// 发送埋点数据
- (void)sendDataWithAPI:(NSString *)api data:(NSArray *)dataArray successBlock:(SendDataSuccessBlock)successBlock {
if (_logEnabled) {
NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
phobosLog([NSString stringWithFormat:@"array prepare to fly --✈: %@", jsonString]);
}
@try {
if (self.phobosSendDataBlock) {
self.phobosSendDataBlock(dataArray);
}
NSData *JSON = [PhobosUtil encodeJSON:dataArray];
NSData *compressedData = [PhobosUtil compressData:JSON];
if (compressedData) {
[PhobosUtil sendData:compressedData success:^(NSInteger code) {
phobosLog(@"✈ ---------- ✈ data arrived Mars");
[GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKey];
}];
[PhobosUtil sendData:compressedData currentAPI:api success:successBlock];
}
}
@catch (NSException *exception) {
......@@ -578,109 +634,76 @@ static NSString *sdkVersion = @"110";
}
}
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
- (void)sendArrayWithCurrentAPI:(NSString *)currentAPI {
NSString *PhobosTempCacheKeyStr = [PhobosUtil MD5String:[PhobosUtil MD5String:currentAPI]]; // 两次加密作为key值,和缓存的key值作区分
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
if (_logEnabled) {
NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
phobosLog([NSString stringWithFormat:@"array prepare to fly --✈: %@", jsonString]);
}
@try {
NSData *JSON = [PhobosUtil encodeJSON:dataArray];
NSData *compressedData = [PhobosUtil compressData:JSON];
if (compressedData) {
[PhobosUtil sendData:compressedData currentAPI:currentAPI success:^(NSInteger code) {
phobosLog(@"✈ ---------- ✈ data arrived Mars");
[GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
}];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
@end
@implementation Phobos (UtilTest)
/** 获取所有非立即发送埋点数量 */
+ (NSInteger)normalPhobosCount {
return Phobos.sharedClient.normalCount;
}
/**
该方法有改动,现在的逻辑是:当前方法只接受发送的请求,然后把数据转存到另一个缓存区,
让sendArray方法负责发送,数据一旦转移到缓存区,就把原有的数据干掉。
@author zhaiguojun 16-10-17 in (null)
@param array 参数
@param clean 是否立即清楚缓存
*/
- (void)sendArray:(NSArray *)array cleanCacheRightNow:(BOOL)clean {
@try {
//1.获取缓存区的数据,把新数据追加进去
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
if (dataArray) {
[dataArray addObjectsFromArray:array];
}else{
dataArray = [NSMutableArray arrayWithArray:array];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosTempCacheKey object:dataArray];
//2.把缓存区的数据发送给服务器
[self sendArray];
//3.把原有的数据删除
if (clean) {
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
/** 获取所有非立即发送埋点数据 */
+ (NSDictionary *)normalPhobosData {
return [GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
}
/**
上边方法新加了一个API字段
*/
- (void)sendArray:(NSArray *)array currentAPI:(NSString *)currentAPI cleanCacheRightNow:(BOOL)clean {
@try {
//1.获取缓存区的数据,把新数据追加进去
NSString *PhobosTempCacheKeyStr = [PhobosUtil MD5String:[PhobosUtil MD5String:currentAPI]]; // 两次加密作为key值,和缓存的key值作区分
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
if (dataArray) {
[dataArray addObjectsFromArray:array];
}else{
dataArray = [NSMutableArray arrayWithArray:array];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr object:dataArray];
//2.把缓存区的数据发送给服务器
[self sendArrayWithCurrentAPI:currentAPI];
//3.把原有的数据删除
[GMCache removeObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
}
@catch (NSException *exception) {
phobosLog(exception);
}
/** 获取url的非立即发送埋点数量 */
+ (NSUInteger)normalPhobosCountForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
NSArray *urlData = dict[url];
return urlData ? urlData.count : 0;
}
#pragma mark - helpers
- (void)catchNullForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@try {
for (NSString *key in attributes.allKeys) {
if ([attributes[key] isMemberOfClass:[NSNull class]]) {
if (self.captureNullExpection) {
self.captureNullExpection(eventId, attributes);
}
break;
}
}
} @catch (NSException *exception) {
}
});
+ (NSArray *)normalPhobosDataForServerAPI {
return [self normalPhobosDataForURL:Phobos.sharedClient.serverAPI];
}
/** 获取url的非立即发送埋点数据 */
+ (NSArray *)normalPhobosDataForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
return dict[url];
}
- (void)addNewApi:(NSString *)api {
for (NSString *item in self.APIArray) {
if ([api isEqualToString:item]) {
break;
}
[self.APIArray addObject:api];
}
+ (NSArray *)immediatelyPhobosForServerAPI {
return [self immediatelyPhobosDataForURL:Phobos.sharedClient.serverAPI];
}
/** 获取所有立即发送埋点数据 */
+ (NSDictionary *)immediatelyPhobosData {
return [GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
}
/** 获取url的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosDataForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
return dict[url];
}
+ (NSUInteger)immediatelyPhobosCountForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
NSArray *urlData = dict[url];
return urlData ? urlData.count : 0;
}
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData {
Phobos.sharedClient.normalCount = 0;
[GMCache removeObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
}
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData {
[GMCache removeObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
}
- (void)setPhobosSendDataBlock:(void (^)(NSArray *))phobosSendDataBlock {
objc_setAssociatedObject(self, @selector(phobosSendDataBlock), phobosSendDataBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(NSArray *))phobosSendDataBlock {
return objc_getAssociatedObject(self, @selector(phobosSendDataBlock));
}
@end
......@@ -18,8 +18,8 @@
#define PhobosHaveOpenApp @"PhobosHaveOpenApp" //是否打开过APP
#define PhobosBeginTime @"PhobosBeginTime" //记录APP打开|从后台启动时的时间戳
#define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳
#define PhobosCacheKey @"PhobosCacheKey" //存放持久化埋点数据的key
#define PhobosTempCacheKey @"PhobosTempCacheKey" //临时存放待发送埋点数据的key
#define PhobosNormalCacheKey @"PhobosNormalCacheKey" //存放持久化埋点数据的key
#define PhobosImmediatelyCacheKey @"PhobosImmediatelyCacheKey" //存放持久化实时埋点数据的key
#define PhobosShardCount 50 //收集数据分段发送的个数
......
......@@ -127,6 +127,8 @@
//没有错误,返回正确;
if (success) {
success(200);
} else {
success(error.code);
}
}
}];
......@@ -150,6 +152,8 @@
//没有错误,返回正确;
if (success) {
success(200);
} else {
success(error.code);
}
}
}];
......
......@@ -25,5 +25,4 @@
*/
- (void)initReferrerTabName;
@end
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