Commit 0e050c9b authored by 乔金柱's avatar 乔金柱

Merge branch 'jql/phobos' into 'master'

PV埋点数据校验+单元测试

See merge request !36
parents 8ecf643f c6df8011
<?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>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
......@@ -8,6 +8,16 @@
#import "GMViewController.h"
#import <GMPhobos/Phobos.h>
@import GMCache;
@import GMPhobos.PhobosUtil;
@import GMPhobos.Phobos;
#define PhobosCacheKey @"PhobosCacheKey"
NSString *const MockAppName = @"gengmei_test";
NSString *const MockChannelId = @"AppStore";
NSString *const MockEventId = @"eventId";
NSString *const MockUserId = @"1";
NSString *const MockCityId = @"beijing";
@interface GMViewController ()
......@@ -24,8 +34,40 @@
#else
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];
NSString *inDate = [PhobosUtil currentTime];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
[dict setObject:inDate forKey:@"in"];
[dict setObject:@"test" forKey:@"page_name"];
[dict setObject:@"" forKey:@"business_id"];
[dict setObject:@"" forKey:@"referrer"];
[dict setObject:@(0) forKey:@"fake"];
[dict setObject:@"" forKey:@"referrer_id"];
[dict setObject:@"" forKey:@"extra_param"];
[dict setObject:@"" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"];
[Phobos track:@"page_view" attributes:dict];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[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];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[Phobos track:@"test" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[Phobos sharedClient].serverAPI = url;
}
- (void)didReceiveMemoryWarning
......
......@@ -35,7 +35,7 @@ PODS:
- GMKit/Protocol (0.8.4):
- Masonry (= 1.1.0)
- SDWebImage (= 3.7.6)
- GMPhobos (1.1.2):
- GMPhobos (1.1.6):
- GMCache (= 0.2.3)
- GMKit
- Masonry (1.1.0)
......@@ -63,7 +63,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
GMCache: 09a3029c96fe130e3a21faef70b3d9d2ce92d639
GMKit: a30da06b84e5c4a357d427c70d1b5ec672a1f6a1
GMPhobos: 5d9b29fb16fb7b8f3b2983aaab8b42cefc510baa
GMPhobos: cdc1ac3bd8dbc57eb2efe4ac9a7e6ae9d128fc8f
Masonry: 678fab65091a9290e40e2832a55e7ab731aad201
SDWebImage: c325cf02c30337336b95beff20a13df489ec0ec9
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
......
......@@ -341,6 +341,46 @@ NSString *const MockCityId = @"beijing";
XCTAssertFalse(controller.needLogPV, @"needLogPV赋值为NO时应该NO");
}
- (void)testCheckPVPhobos {
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
NSString *inDate = [PhobosUtil currentTime];
[NSThread sleepForTimeInterval:1];//模拟浏览页面,让out和in时间相差1s
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
[dict setObject:inDate forKey:@"in"];
[dict setObject:@"test" forKey:@"page_name"];
[dict setObject:@"" forKey:@"business_id"];
[dict setObject:@"" forKey:@"referrer"];
[dict setObject:@(0) forKey:@"fake"];
[dict setObject:@"" forKey:@"referrer_id"];
[dict setObject:@"" forKey:@"extra_param"];
[dict setObject:@"" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"];
[Phobos track:@"page_view" attributes:dict];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"PhobosCacheKey 下面应该有数据");
[Phobos track:@"page_view" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"PhobosCacheKey 上条数据不应该发送");
[NSThread sleepForTimeInterval:2];//模拟浏览页面,让第二次浏览和上一次时间相差2s
[dict setObject:[PhobosUtil currentTime] forKey:@"in"];
[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 上条数据应该发送");
[Phobos track:@"page_view" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"PhobosCacheKey 上条数据不应该发送");
[Phobos track:@"test" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 3, @"PhobosCacheKey 上条数据应该发送");
}
#pragma mark - 其它方法test
......
......@@ -448,8 +448,42 @@ static NSString *sdkVersion = @"110";
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
if (dataArray) {
#ifdef APPSTORE
[dataArray addObject:data];
}else{
#else
if (data[@"type"] && [data[@"type"] isEqualToString:@"page_view"]) {
NSDictionary *pageParams = data[@"params"];
NSDate *pageInTime = [NSDate dateWithTimeIntervalSince1970:[pageParams[@"in"] longLongValue]];
NSDate *pageOutTime = [NSDate dateWithTimeIntervalSince1970:[pageParams[@"out"] longLongValue]];
if (pageInTime && pageOutTime) {
__block BOOL checkTimeError;
[dataArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj[@"type"] && [obj[@"type"] isEqualToString:@"page_view"]) {
NSDictionary *params = obj[@"params"];
NSDate *objInTime = [NSDate dateWithTimeIntervalSince1970:[params[@"in"] longLongValue]];
NSDate *objOutTime = [NSDate dateWithTimeIntervalSince1970:[params[@"out"] longLongValue]];
if ([pageInTime timeIntervalSinceDate:objInTime] < 1 || [pageOutTime timeIntervalSinceDate:objOutTime] < 1) {
checkTimeError = YES;
stop = YES;
phobosLog(@"%s____数据校验失败", __func__);
#ifndef APPSTORE
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"PV埋点可能数据异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
#endif
}
}
}];
if (!checkTimeError) {
phobosLog(@"%s____数据校验成功", __func__);
[dataArray addObject:data];
}
}
} else {
[dataArray addObject:data];
}
#endif
} else {
dataArray = [NSMutableArray arrayWithObject:data];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosCacheKey object:dataArray];
......
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