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

photo数据存储重构

parent 0caeec4a
......@@ -97,23 +97,24 @@ NSString *const MockCityId = @"beijing";
[Phobos track:@"计数发送" attributes:@{@"index":@(i)} sendNow:NO];
}
NSLog(@"&&&&&&&&&&&&&&&&&");
}
break;
case 3://@"瞬间发送",
{
[Phobos track:@"瞬间发送" attributes:@{@"index":@(0)} sendNow:YES];
}
break;
case 4://瞬间发送和计数发送
{
for (int i = 0; i< 100; i++) {
[Phobos track:@"瞬间发送和计数发送" attributes:@{@"index":@(i)} sendNow:i % 9];
}
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i< 100; i++) {
[Phobos track:@"瞬间发送和计数发送" attributes:@{@"index":@(i)} sendNow:i % 9];
}
});
}
break;
case 5://清空数据
case 5://表中有多少条数据
{
[Phobos fetchToBeSendPhobosDataCount];
}
......@@ -134,13 +135,13 @@ NSString *const MockCityId = @"beijing";
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:@"home" forKey:@"page_name"];
[dict setObject:@"referrer" forKey:@"referrer"];
[dict setObject:@"referrer" 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:@"referrer_id" forKey:@"referrer_id"];
[dict setObject:@"extra_param" forKey:@"extra_param"];
[dict setObject:@"referrer_tab_name" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"];
NSString *name = [NSString stringWithFormat:@"page_view-%d", index];
[Phobos track:name attributes:dict];
......
......@@ -173,9 +173,6 @@ NS_ASSUME_NONNULL_BEGIN
/** 获取所有非立即发送埋点数量 */
+ (NSUInteger)fetchToBeSendPhobosDataCount;
/** 获取待发送埋点数据 */
+ (NSArray *)fetchToBeSendPhobosData;
/** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData;
......
......@@ -105,15 +105,15 @@ static NewPhobos *_sharedClient;
- (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/
NSDictionary *dict = @{@"build_cpu_abi": [PhobosUtil currentDeviceCPUType],
@"cpu_count": [PhobosUtil currentDeviceCPUCount],
@"mac_address": [PhobosUtil getMacAddress],
@"phone_operator": [PhobosUtil getTelephonyInfo],
@"total_memory": [PhobosUtil getTotalMemorySize],
@"run_time": [PhobosUtil deviceRunTime],
@"uuid": [PhobosUtil deviceId],
@"build_version_release": [[UIDevice currentDevice] systemVersion],
};
NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
};
[NewPhobos track:@"device_opened" attributes:dict sendNow:YES];
}
......@@ -435,11 +435,6 @@ static NewPhobos *_sharedClient;
return [[PhobosDataManager sharedPhobosDataManager] messageCount] ;
}
///** 获取待发送埋点数据, 用同步来保障异步获取数据 */
//+ (NSArray *)fetchToBeSendPhobosData {
// return [PhobosDataManager fetchToBeSendDataEntities];
//}
//
/** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData {
[[PhobosDataManager sharedPhobosDataManager] removeAll];
......
......@@ -98,14 +98,15 @@ static OldPhobos *sharedClient = nil;
- (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/
NSDictionary *dict = @{@"build_cpu_abi": [PhobosUtil currentDeviceCPUType],
@"cpu_count": [PhobosUtil currentDeviceCPUCount],
@"mac_address": [PhobosUtil getMacAddress],
@"phone_operator": [PhobosUtil getTelephonyInfo],
@"total_memory": [PhobosUtil getTotalMemorySize],
@"run_time": [PhobosUtil deviceRunTime],
@"uuid": [PhobosUtil deviceId],
@"build_version_release": [[UIDevice currentDevice] systemVersion],
NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
};
[OldPhobos track:@"device_opened" attributes:dict sendNow:YES];
}
......
......@@ -244,10 +244,10 @@ static BOOL isGray = NO;
return isGray ? [NewPhobos fetchToBeSendPhobosDataCount] : 0;
}
/** 获取待发送埋点数据 */
+ (NSArray *)fetchToBeSendPhobosData {
return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[];
}
///** 获取待发送埋点数据 */
//+ (NSArray *)fetchToBeSendPhobosData {
// return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[];
//}
/** 清除待发送埋点数据缓存 */
+ (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 @@
//
#import <Foundation/Foundation.h>
#import "GMPhobosTool.h"
#import <mach/mach_time.h>
typedef NS_ENUM(int, PhobosDataSendStatus) {
PhobosDataSendStatus_wait = 1, // 待发送数据
......@@ -19,21 +20,25 @@ typedef NS_ENUM(int, PhobosDataSendStatus) {
@interface GMPhobosSqulitModel : NSObject
/// 唯一标识(用于筛选)
@property (nonatomic, strong) NSString *messageId;
@property (nonatomic, strong) NSString *messageItemId;
/// 埋点类型
@property (nonatomic, strong) NSString *phobosType;
/// 内容
@property (nonatomic, strong) NSString *data;
/// 路径
@property (nonatomic, strong) NSString *api;
///插入表格名称
@property (nonatomic, strong) NSString *tableName;
/// 数据状态
@property (nonatomic, assign) PhobosDataSendStatus status;
///是否是立刻发的埋点
@property (nonatomic, assign) BOOL immediately;
+ (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
immediately:(BOOL)status;
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status;
@end
......@@ -6,17 +6,20 @@
//
#import "GMPhobosSqulitModel.h"
#import <mach/mach_time.h>
@implementation GMPhobosSqulitModel
+ (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
immediately:(BOOL)status {
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status{
GMPhobosSqulitModel *model = [GMPhobosSqulitModel new];
model.data = [GMPhobosSqulitModel dictionaryToJsonString:data];
model.tableName = name;
model.data = [GMPhobosTool dictionaryToJsonString:data];
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.phobosType = phobosType;
model.api = urlapi;
......@@ -24,13 +27,5 @@
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
//
// 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 @@
//
#import <Foundation/Foundation.h>
#import <MJExtension/MJExtension.h>
// 数据库中常见的几种类型
#define SQL_TEXT @"TEXT" //文本
......@@ -16,9 +17,6 @@
@interface GMSQliteManager : NSObject
#pragma mark - 创建数据库
/// 创建数据库的单利方法
+ (instancetype)sharedSQliteManager;
#pragma mark - 创建表
/// 创建表
......
......@@ -23,34 +23,26 @@
@implementation GMSQliteManager
static GMSQliteManager *squliteManager = nil;
+ (instancetype)sharedSQliteManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[GMSQliteManager shareDatabase];
});
return squliteManager;
}
+ (instancetype)shareDatabase {
if (!squliteManager) {
- (instancetype)init
{
self = [super init];
if (self) {
NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"GMFMDB.sqlite"];
NSLog(@"path------%@", path);
FMDatabase *fmdb = [FMDatabase databaseWithPath:path];
squliteManager = [GMSQliteManager new];
squliteManager.db = fmdb;
squliteManager.dbPath = path;
self.db = fmdb;
self.dbPath = path;
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
squliteManager.dbQueue = queue;
[squliteManager.db open];
self.dbQueue = queue;
[self.db open];
}
if (!squliteManager.db.isOpen) {
[squliteManager.db open];
if (!self.db.isOpen) {
[self.db open];
}
return squliteManager;
return self;
}
/// 创建表
- (BOOL)gm_createTable:(NSString *)tableName model:(Class)modelClass {
NSDictionary *dict = [self modelToDictionary:modelClass excludePropertyName:nil];
......@@ -79,7 +71,8 @@ static GMSQliteManager *squliteManager = nil;
/// 模型插入
- (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];
return status;
}
......@@ -176,7 +169,8 @@ static GMSQliteManager *squliteManager = nil;
if ([parameters isKindOfClass:[NSDictionary class]]) {
dict = parameters;
} else if([parameters isKindOfClass:[NSObject class]]) {
dict = [self getModelPropertyKeyValue:parameters];
NSObject *object =(NSObject *)parameters;
dict = object.mj_keyValues;
} else {
return NO;
}
......@@ -208,7 +202,9 @@ static GMSQliteManager *squliteManager = nil;
objectClass:(Class)cls
whereFormat:(NSString *)format {
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 {
......@@ -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是属性的类型
- (NSDictionary *)modelToDictionary:(Class)cls excludePropertyName:(NSArray *)nameArr
{
if (cls == [NSObject class]) {
return [NSMutableDictionary dictionary];
}
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(cls, &outCount);
for (int i = 0; i < outCount; i++) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
if ([nameArr containsObject:name]) continue;
NSString *type = [NSString stringWithCString:property_getAttributes(properties[i]) encoding:NSUTF8StringEncoding];
id value = [self propertTypeConvert:type];
if (value) {
[mDic setObject:value forKey:name];
}
}
free(properties);
NSDictionary *subDict = [self modelToDictionary:[cls superclass] excludePropertyName:nameArr];
[mDic addEntriesFromDictionary:subDict];
return mDic;
}
......
......@@ -55,21 +55,8 @@
}
+ (void)sendDataWithEntities:(NSArray<GMPhobosSqulitModel *> *)sendDataEntities completion:(void (^)(NSArray<GMPhobosSqulitModel *> * _Nonnull, NSInteger))completion {
if (sendDataEntities.count == 0) {
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;
}
if (sendDataEntities.count == 0) return;
NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
// 将相同api的数据合并
[sendDataEntities enumerateObjectsUsingBlock:^(GMPhobosSqulitModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
......
......@@ -242,11 +242,11 @@
}
- (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 = objc_getAssociatedObject(self, @selector(messageId));
NSString *messageId = objc_getAssociatedObject(self, @selector(messageItemId));
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