Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
GMPhobos
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gengmeiios
GMPhobos
Commits
c30bae52
Commit
c30bae52
authored
Apr 13, 2020
by
汪洋
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'jql/phobos' into 'master'
埋点库优化 See merge request
!51
parents
2fb8eefb
1367b35b
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
49 additions
and
41 deletions
+49
-41
GMViewController.m
Example/GMPhobos/GMViewController.m
+4
-14
Podfile.lock
Example/Podfile.lock
+3
-3
GMPhotoTest.m
Example/Tests/GMPhotoTest.m
+7
-4
NewPhobos.m
GMPhobos/Classes/NewPhobos.m
+10
-9
PhobosConfig.h
GMPhobos/Classes/PhobosConfig.h
+1
-1
PhobosDataManager.m
GMPhobos/Classes/PhobosDataManager.m
+20
-9
PhobosSendManager.m
GMPhobos/Classes/PhobosSendManager.m
+4
-1
No files found.
Example/GMPhobos/GMViewController.m
View file @
c30bae52
...
...
@@ -66,20 +66,10 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[
dict
setObject
:[
PhobosUtil
currentTime
]
forKey
:
@"in"
];
[
dict
setObject
:[
PhobosUtil
currentTime
]
forKey
:
@"out"
];
[
Phobos
track
:
@"page_view"
attributes
:
dict
sendNow
:
YES
];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[
Phobos
track
:
@"test"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test2"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test3"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test4"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test5"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test6"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test7"
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:
@"test8"
attributes
:
dict
sendNow
:
YES
];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
[
Phobos
track
:[
NSString
stringWithFormat
:
@"tt-%d"
,
i
]
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:[
NSString
stringWithFormat
:
@"pv-%d"
,
i
]
attributes
:
dict
sendNow
:
YES
];
}
}
-
(
void
)
touchesBegan
:
(
NSSet
<
UITouch
*>
*
)
touches
withEvent
:
(
UIEvent
*
)
event
{
...
...
Example/Podfile.lock
View file @
c30bae52
PODS:
- GMCache (1.0.1):
- TMCache (= 2.1.0)
- GMPhobos (2.0.
0
):
- GMPhobos (2.0.
3
):
- GMCache
- MagicalRecord
- MJExtension
...
...
@@ -28,11 +28,11 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
GMCache: b78d8e46db864405e91d226ce640cc80d966c611
GMPhobos:
3cad92cfa28da2b719ccef040a8fb4e33012b1ad
GMPhobos:
6bd14d7b98841b6a3e35c5aa8b1f091ad07bbbf7
MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad
COCOAPODS: 1.9.
0
COCOAPODS: 1.9.
1
Example/Tests/GMPhotoTest.m
View file @
c30bae52
...
...
@@ -117,12 +117,14 @@ NSString *const MockCityId = @"beijing";
*/
-
(
void
)
testTrackEventWithAttrAndSendNowDonotClearNormal
{
// Given
[
Phobos
removeAllPhobosData
];
NSDictionary
*
attr
=
@{
@"attr"
:
@"track_attr"
};
NSDictionary
*
sendNowAttr
=
@{
@"attr"
:
@"track_attr_send_now"
};
// When
[
Phobos
track
:
MockEventId
attributes
:
attr
sendNow
:
NO
];
// When
[
Phobos
track
:
MockEventId
attributes
:
sendNowAttr
sendNow
:
YES
];
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
0
,
@"array should be empty"
);
}
...
...
@@ -132,6 +134,7 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
-
(
void
)
testTrackEventWithDoubleAttrAndSendNow
{
[
Phobos
removeAllPhobosData
];
NSDictionary
*
attr
=
@{
@"attr"
:
@"track_attr"
};
[
Phobos
track
:
MockEventId
attributes
:
attr
sendNow
:
YES
];
...
...
@@ -160,9 +163,9 @@ NSString *const MockCityId = @"beijing";
[
Phobos
track
:
MockEventId
attributes
:
attr
];
}
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
0
,
@"array shouldn't be empty"
);
[
Phobos
track
:
MockEventId
attributes
:
attr
];
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
1
,
@"array shouldn't be empty"
);
[
Phobos
track
:
MockEventId
attributes
:
attr
];
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
2
,
@"array shouldn't be empty"
);
}
/**
...
...
@@ -221,10 +224,10 @@ NSString *const MockCityId = @"beijing";
for
(
int
i
=
0
;
i
<
50
;
i
++
)
{
[
Phobos
track
:
MockEventId
attributes
:
attr
];
}
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
0
,
@"array shouldn't be empty"
);
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
1
,
@"array shouldn't be empty"
);
[
Phobos
track
:
MockEventId
attributes
:
attr
];
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
1
,
@"array shouldn't be empty"
);
XCTAssertTrue
([
Phobos
fetchToBeSendPhobosDataCount
]
==
2
,
@"array shouldn't be empty"
);
}
-
(
void
)
verfiyDict
:
(
NSDictionary
*
)
dict
{
...
...
GMPhobos/Classes/NewPhobos.m
View file @
c30bae52
...
...
@@ -32,6 +32,8 @@ static NewPhobos *_sharedClient;
@implementation
NewPhobos
static
dispatch_semaphore_t
_phobos_semaphore
;
+
(
NewPhobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
{
NewPhobos
.
sharedClient
.
appName
=
appName
;
NewPhobos
.
sharedClient
.
channelId
=
channelId
;
...
...
@@ -40,6 +42,7 @@ static NewPhobos *_sharedClient;
-
(
instancetype
)
init
{
if
(
self
=
[
super
init
])
{
_phobos_semaphore
=
dispatch_semaphore_create
(
1
);
_appName
=
@""
;
_channelId
=
@""
;
_logEnabled
=
NO
;
...
...
@@ -420,16 +423,14 @@ static NewPhobos *_sharedClient;
* 处理发送数据
*/
+
(
void
)
disposeSendDataWithImmediately
:
(
BOOL
)
immediately
{
NSArray
<
PhobosSendDataEntity
*>
*
entities
=
[
PhobosDataManager
fetchToBeSendDataEntities
];
if
(
immediately
||
entities
.
count
>
PhobosShardCount
)
{
NSInteger
count
=
[
PhobosDataManager
fetchCountOfToBeSendEntities
];
if
(
immediately
||
count
>=
PhobosShardCount
)
{
dispatch_semaphore_wait
(
_phobos_semaphore
,
DISPATCH_TIME_FOREVER
);
NSArray
<
PhobosSendDataEntity
*>
*
entities
=
[
PhobosDataManager
fetchToBeSendDataEntities
];
[
PhobosDataManager
updateDataEntities
:
entities
sendStatus
:
PhobosDataSendStatusSending
];
dispatch_semaphore_signal
(
_phobos_semaphore
);
[
PhobosSendManager
sendDataWithEntities
:
entities
completion
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
_Nonnull
finishEntities
,
NSInteger
code
)
{
if
(
code
==
200
)
{
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:
PhobosDataSendStatusFinish
];
[
PhobosDataManager
deleteDataEntities
:
finishEntities
];
}
else
{
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:
PhobosDataSendStatusError
];
}
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:(
code
==
200
?
PhobosDataSendStatusFinish
:
PhobosDataSendStatusError
)];
}];
}
}
...
...
@@ -443,7 +444,7 @@ static NewPhobos *_sharedClient;
return
[
PhobosDataManager
fetchCountOfToBeSendEntities
];
}
/** 获取待发送埋点数据 */
/** 获取待发送埋点数据
, 用同步来保障异步获取数据
*/
+
(
NSArray
*
)
fetchToBeSendPhobosData
{
return
[
PhobosDataManager
fetchToBeSendDataEntities
];
}
...
...
GMPhobos/Classes/PhobosConfig.h
View file @
c30bae52
...
...
@@ -16,7 +16,7 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
PhobosSigningTypeDebug
};
static
NSString
*
sdkVersion
=
@"2.0.
2
"
;
static
NSString
*
sdkVersion
=
@"2.0.
3
"
;
#ifdef DEBUG
#define phobosLog(...) NSLog(@"[Phobos] %@",__VA_ARGS__)
...
...
GMPhobos/Classes/PhobosDataManager.m
View file @
c30bae52
...
...
@@ -14,13 +14,16 @@
@implementation
PhobosDataManager
static
NSManagedObjectContext
*
Phobos_defaultContext
;
static
NSManagedObjectContext
*
PhobosDefaultContext
;
static
dispatch_semaphore_t
phobos_semaphore_t
;
+
(
void
)
initialize
{
static
dispatch_once_t
onceToken
;
dispatch_once
(
&
onceToken
,
^
{
phobos_semaphore_t
=
dispatch_semaphore_create
(
1
);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
Phobos
_defaultContext
=
[[
NSManagedObjectContext
alloc
]
initWithConcurrencyType
:
NSMain
QueueConcurrencyType
];
Phobos
DefaultContext
=
[[
NSManagedObjectContext
alloc
]
initWithConcurrencyType
:
NSPrivate
QueueConcurrencyType
];
NSBundle
*
bundle
=
[
NSBundle
bundleForClass
:[
self
class
]];
// 获取埋点数据的实体
NSURL
*
modelURL
=
[
bundle
URLForResource
:
@"GMPhobosData"
withExtension
:
@"momd"
];
...
...
@@ -36,7 +39,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
[
coordinator
addPersistentStoreWithType
:
NSSQLiteStoreType
configuration
:
nil
URL
:
[
NSURL
fileURLWithPath
:
dataPath
]
options
:
nil
error
:
nil
];
// 设置storeCoordinator
Phobos
_d
efaultContext
.
persistentStoreCoordinator
=
coordinator
;
Phobos
D
efaultContext
.
persistentStoreCoordinator
=
coordinator
;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate
*
predicate
=
[
NSPredicate
predicateWithFormat
:
@"status = %d"
,
PhobosDataSendStatusSending
];
...
...
@@ -56,7 +59,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchDataEntitiesWithPredicate
:
(
NSPredicate
*
)
searchFilter
{
return
[
PhobosSendDataEntity
MR_findAllWithPredicate
:
searchFilter
inContext
:
Phobos
_d
efaultContext
];
return
[
PhobosSendDataEntity
MR_findAllWithPredicate
:
searchFilter
inContext
:
Phobos
D
efaultContext
];
}
+
(
NSUInteger
)
fetchCountOfToBeSendEntities
{
...
...
@@ -65,7 +68,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+
(
NSUInteger
)
fetchCountOfEntitiesWithPredicate
:
(
NSPredicate
*
)
searchFilter
{
return
[[
PhobosSendDataEntity
MR_numberOfEntitiesWithPredicate
:
searchFilter
inContext
:
Phobos
_d
efaultContext
]
integerValue
];
return
[[
PhobosSendDataEntity
MR_numberOfEntitiesWithPredicate
:
searchFilter
inContext
:
Phobos
D
efaultContext
]
integerValue
];
}
+
(
void
)
insertData
:
(
NSDictionary
*
)
data
sendAPI
:
(
NSString
*
)
sendAPI
{
...
...
@@ -76,11 +79,13 @@ static NSManagedObjectContext *Phobos_defaultContext;
if
(
!
sendAPI
||
[
sendAPI
isEqualToString
:
@""
]
||
!
data
)
{
return
;
}
PhobosSendDataEntity
*
entity
=
[
PhobosSendDataEntity
MR_createEntityInContext
:
Phobos_defaultContext
];
dispatch_semaphore_wait
(
phobos_semaphore_t
,
DISPATCH_TIME_FOREVER
);
PhobosSendDataEntity
*
entity
=
[
PhobosSendDataEntity
MR_createEntityInContext
:
PhobosDefaultContext
];
entity
.
data
=
[
data
mj_JSONData
];
entity
.
api
=
sendAPI
;
entity
.
status
=
PhobosDataSendStatusToBeSend
;
entity
.
id
=
mach_absolute_time
();
dispatch_semaphore_signal
(
phobos_semaphore_t
);
[
self
saveWithCompletion
:
completion
];
}
...
...
@@ -90,24 +95,30 @@ static NSManagedObjectContext *Phobos_defaultContext;
+
(
void
)
updateDataEntities
:
(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
sendStatus
:
(
PhobosDataSendStatus
)
sendStatus
completion
:
(
MRSaveCompletionHandler
)
completion
{
if
(
entities
.
count
>
0
)
{
dispatch_semaphore_wait
(
phobos_semaphore_t
,
DISPATCH_TIME_FOREVER
);
[
entities
enumerateObjectsUsingBlock
:
^
(
PhobosSendDataEntity
*
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
obj
.
status
=
sendStatus
;
}];
dispatch_semaphore_signal
(
phobos_semaphore_t
);
[
self
saveWithCompletion
:
completion
];
}
}
+
(
void
)
deleteDataEntities
:
(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
{
if
(
entities
.
count
>
0
)
{
dispatch_semaphore_wait
(
phobos_semaphore_t
,
DISPATCH_TIME_FOREVER
);
[
entities
enumerateObjectsUsingBlock
:
^
(
PhobosSendDataEntity
*
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
[
obj
MR_deleteEntityInContext
:
Phobos
_d
efaultContext
];
[
obj
MR_deleteEntityInContext
:
Phobos
D
efaultContext
];
}];
dispatch_semaphore_signal
(
phobos_semaphore_t
);
[
self
saveWithCompletion
:
nil
];
}
}
+
(
void
)
deleteAllEntities
{
[
PhobosSendDataEntity
MR_truncateAllInContext
:
Phobos_defaultContext
];
dispatch_semaphore_wait
(
phobos_semaphore_t
,
DISPATCH_TIME_FOREVER
);
[
PhobosSendDataEntity
MR_truncateAllInContext
:
PhobosDefaultContext
];
dispatch_semaphore_signal
(
phobos_semaphore_t
);
[
self
saveWithCompletion
:
nil
];
}
...
...
@@ -115,7 +126,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
* 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
*/
+
(
void
)
saveWithCompletion
:
(
MRSaveCompletionHandler
)
completion
{
[
Phobos
_d
efaultContext
MR_saveOnlySelfWithCompletion
:
completion
];
[
Phobos
D
efaultContext
MR_saveOnlySelfWithCompletion
:
completion
];
}
@end
GMPhobos/Classes/PhobosSendManager.m
View file @
c30bae52
...
...
@@ -84,7 +84,10 @@
[
sendDataMap
enumerateKeysAndObjectsUsingBlock
:
^
(
NSString
*
api
,
NSArray
<
PhobosSendDataEntity
*>
*
entities
,
BOOL
*
_Nonnull
stop
)
{
NSMutableArray
*
sendDatas
=
[
NSMutableArray
new
];
[
entities
enumerateObjectsUsingBlock
:
^
(
PhobosSendDataEntity
*
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
[
sendDatas
addObject
:[
obj
.
data
mj_JSONObject
]];
id
data
=
[
obj
.
data
mj_JSONObject
];
if
(
data
)
{
[
sendDatas
addObject
:
data
];
}
}];
if
(
sendDatas
.
count
>
0
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment