Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event not getting removed from DB and certain processing issue #340

Merged
merged 14 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ PODS:
- nanopb/decode (2.30909.0)
- nanopb/encode (2.30909.0)
- PromisesObjC (2.2.0)
- Rudder (1.15.1)
- Rudder (1.16.0)
- Rudder-Amplitude (1.0.2):
- Amplitude (~> 7.2.0)
- Rudder
- Rudder-Braze (1.2.0):
- Appboy-iOS-SDK (~> 4.5.4)
- Rudder (~> 1.12)
- SDWebImage (5.15.8):
- SDWebImage/Core (= 5.15.8)
- SDWebImage/Core (5.15.8)
- SDWebImage (5.16.0):
- SDWebImage/Core (= 5.16.0)
- SDWebImage/Core (5.16.0)

DEPENDENCIES:
- Amplitude (~> 7.2.0)
Expand Down Expand Up @@ -121,11 +121,11 @@ SPEC CHECKSUMS:
GoogleUtilities: 9aa0ad5a7bc171f8bae016300bfcfa3fb8425749
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
PromisesObjC: 09985d6d70fbe7878040aa746d78236e6946d2ef
Rudder: 41040d4537a178e4e32477b68400f98ca0c354eb
Rudder: 718bc5bf0de0e4d83850dedd792f08e5e2065a0a
Rudder-Amplitude: f845cc125a1a58d4de6155391a2b0392815ae898
Rudder-Braze: e42eb914a03cb418ed8b7a3cd90b724a91476631
SDWebImage: cb032eba469c54e0000e78bcb0a13cdde0a52798
SDWebImage: 2aea163b50bfcb569a2726b6a754c54a4506fcf6

PODFILE CHECKSUM: 42bfa6ba9271b8d9518a669daca1fd9a7bbf9f6e

COCOAPODS: 1.12.1
COCOAPODS: 1.11.3
1 change: 1 addition & 0 deletions Sources/Classes/Headers/Public/RSDBPersistentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
-(void) clearProcessedEventsFromDB;
- (int) getDBRecordCountForMode:(MODES) mode;
-(void) flushEventsFromDB;
-(void) markUnProcessedDeviceModeEventsStatusesAsDeviceModeProcessingDone;
pallabmaiti marked this conversation as resolved.
Show resolved Hide resolved

@end

Expand Down
3 changes: 2 additions & 1 deletion Sources/Classes/Headers/Public/RSDeviceModeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
RSEventFilteringPlugin *eventFilteringPlugin;
RSConsentFilterHandler *consentFilterHandler;
NSMutableDictionary<NSString*, id<RSIntegration>>* integrationOperationMap;
NSMutableDictionary<NSNumber*, RSMessage*> *eventReplayMessage;
NSDictionary<NSString*, NSString*>* destinationsWithTransformationsEnabled;
BOOL areFactoriesInitialized;
BOOL isDeviceModeFactoriesNotPresent;
}

- (instancetype) initWithConfig:(RSConfig *) config andDBPersistentManager:(RSDBPersistentManager *)dbPersistentManager andNetworkManager:(RSNetworkManager *)networkManager;
Expand All @@ -32,5 +32,6 @@
- (void) dumpTransformedEvents:(NSArray*) transformedPayloads toDestinationId:(NSString*) destinationId;
- (void) reset;
- (void) flush;
- (void) handleCaseWhenNoDeviceModeFactoryIsPresent;

@end
3 changes: 3 additions & 0 deletions Sources/Classes/Headers/Public/RSPreferenceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ extern NSString *const RSSessionAutoTrackStatus;
- (void) saveAutoTrackingStatus: (BOOL) autoTrackingStatus;
- (BOOL) getAutoTrackingStatus;

- (void) saveEventDeletionStatus;
- (BOOL) getEventDeletionStatus;

@end

NS_ASSUME_NONNULL_END
2 changes: 2 additions & 0 deletions Sources/Classes/Headers/Public/RSUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (BOOL) isValidURL:(NSURL*) url;
+ (NSString*) appendSlashToUrl:(NSString*) url;
+ (NSString* _Nullable) getBase64EncodedString:(NSString* __nonnull) inputString;
+ (NSNumber * _Nullable) convertStringIntoNSNumber:(NSString* __nonnull) stringNumber;
+ (BOOL) isApplicationUpdated;

extern unsigned int MAX_EVENT_SIZE;
extern unsigned int MAX_BATCH_SIZE;
Expand Down
11 changes: 11 additions & 0 deletions Sources/Classes/RSDBPersistentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ - (void)clearEventsFromDB:(NSMutableArray<NSString *> *)messageIds {
}
}

-(void) markUnProcessedDeviceModeEventsStatusesAsDeviceModeProcessingDone {
NSString* updateEventStatusSQL = [[NSString alloc] initWithFormat:@"UPDATE %@ SET %@ = (%@ | %d) WHERE %@ IN (%d, %d);", TABLE_EVENTS, COL_STATUS, COL_STATUS, DEVICE_MODE_PROCESSING_DONE, COL_STATUS, NOT_PROCESSED, CLOUD_MODE_PROCESSING_DONE];
@synchronized (self) {
if([self execSQL:updateEventStatusSQL]) {
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDBPersistentManager: markUnProcessedDeviceModeEventsStatusesAsDeviceModeProcessingDone: Successfully updated the event status for unprocessed and cloud mode processing done events to device mode processing done."]];
return;
}
[RSLogger logError:[[NSString alloc] initWithFormat:@"RSDBPersistentManager: markUnProcessedDeviceModeEventsStatusesAsDeviceModeProcessingDone: Failed to update the status for events."]];
}
}

-(RSDBMessage *)fetchEventsFromDB:(int)count ForMode:(MODES) mode {
NSString* querySQLString = nil;
switch(mode) {
Expand Down
36 changes: 19 additions & 17 deletions Sources/Classes/RSDeviceModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ - (instancetype)initWithConfig:(RSConfig *) config andDBPersistentManager:(RSDBP
self->dbPersistentManager = dbPersistentManager;
self->networkManager = networkManager;
self->integrationOperationMap = [[NSMutableDictionary alloc] init];
self->eventReplayMessage = [[NSMutableDictionary alloc] init];
}
return self;
}
Expand Down Expand Up @@ -49,13 +48,20 @@ - (void) startDeviceModeProcessor:(NSArray<RSServerDestination*>*) destinations
}
}

- (void) handleCaseWhenNoDeviceModeFactoryIsPresent {
self->isDeviceModeFactoriesNotPresent = YES;
[self replayMessageQueue];
}

- (void) initiateFactories : (NSArray*) destinations {
if (![self areFactoriesPassedInConfig]) {
[RSLogger logInfo:@"RSDeviceModeManager: initiateFactories: No native SDK is found in the config"];
self->isDeviceModeFactoriesNotPresent = YES;
return;
}
if (destinations.count == 0) {
[RSLogger logInfo:@"RSDeviceModeManager: initiateFactories: No native SDK factory is found in the server config"];
self->isDeviceModeFactoriesNotPresent = YES;
return;
}
RSClient* client = [RSClient sharedInstance];
Expand Down Expand Up @@ -110,21 +116,23 @@ - (void) initiateCustomFactories {
}

- (void) replayMessageQueue {
@synchronized (self->eventReplayMessage) {
[RSLogger logDebug:@"RSDeviceModeManager: replayMessageQueue: replaying old messages with factory"];
if (self->eventReplayMessage.count > 0) {
NSMutableArray<NSNumber*>* rowIds = [self->eventReplayMessage.allKeys mutableCopy];
[RSUtils sortArray:rowIds inOrder:ASCENDING];
for(NSNumber* rowId in rowIds) {
[self makeFactoryDump:eventReplayMessage[rowId] FromHistory:YES withRowId:rowId];
}
RSDBMessage *dbMessage = [self->dbPersistentManager fetchAllEventsFromDBForMode:DEVICEMODE];
NSArray *messageIds = dbMessage.messageIds;
NSArray *messages = dbMessage.messages;
for (int i=0; i<messageIds.count; i++) {
id object = [RSUtils deSerializeJSONString:messages[i]];
NSNumber *rowId = [RSUtils convertStringIntoNSNumber:messageIds[i]];
pallabmaiti marked this conversation as resolved.
Show resolved Hide resolved
if (object && rowId) {
RSMessage* originalMessage = [[RSMessage alloc] initWithDict:object];
[self makeFactoryDump:originalMessage FromHistory:YES withRowId:rowId];
}
[self->eventReplayMessage removeAllObjects];
}
}

- (void) makeFactoryDump:(RSMessage *)message FromHistory:(BOOL) fromHistory withRowId:(NSNumber *) rowId {
if (self->areFactoriesInitialized || fromHistory) {
if (self->isDeviceModeFactoriesNotPresent) {
[self->dbPersistentManager updateEventWithId:rowId withStatus:DEVICE_MODE_PROCESSING_DONE];
} else if (self->areFactoriesInitialized || fromHistory) {
[RSLogger logVerbose:@"RSDeviceModeManager: makeFactoryDump: dumping message to native sdk factories"];
NSArray<NSString *>* eligibleDestinations = [self getEligibleDestinations:message];

Expand All @@ -140,12 +148,6 @@ - (void) makeFactoryDump:(RSMessage *)message FromHistory:(BOOL) fromHistory wit

NSArray<NSString *>* destinationsWithoutTransformations = [self getDestinationsWithTransformationStatus:DISABLED fromDestinations:eligibleDestinations];
[self dumpEvent:message toDestinations:destinationsWithoutTransformations withLogTag:@"makeFactoryDump"];

} else {
@synchronized (self->eventReplayMessage) {
[RSLogger logDebug:@"RSDeviceModeManager: makeFactoryDump: factories are not initialized. dumping to replay queue"];
self->eventReplayMessage[rowId] = message;
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions Sources/Classes/RSEventRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ - (instancetype)init:(NSString*)_writeKey config:(RSConfig*)_config client:(RSCl
[RSLogger logDebug:@"EventRepository: Initiating RSDeviceModeManager"];
self->deviceModeManager = [[RSDeviceModeManager alloc] initWithConfig:config andDBPersistentManager:self->dbpersistenceManager andNetworkManager:self->networkManager];

// Prviously in certain cases (e.g., network unavialability) events are not processed by device mode factories and statuses remains at either 0 or 2. Now we are marking those events as device_mode_processing_done.
[RSLogger logDebug:@"EventRepository: Checking if previous unprocessed event deletion is allowed or not. If allowed then mark previous events with status code 0 and 2 as device_mode_processing_done."];
if ([self isPreviousEventsDeletionAllowed]) {
[self->dbpersistenceManager markUnProcessedDeviceModeEventsStatusesAsDeviceModeProcessingDone];
[self->dbpersistenceManager clearProcessedEventsFromDB];
}

[RSLogger logDebug:@"EventRepository: Initiating the SDK"];
[self __initiateSDK];

Expand Down Expand Up @@ -133,6 +140,14 @@ - (instancetype)init:(NSString*)_writeKey config:(RSConfig*)_config client:(RSCl
return self;
}

- (BOOL) isPreviousEventsDeletionAllowed {
if (![preferenceManager getEventDeletionStatus]) {
[preferenceManager saveEventDeletionStatus];
return [RSUtils isApplicationUpdated];
}
return NO;
}

- (void) setAnonymousIdToken {
NSData *anonymousIdData = [[[NSString alloc] initWithFormat:@"%@:", [RSElementCache getAnonymousId]] dataUsingEncoding:NSUTF8StringEncoding];
dispatch_sync(repositoryQueue, ^{
Expand Down Expand Up @@ -167,13 +182,15 @@ - (void) __initiateSDK {
[RSLogger logDebug:@"EventRepository: Initiating ConsentFilterHandler"];
strongSelf->consentFilterHandler = [RSConsentFilterHandler initiate:strongSelf->config.consentFilter withServerConfig:serverConfig];
}

// initiate the native SDK factories if destinations are present
if (serverConfig.destinations != nil && serverConfig.destinations.count > 0) {
NSArray <RSServerDestination *> *consentedDestinations = self->consentFilterHandler != nil ? [self->consentFilterHandler filterDestinationList:serverConfig.destinations] : serverConfig.destinations;
if(consentedDestinations != nil && consentedDestinations.count > 0 ) {
[self->deviceModeManager startDeviceModeProcessor:consentedDestinations andDestinationsWithTransformationsEnabled:[strongSelf->configManager getDestinationsWithTransformationsEnabled]];
}
} else {
[self->deviceModeManager handleCaseWhenNoDeviceModeFactoryIsPresent];
[RSLogger logDebug:@"EventRepository: no device mode present"];
}
} else {
Expand Down
11 changes: 10 additions & 1 deletion Sources/Classes/RSPreferenceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ @implementation RSPreferenceManager
NSString *const RSSessionIdKey = @"rl_session_id";
NSString *const RSLastEventTimeStamp = @"rl_last_event_time_stamp";
NSString *const RSSessionAutoTrackStatus = @"rl_session_auto_track_status";

NSString *const RSEventDeletionStatus = @"rl_event_deletion_status";

+ (instancetype)getInstance {
if (instance == nil) {
Expand Down Expand Up @@ -255,4 +255,13 @@ - (BOOL) getAutoTrackingStatus {
return [[NSUserDefaults standardUserDefaults] boolForKey:RSSessionAutoTrackStatus];
}

- (void) saveEventDeletionStatus {
[[NSUserDefaults standardUserDefaults] setBool:true forKey:RSEventDeletionStatus];
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL) getEventDeletionStatus {
return [[NSUserDefaults standardUserDefaults] boolForKey:RSEventDeletionStatus];
}

@end
12 changes: 12 additions & 0 deletions Sources/Classes/RSUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ + (NSString* _Nullable) getBase64EncodedString:(NSString* __nonnull) inputString
return base64EncodedString;
}

+ (NSNumber * _Nullable) convertStringIntoNSNumber:(NSString* __nonnull) stringNumber {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *number = [numberFormatter numberFromString:stringNumber];
return number;
}

+ (BOOL) isApplicationUpdated {
pallabmaiti marked this conversation as resolved.
Show resolved Hide resolved
NSString *previousVersion = [[RSPreferenceManager getInstance] getVersionNumber];
NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
return (previousVersion && ![previousVersion isEqualToString:currentVersion]);
}

unsigned int MAX_EVENT_SIZE = 32 * 1024; // 32 KB
unsigned int MAX_BATCH_SIZE = 500 * 1024; // 500 KB

Expand Down