Commit 4a58871b authored by yueming lu's avatar yueming lu

photo数据存储重构

parent 0caeec4a
...@@ -97,23 +97,24 @@ NSString *const MockCityId = @"beijing"; ...@@ -97,23 +97,24 @@ NSString *const MockCityId = @"beijing";
[Phobos track:@"计数发送" attributes:@{@"index":@(i)} sendNow:NO]; [Phobos track:@"计数发送" attributes:@{@"index":@(i)} sendNow:NO];
} }
NSLog(@"&&&&&&&&&&&&&&&&&"); NSLog(@"&&&&&&&&&&&&&&&&&");
} }
break; break;
case 3://@"瞬间发送", case 3://@"瞬间发送",
{ {
[Phobos track:@"瞬间发送" attributes:@{@"index":@(0)} sendNow:YES]; [Phobos track:@"瞬间发送" attributes:@{@"index":@(0)} sendNow:YES];
} }
break; break;
case 4://瞬间发送和计数发送 case 4://瞬间发送和计数发送
{ {
for (int i = 0; i< 100; i++) { dispatch_async(dispatch_get_global_queue(0, 0), ^{
[Phobos track:@"瞬间发送和计数发送" attributes:@{@"index":@(i)} sendNow:i % 9]; for (int i = 0; i< 100; i++) {
} [Phobos track:@"瞬间发送和计数发送" attributes:@{@"index":@(i)} sendNow:i % 9];
}
});
} }
break; break;
case 5://清空数据 case 5://表中有多少条数据
{ {
[Phobos fetchToBeSendPhobosDataCount]; [Phobos fetchToBeSendPhobosDataCount];
} }
...@@ -134,13 +135,13 @@ NSString *const MockCityId = @"beijing"; ...@@ -134,13 +135,13 @@ NSString *const MockCityId = @"beijing";
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[PhobosUtil currentTime] forKey:@"out"]; [dict setObject:[PhobosUtil currentTime] forKey:@"out"];
[dict setObject:inDate forKey:@"in"]; [dict setObject:inDate forKey:@"in"];
[dict setObject:@"test" forKey:@"page_name"]; [dict setObject:@"home" forKey:@"page_name"];
[dict setObject:@"" forKey:@"business_id"]; [dict setObject:@"referrer" forKey:@"referrer"];
[dict setObject:@"" forKey:@"referrer"]; [dict setObject:@"referrer" forKey:@"referrer"];
[dict setObject:@(0) forKey:@"fake"]; [dict setObject:@(0) forKey:@"fake"];
[dict setObject:@"" forKey:@"referrer_id"]; [dict setObject:@"referrer_id" forKey:@"referrer_id"];
[dict setObject:@"" forKey:@"extra_param"]; [dict setObject:@"extra_param" forKey:@"extra_param"];
[dict setObject:@"" forKey:@"referrer_tab_name"]; [dict setObject:@"referrer_tab_name" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"]; [dict setObject:@(0) forKey:@"is_push"];
NSString *name = [NSString stringWithFormat:@"page_view-%d", index]; NSString *name = [NSString stringWithFormat:@"page_view-%d", index];
[Phobos track:name attributes:dict]; [Phobos track:name attributes:dict];
......
...@@ -173,9 +173,6 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -173,9 +173,6 @@ NS_ASSUME_NONNULL_BEGIN
/** 获取所有非立即发送埋点数量 */ /** 获取所有非立即发送埋点数量 */
+ (NSUInteger)fetchToBeSendPhobosDataCount; + (NSUInteger)fetchToBeSendPhobosDataCount;
/** 获取待发送埋点数据 */
+ (NSArray *)fetchToBeSendPhobosData;
/** 清除待发送埋点数据缓存 */ /** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData; + (void)removeAllPhobosData;
......
...@@ -105,15 +105,15 @@ static NewPhobos *_sharedClient; ...@@ -105,15 +105,15 @@ static NewPhobos *_sharedClient;
- (void)handleEventDeviceOpened{ - (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/ /** 每次打开APP埋点 **/
NSDictionary *dict = @{@"build_cpu_abi": [PhobosUtil currentDeviceCPUType], NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"cpu_count": [PhobosUtil currentDeviceCPUCount], @"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"mac_address": [PhobosUtil getMacAddress], @"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"phone_operator": [PhobosUtil getTelephonyInfo], @"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": [PhobosUtil getTotalMemorySize], @"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": [PhobosUtil deviceRunTime], @"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": [PhobosUtil deviceId], @"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"build_version_release": [[UIDevice currentDevice] systemVersion], @"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
}; };
[NewPhobos track:@"device_opened" attributes:dict sendNow:YES]; [NewPhobos track:@"device_opened" attributes:dict sendNow:YES];
} }
...@@ -435,11 +435,6 @@ static NewPhobos *_sharedClient; ...@@ -435,11 +435,6 @@ static NewPhobos *_sharedClient;
return [[PhobosDataManager sharedPhobosDataManager] messageCount] ; return [[PhobosDataManager sharedPhobosDataManager] messageCount] ;
} }
///** 获取待发送埋点数据, 用同步来保障异步获取数据 */
//+ (NSArray *)fetchToBeSendPhobosData {
// return [PhobosDataManager fetchToBeSendDataEntities];
//}
//
/** 清除待发送埋点数据缓存 */ /** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData { + (void)removeAllPhobosData {
[[PhobosDataManager sharedPhobosDataManager] removeAll]; [[PhobosDataManager sharedPhobosDataManager] removeAll];
......
...@@ -98,14 +98,15 @@ static OldPhobos *sharedClient = nil; ...@@ -98,14 +98,15 @@ static OldPhobos *sharedClient = nil;
- (void)handleEventDeviceOpened{ - (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/ /** 每次打开APP埋点 **/
NSDictionary *dict = @{@"build_cpu_abi": [PhobosUtil currentDeviceCPUType],
@"cpu_count": [PhobosUtil currentDeviceCPUCount], NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"mac_address": [PhobosUtil getMacAddress], @"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"phone_operator": [PhobosUtil getTelephonyInfo], @"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"total_memory": [PhobosUtil getTotalMemorySize], @"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"run_time": [PhobosUtil deviceRunTime], @"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"uuid": [PhobosUtil deviceId], @"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"build_version_release": [[UIDevice currentDevice] systemVersion], @"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
}; };
[OldPhobos track:@"device_opened" attributes:dict sendNow:YES]; [OldPhobos track:@"device_opened" attributes:dict sendNow:YES];
} }
......
...@@ -244,10 +244,10 @@ static BOOL isGray = NO; ...@@ -244,10 +244,10 @@ static BOOL isGray = NO;
return isGray ? [NewPhobos fetchToBeSendPhobosDataCount] : 0; return isGray ? [NewPhobos fetchToBeSendPhobosDataCount] : 0;
} }
/** 获取待发送埋点数据 */ ///** 获取待发送埋点数据 */
+ (NSArray *)fetchToBeSendPhobosData { //+ (NSArray *)fetchToBeSendPhobosData {
return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[]; // return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[];
} //}
/** 清除待发送埋点数据缓存 */ /** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData { + (void)removeAllPhobosData {
......
//
// GMPhobosPageViewModel.h
// GMPhobos
//
// Created by edz on 2020/6/22.
//
#import "GMPhobosSqulitModel.h"
@interface GMPhobosPageViewModel : GMPhobosSqulitModel
@property (nonatomic, strong) NSString * gm_out;
@property (nonatomic, strong) NSString * gm_in;
@property (nonatomic, strong) NSString * gm_page_name;
@property (nonatomic, strong) NSString * gm_business_id;
@property (nonatomic, strong) NSString * gm_referrer;
@property (nonatomic, strong) NSString * gm_referrer_link;
@property (nonatomic, strong) NSString * gm_referrer_id;
@property (nonatomic, strong) NSString * gm_extra_param;
@property (nonatomic, strong) NSString * gm_referrer_tab_name;
@property (nonatomic, strong) NSString * gm_is_push;
@property (nonatomic, strong) NSString * gm_message_id;
@property (nonatomic, strong) NSString * gm_in_time_millis;
@property (nonatomic, strong) NSString * gm_out_time_millis;
@property (nonatomic, strong) NSString * gm_is_first;
+ (GMPhobosPageViewModel *)pageViewModelOfDict:(NSDictionary *)data
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status;
@end
//
// GMPhobosPageViewModel.m
// GMPhobos
//
// Created by edz on 2020/6/22.
//
#import "GMPhobosPageViewModel.h"
@implementation GMPhobosPageViewModel
+ (GMPhobosPageViewModel *)pageViewModelOfDict:(NSDictionary *)dict
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status {
NSDictionary *data = dict[@"params"];
GMPhobosPageViewModel *model = [[GMPhobosPageViewModel alloc] init];
//共有参数
model.tableName = name;
model.data = [GMPhobosTool dictionaryToJsonString:data];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
model.messageItemId = [NSString stringWithFormat:@"%.f*%lld", time, mach_absolute_time()];
model.status = PhobosDataSendStatus_wait;
model.phobosType = phobosType;
model.api = urlapi;
model.immediately = status;
//私有参数
model.gm_out = data[@"out"];
model.gm_in = data[@"in"];
model.gm_page_name = data[@"page_name"];
model.gm_business_id = data[@"business_id"];
model.gm_referrer = data[@"referrer"];
model.gm_referrer_link = data[@"referrer_link"];
model.gm_referrer_id = data[@"referrer_id"];
model.gm_extra_param = data[@"extra_param"];
model.gm_referrer_tab_name = data[@"referrer_tab_name"];
model.gm_is_push = data[@"is_push"];
model.gm_is_push = data[@"is_push"];
model.gm_message_id = data[@"message_id"];
model.gm_in_time_millis= data[@"in_time_millis"];
model.gm_out_time_millis = data[@"out_time_millis"];
model.gm_is_first = data[@"is_first"];
return model;
}
@end
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "GMPhobosTool.h"
#import <mach/mach_time.h>
typedef NS_ENUM(int, PhobosDataSendStatus) { typedef NS_ENUM(int, PhobosDataSendStatus) {
PhobosDataSendStatus_wait = 1, // 待发送数据 PhobosDataSendStatus_wait = 1, // 待发送数据
...@@ -19,21 +20,25 @@ typedef NS_ENUM(int, PhobosDataSendStatus) { ...@@ -19,21 +20,25 @@ typedef NS_ENUM(int, PhobosDataSendStatus) {
@interface GMPhobosSqulitModel : NSObject @interface GMPhobosSqulitModel : NSObject
/// 唯一标识(用于筛选) /// 唯一标识(用于筛选)
@property (nonatomic, strong) NSString *messageId; @property (nonatomic, strong) NSString *messageItemId;
/// 埋点类型 /// 埋点类型
@property (nonatomic, strong) NSString *phobosType; @property (nonatomic, strong) NSString *phobosType;
/// 内容 /// 内容
@property (nonatomic, strong) NSString *data; @property (nonatomic, strong) NSString *data;
/// 路径 /// 路径
@property (nonatomic, strong) NSString *api; @property (nonatomic, strong) NSString *api;
///插入表格名称
@property (nonatomic, strong) NSString *tableName;
/// 数据状态 /// 数据状态
@property (nonatomic, assign) PhobosDataSendStatus status; @property (nonatomic, assign) PhobosDataSendStatus status;
///是否是立刻发的埋点 ///是否是立刻发的埋点
@property (nonatomic, assign) BOOL immediately; @property (nonatomic, assign) BOOL immediately;
+ (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data + (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data
type:(NSString *)phobosType type:(NSString *)phobosType
urlApi:(NSString *)urlapi urlApi:(NSString *)urlapi
immediately:(BOOL)status; tableName:(NSString *)name
immediately:(BOOL)status;
@end @end
...@@ -6,17 +6,20 @@ ...@@ -6,17 +6,20 @@
// //
#import "GMPhobosSqulitModel.h" #import "GMPhobosSqulitModel.h"
#import <mach/mach_time.h>
@implementation GMPhobosSqulitModel @implementation GMPhobosSqulitModel
+ (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data + (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data
type:(NSString *)phobosType type:(NSString *)phobosType
urlApi:(NSString *)urlapi urlApi:(NSString *)urlapi
immediately:(BOOL)status { tableName:(NSString *)name
immediately:(BOOL)status{
GMPhobosSqulitModel *model = [GMPhobosSqulitModel new]; GMPhobosSqulitModel *model = [GMPhobosSqulitModel new];
model.data = [GMPhobosSqulitModel dictionaryToJsonString:data]; model.tableName = name;
model.data = [GMPhobosTool dictionaryToJsonString:data];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000; NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
model.messageId = [NSString stringWithFormat:@"%.f*%lld", time, mach_absolute_time()]; model.messageItemId = [NSString stringWithFormat:@"%.f*%lld", time, mach_absolute_time()];
model.status = PhobosDataSendStatus_wait; model.status = PhobosDataSendStatus_wait;
model.phobosType = phobosType; model.phobosType = phobosType;
model.api = urlapi; model.api = urlapi;
...@@ -24,13 +27,5 @@ ...@@ -24,13 +27,5 @@
return model; return model;
} }
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
return nil;
}
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end @end
//
// GMPhobosTool.h
// GMPhobos
//
// Created by edz on 2020/6/23.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface GMPhobosTool : NSObject
/// 拆分数组
/// @param array 原数组
/// @param subSize 每个数组的长度
+ (NSArray *)splitArray:(NSArray *)array withSubSize:(int)subSize;
/// dictionary 转json
/// @param dict dict
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict;
@end
NS_ASSUME_NONNULL_END
//
// GMPhobosTool.m
// GMPhobos
//
// Created by edz on 2020/6/23.
//
#import "GMPhobosTool.h"
@implementation GMPhobosTool
+ (NSArray *)splitArray:(NSArray *)array withSubSize:(int)subSize {
if (subSize < 1) return [NSArray new];
// 数组将被拆分成指定长度数组的个数
unsigned long count = array.count % subSize == 0 ? (array.count / subSize) : (array.count / subSize + 1);
// 用来保存指定长度数组的可变数组对象
NSMutableArray *arr = [[NSMutableArray alloc] init];
//利用总个数进行循环,将指定长度的元素加入数组
for (int i = 0; i < count; i ++) {
//数组下标
int index = i * subSize;
//保存拆分的固定长度的数组元素的可变数组
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
//移除子数组的所有元素
[arr1 removeAllObjects];
int j = index;
//将数组下标乘以1、2、3,得到拆分时数组的最大下标值,但最大不能超过数组的总大小
while (j < subSize*(i + 1) && j < array.count) {
[arr1 addObject:[array objectAtIndex:j]];
j += 1;
}
//将子数组添加到保存子数组的数组中
[arr addObject:[arr1 copy]];
}
return [arr copy];
}
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
return nil;
}
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <MJExtension/MJExtension.h>
// 数据库中常见的几种类型 // 数据库中常见的几种类型
#define SQL_TEXT @"TEXT" //文本 #define SQL_TEXT @"TEXT" //文本
...@@ -16,9 +17,6 @@ ...@@ -16,9 +17,6 @@
@interface GMSQliteManager : NSObject @interface GMSQliteManager : NSObject
#pragma mark - 创建数据库
/// 创建数据库的单利方法
+ (instancetype)sharedSQliteManager;
#pragma mark - 创建表 #pragma mark - 创建表
/// 创建表 /// 创建表
......
...@@ -23,34 +23,26 @@ ...@@ -23,34 +23,26 @@
@implementation GMSQliteManager @implementation GMSQliteManager
static GMSQliteManager *squliteManager = nil; - (instancetype)init
{
+ (instancetype)sharedSQliteManager { self = [super init];
static dispatch_once_t onceToken; if (self) {
dispatch_once(&onceToken, ^{
[GMSQliteManager shareDatabase];
});
return squliteManager;
}
+ (instancetype)shareDatabase {
if (!squliteManager) {
NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"GMFMDB.sqlite"]; NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"GMFMDB.sqlite"];
NSLog(@"path------%@", path); NSLog(@"path------%@", path);
FMDatabase *fmdb = [FMDatabase databaseWithPath:path]; FMDatabase *fmdb = [FMDatabase databaseWithPath:path];
squliteManager = [GMSQliteManager new]; self.db = fmdb;
squliteManager.db = fmdb; self.dbPath = path;
squliteManager.dbPath = path;
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path]; FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
squliteManager.dbQueue = queue; self.dbQueue = queue;
[squliteManager.db open]; [self.db open];
} }
if (!squliteManager.db.isOpen) { if (!self.db.isOpen) {
[squliteManager.db open]; [self.db open];
} }
return squliteManager; return self;
} }
/// 创建表 /// 创建表
- (BOOL)gm_createTable:(NSString *)tableName model:(Class)modelClass { - (BOOL)gm_createTable:(NSString *)tableName model:(Class)modelClass {
NSDictionary *dict = [self modelToDictionary:modelClass excludePropertyName:nil]; NSDictionary *dict = [self modelToDictionary:modelClass excludePropertyName:nil];
...@@ -79,7 +71,8 @@ static GMSQliteManager *squliteManager = nil; ...@@ -79,7 +71,8 @@ static GMSQliteManager *squliteManager = nil;
/// 模型插入 /// 模型插入
- (BOOL)gm_insertTable:(NSString *)tableName model:(NSObject *)model { - (BOOL)gm_insertTable:(NSString *)tableName model:(NSObject *)model {
//模型转字典 //模型转字典
NSDictionary *dict = [self getModelPropertyKeyValue:model];
NSDictionary *dict = model.mj_keyValues;;
bool status = [self gm_insertTable:tableName dict:dict]; bool status = [self gm_insertTable:tableName dict:dict];
return status; return status;
} }
...@@ -176,7 +169,8 @@ static GMSQliteManager *squliteManager = nil; ...@@ -176,7 +169,8 @@ static GMSQliteManager *squliteManager = nil;
if ([parameters isKindOfClass:[NSDictionary class]]) { if ([parameters isKindOfClass:[NSDictionary class]]) {
dict = parameters; dict = parameters;
} else if([parameters isKindOfClass:[NSObject class]]) { } else if([parameters isKindOfClass:[NSObject class]]) {
dict = [self getModelPropertyKeyValue:parameters]; NSObject *object =(NSObject *)parameters;
dict = object.mj_keyValues;
} else { } else {
return NO; return NO;
} }
...@@ -208,7 +202,9 @@ static GMSQliteManager *squliteManager = nil; ...@@ -208,7 +202,9 @@ static GMSQliteManager *squliteManager = nil;
objectClass:(Class)cls objectClass:(Class)cls
whereFormat:(NSString *)format { whereFormat:(NSString *)format {
NSDictionary *modlDict = [self modelToDictionary:cls excludePropertyName:filterArray]; NSDictionary *modlDict = [self modelToDictionary:cls excludePropertyName:filterArray];
return [self gm_lookupTable:tableName keyArray:modlDict.allKeys whereFormat:format]; NSArray *array = [self gm_lookupTable:tableName keyArray:modlDict.allKeys whereFormat:format];
NSMutableArray *modelArray = [cls mj_objectArrayWithKeyValuesArray:array];
return modelArray;
} }
- (NSArray *)gm_lookupTable:(NSString *)tableName keyArray:(NSArray *)keyArray whereFormat:(NSString *)format { - (NSArray *)gm_lookupTable:(NSString *)tableName keyArray:(NSArray *)keyArray whereFormat:(NSString *)format {
...@@ -312,45 +308,27 @@ static GMSQliteManager *squliteManager = nil; ...@@ -312,45 +308,27 @@ static GMSQliteManager *squliteManager = nil;
}]; }];
} }
#pragma mark - 模型转字典
- (NSDictionary *)getModelPropertyKeyValue:(id)model
{
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList([model class], &outCount);
for (int i = 0; i < outCount; i++) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
id value = [model valueForKey:name];
if (value) {
[mDic setObject:value forKey:name];
}
}
free(properties);
return mDic;
}
#pragma mark - 通过类对象获取对象的Property的字典 key 是属性名称, value是属性的类型 #pragma mark - 通过类对象获取对象的Property的字典 key 是属性名称, value是属性的类型
- (NSDictionary *)modelToDictionary:(Class)cls excludePropertyName:(NSArray *)nameArr - (NSDictionary *)modelToDictionary:(Class)cls excludePropertyName:(NSArray *)nameArr
{ {
if (cls == [NSObject class]) {
return [NSMutableDictionary dictionary];
}
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init]; NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
unsigned int outCount; unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(cls, &outCount); objc_property_t *properties = class_copyPropertyList(cls, &outCount);
for (int i = 0; i < outCount; i++) { for (int i = 0; i < outCount; i++) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding]; NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
if ([nameArr containsObject:name]) continue; if ([nameArr containsObject:name]) continue;
NSString *type = [NSString stringWithCString:property_getAttributes(properties[i]) encoding:NSUTF8StringEncoding]; NSString *type = [NSString stringWithCString:property_getAttributes(properties[i]) encoding:NSUTF8StringEncoding];
id value = [self propertTypeConvert:type]; id value = [self propertTypeConvert:type];
if (value) { if (value) {
[mDic setObject:value forKey:name]; [mDic setObject:value forKey:name];
} }
} }
free(properties); free(properties);
NSDictionary *subDict = [self modelToDictionary:[cls superclass] excludePropertyName:nameArr];
[mDic addEntriesFromDictionary:subDict];
return mDic; return mDic;
} }
......
...@@ -11,10 +11,15 @@ ...@@ -11,10 +11,15 @@
#import "PhobosUtil.h" #import "PhobosUtil.h"
#import "GMSQliteManager.h" #import "GMSQliteManager.h"
#import "GMPhobosSqulitModel.h" #import "GMPhobosSqulitModel.h"
#import "GMPhobosPageViewModel.h"
#import <MJExtension/MJExtension.h> #import <MJExtension/MJExtension.h>
#import "GMPhobosThread.h" #import "GMPhobosThread.h"
#import "GMPhobosTool.h"
#define KtableName @"phobosTable" #define KtableName @"phobosTable"
#define KPageViewTableName @"pageViewTable"
#define KName @"tableName"
#define KClass @"class"
@interface PhobosDataManager () @interface PhobosDataManager ()
//数据库操作对象(单利) //数据库操作对象(单利)
...@@ -23,6 +28,7 @@ ...@@ -23,6 +28,7 @@
@property (nonatomic, assign) NSInteger messageCont; @property (nonatomic, assign) NSInteger messageCont;
/// runloop线程,用来处理埋点的数据 /// runloop线程,用来处理埋点的数据
@property (nonatomic, strong) GMPhobosThread *phobosThread; @property (nonatomic, strong) GMPhobosThread *phobosThread;
@property (nonatomic, strong) NSArray *tableMessageArray;
@end @end
@implementation PhobosDataManager @implementation PhobosDataManager
...@@ -40,6 +46,16 @@ static PhobosDataManager *dataManager; ...@@ -40,6 +46,16 @@ static PhobosDataManager *dataManager;
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
_tableMessageArray = @[
@{
KName:KtableName,
KClass:[GMPhobosSqulitModel class]
},
@{
KName:KPageViewTableName,
KClass:[GMPhobosPageViewModel class]
}
];
[self setupSDK]; [self setupSDK];
} }
return self; return self;
...@@ -51,20 +67,24 @@ static PhobosDataManager *dataManager; ...@@ -51,20 +67,24 @@ static PhobosDataManager *dataManager;
- (void)remove { - (void)remove {
bool status = [_squlitManager gm_deleteAllDataFromTable:KtableName]; bool status = [_squlitManager gm_deleteAllDataFromTable:KtableName];
NSLog(@"清空表格数状态:%d",status); bool status1 = [_squlitManager gm_deleteAllDataFromTable:KPageViewTableName];
NSLog(@"清空表格数状态-KtableName:%d KPageViewTableName:%d",status,status1);
} }
- (int)messageCount { - (int)messageCount {
int count = [_squlitManager gm_tableItemCount:KtableName whereFormat:nil]; int count = [_squlitManager gm_tableItemCount:KtableName whereFormat:nil];
NSLog(@"数据总数:%d",count); int count1 = [_squlitManager gm_tableItemCount:KPageViewTableName whereFormat:nil];
NSLog(@"数据总数:%d - KtableName:%d KPageViewTableName:%d", count + count1, count, count1);
return count; return count;
} }
#pragma mark - 初始化 #pragma mark - 初始化
- (void)setupSDK { - (void)setupSDK {
_squlitManager = [GMSQliteManager sharedSQliteManager]; //创建数据库和表格
//创建表格 _squlitManager = [[GMSQliteManager alloc] init];
bool status = [_squlitManager gm_createTable:KtableName model:[GMPhobosSqulitModel class]]; for (NSDictionary *dict in _tableMessageArray) {
[_squlitManager gm_createTable:dict[KName] model:dict[KClass]];
}
//初始化线程和runloop //初始化线程和runloop
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
_phobosThread = [[GMPhobosThread alloc] initWithBlock:^{ _phobosThread = [[GMPhobosThread alloc] initWithBlock:^{
...@@ -81,15 +101,15 @@ static PhobosDataManager *dataManager; ...@@ -81,15 +101,15 @@ static PhobosDataManager *dataManager;
#pragma mark - 方法只用在APP启动的时候 #pragma mark - 方法只用在APP启动的时候
- (void)resetData { - (void)resetData {
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */ for (NSDictionary *dict in _tableMessageArray) {
NSString *whereStr = [NSString stringWithFormat:@"where status = %d", PhobosDataSendStatus_send]; /** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
[_squlitManager gm_updateTable:KtableName dicOrModel:@{@"status":@(PhobosDataSendStatus_error)} whereFormat:whereStr]; NSString *whereStr = [NSString stringWithFormat:@"where status = %d", PhobosDataSendStatus_send];
/** 将发送成功的数据删除 */ [_squlitManager gm_updateTable:dict[KName] dicOrModel:@{@"status":@(PhobosDataSendStatus_error)} whereFormat:whereStr];
NSString *whereDelet = [NSString stringWithFormat:@"where status = %d", PhobosDataSendStatus_finish]; /** 将发送成功的数据删除 */
[_squlitManager gm_deleteTable:KtableName whereFormat:whereDelet]; NSString *whereDelet = [NSString stringWithFormat:@"where status = %d", PhobosDataSendStatus_finish];
/** 设置未发送数据数量 */ [_squlitManager gm_deleteTable:dict[KName] whereFormat:whereDelet];
NSString *whereCount = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error]; }
_messageCont = [_squlitManager gm_tableItemCount:KtableName whereFormat:whereCount];
} }
#pragma mark - 插入数据 #pragma mark - 插入数据
...@@ -98,12 +118,18 @@ static PhobosDataManager *dataManager; ...@@ -98,12 +118,18 @@ static PhobosDataManager *dataManager;
phobosType:(NSString *)phoboType phobosType:(NSString *)phoboType
immediately:(BOOL)status { immediately:(BOOL)status {
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) return; if (!sendAPI || [sendAPI isEqualToString:@""] || !data) return;
GMPhobosSqulitModel *squlitModel = [GMPhobosSqulitModel modelOfDict:data type:phoboType urlApi:sendAPI immediately:status]; if ([phoboType containsString:@"page_view"]) {
[self performSelector:@selector(insertModel:) onThread:_phobosThread withObject:squlitModel waitUntilDone:NO]; GMPhobosPageViewModel *squlitModel = [GMPhobosPageViewModel pageViewModelOfDict:data type:phoboType urlApi:sendAPI tableName:KPageViewTableName immediately:status];
[self performSelector:@selector(insertModel:) onThread:_phobosThread withObject:squlitModel waitUntilDone:NO];
} else {
GMPhobosSqulitModel *squlitModel = [GMPhobosSqulitModel modelOfDict:data type:phoboType urlApi:sendAPI tableName:KtableName immediately:status];
[self performSelector:@selector(insertModel:) onThread:_phobosThread withObject:squlitModel waitUntilDone:NO];
}
} }
- (void)insertModel:(GMPhobosSqulitModel *)squlitModel { - (void)insertModel:(GMPhobosSqulitModel *)squlitModel {
BOOL insertType = [_squlitManager gm_insertTable:KtableName model:squlitModel];
BOOL insertType = [_squlitManager gm_insertTable:squlitModel.tableName model:squlitModel];
if (!insertType) {//如果插入失败,就直接发送给服务器 if (!insertType) {//如果插入失败,就直接发送给服务器
[self sendModelArray:@[squlitModel]]; [self sendModelArray:@[squlitModel]];
} else if (squlitModel.immediately) {//需要立刻发送的埋点 } else if (squlitModel.immediately) {//需要立刻发送的埋点
...@@ -112,26 +138,51 @@ static PhobosDataManager *dataManager; ...@@ -112,26 +138,51 @@ static PhobosDataManager *dataManager;
self.messageCont++; self.messageCont++;
[self disposeSendDataWithImmediately:NO]; [self disposeSendDataWithImmediately:NO];
} }
NSLog(@"插入数状态%d count:%d", insertType,_messageCont); NSLog(@"插入%@状态%d count:%d",squlitModel.phobosType, insertType,_messageCont);
} }
#pragma mark - 获取待发送数据并修改状态为“发送” #pragma mark - 获取待发送数据并修改状态为“发送”
- (NSArray *)fetchToBeSendDataEntitiesAndUpdateWithSendStatus { - (NSMutableArray *)fetchToBeSendDataEntitiesAndUpdateWithSendStatus {
NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error]; NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error];
/**获取待发送的数据*/ NSMutableArray *array = [NSMutableArray array];
NSArray *array = [_squlitManager gm_lookupTable:KtableName filterArray:nil objectClass:[GMPhobosSqulitModel class] whereFormat:where]; for (NSDictionary *dict in _tableMessageArray) {
/**将待发送的数据修改为“发送中”*/ /**获取待发送的数据*/
[_squlitManager gm_updateTable:KtableName dicOrModel:@{@"status":@(PhobosDataSendStatus_send)} whereFormat:where]; NSArray *sub = [_squlitManager gm_lookupTable:dict[KName] filterArray:nil objectClass:dict[KClass] whereFormat:where];
[array addObjectsFromArray:sub];
/**将待发送的数据修改为“发送中”*/
[_squlitManager gm_updateTable:dict[KName] dicOrModel:@{@"status":@(PhobosDataSendStatus_send)} whereFormat:where];
}
return array; return array;
} }
//获取待发送的数据个数
- (int)getAllTabelNeedSendMessageCount {
int count = 0;
NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error];
for (NSDictionary *dict in _tableMessageArray) {
int tableCount = [_squlitManager gm_tableItemCount:dict[KName] whereFormat:where];
count += tableCount;
}
return count;
}
#pragma mark - 是否需要发送数据 #pragma mark - 是否需要发送数据
- (void)disposeSendDataWithImmediately:(BOOL)status { - (void)disposeSendDataWithImmediately:(BOOL)status {
NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error]; if (!status) {
_messageCont = [_squlitManager gm_tableItemCount:KtableName whereFormat:where]; _messageCont = [self getAllTabelNeedSendMessageCount];
if (status || _messageCont >= PhobosShardCount) { if (_messageCont < PhobosShardCount) return;
NSArray *array = [self fetchToBeSendDataEntitiesAndUpdateWithSendStatus]; }
NSMutableArray *entities = [GMPhobosSqulitModel mj_objectArrayWithKeyValuesArray:array]; /**
能走到下面说明“满足上报埋点条件 status == yes || _messageCont >= PhobosShardCount”
*/
NSMutableArray *entities = [self fetchToBeSendDataEntitiesAndUpdateWithSendStatus];
if (entities.count >= PhobosMaxSendCount) { //大于100条 要对数据进行拆分
//拆分数组
NSArray *array = [GMPhobosTool splitArray:entities withSubSize:PhobosShardCount];
for (NSArray *sub in array) {
[self sendModelArray:sub];
}
} else {
[self sendModelArray:entities]; [self sendModelArray:entities];
} }
} }
...@@ -147,8 +198,8 @@ static PhobosDataManager *dataManager; ...@@ -147,8 +198,8 @@ static PhobosDataManager *dataManager;
#pragma mark - 上报数据成功 #pragma mark - 上报数据成功
- (void)sendSuccess:(NSArray<GMPhobosSqulitModel*> *)entities { - (void)sendSuccess:(NSArray<GMPhobosSqulitModel*> *)entities {
for (GMPhobosSqulitModel *model in entities) { for (GMPhobosSqulitModel *model in entities) {
NSString *whereStr = [NSString stringWithFormat:@"where messageId = '%@'", model.messageId]; NSString *whereStr = [NSString stringWithFormat:@"where messageItemId = '%@'", model.messageItemId];
bool status = [_squlitManager gm_deleteTable:KtableName whereFormat:whereStr]; bool status = [_squlitManager gm_deleteTable:model.tableName whereFormat:whereStr];
NSLog(@"删除状态:%d", status); NSLog(@"删除状态:%d", status);
} }
} }
...@@ -156,8 +207,8 @@ static PhobosDataManager *dataManager; ...@@ -156,8 +207,8 @@ static PhobosDataManager *dataManager;
#pragma mark - 上报数据失败 #pragma mark - 上报数据失败
- (void)sendError:(NSArray<GMPhobosSqulitModel*> *)entities { - (void)sendError:(NSArray<GMPhobosSqulitModel*> *)entities {
for (GMPhobosSqulitModel *model in entities) { for (GMPhobosSqulitModel *model in entities) {
NSString *whereStr = [NSString stringWithFormat:@"where messageId = '%@'", model.messageId]; NSString *whereStr = [NSString stringWithFormat:@"where messageItemId = '%@'", model.messageItemId];
bool status = [_squlitManager gm_updateTable:KtableName bool status = [_squlitManager gm_updateTable:model.tableName
dicOrModel: @{@"status":@(PhobosDataSendStatus_error)} dicOrModel: @{@"status":@(PhobosDataSendStatus_error)}
whereFormat:whereStr]; whereFormat:whereStr];
NSLog(@"发送失败后修改状态:%d",status); NSLog(@"发送失败后修改状态:%d",status);
......
...@@ -55,21 +55,8 @@ ...@@ -55,21 +55,8 @@
} }
+ (void)sendDataWithEntities:(NSArray<GMPhobosSqulitModel *> *)sendDataEntities completion:(void (^)(NSArray<GMPhobosSqulitModel *> * _Nonnull, NSInteger))completion { + (void)sendDataWithEntities:(NSArray<GMPhobosSqulitModel *> *)sendDataEntities completion:(void (^)(NSArray<GMPhobosSqulitModel *> * _Nonnull, NSInteger))completion {
if (sendDataEntities.count == 0) { if (sendDataEntities.count == 0) return;
return;
} else if (sendDataEntities.count > PhobosMaxSendCount) {
/** 如果数据超过 PhobosMaxSendCount 数据规模,进行拆分,分批发送,默认100条 */
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:PhobosMaxSendCount];
[sendDataEntities enumerateObjectsUsingBlock:^(GMPhobosSqulitModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (temp.count >= PhobosMaxSendCount) {
// 会调用当前方法,但数据规模不会满足 > PhobosMaxSendCount,不会让数据走到下面的发送,拆分完成 会执行return
[self sendDataWithEntities:temp completion:completion];
[temp removeAllObjects];
}
[temp addObject:obj];
}];
return;
}
NSMutableDictionary *sendDataMap = [NSMutableDictionary new]; NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
// 将相同api的数据合并 // 将相同api的数据合并
[sendDataEntities enumerateObjectsUsingBlock:^(GMPhobosSqulitModel *obj, NSUInteger idx, BOOL * _Nonnull stop) { [sendDataEntities enumerateObjectsUsingBlock:^(GMPhobosSqulitModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
......
...@@ -242,11 +242,11 @@ ...@@ -242,11 +242,11 @@
} }
- (void)setMessageId:(NSString *)messageId { - (void)setMessageId:(NSString *)messageId {
objc_setAssociatedObject(self, @selector(messageId), messageId, OBJC_ASSOCIATION_COPY_NONATOMIC); objc_setAssociatedObject(self, @selector(messageItemId), messageId, OBJC_ASSOCIATION_COPY_NONATOMIC);
} }
- (NSString *)messageId { - (NSString *)messageId {
NSString *messageId = objc_getAssociatedObject(self, @selector(messageId)); NSString *messageId = objc_getAssociatedObject(self, @selector(messageItemId));
return messageId == nil ? @"" : messageId; return messageId == nil ? @"" : messageId;
} }
......
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