Skip to content

Commit

Permalink
Send tracks events in batches of no more than 1000 (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjhomer authored Jul 22, 2024
2 parents b11b8be + c96912b commit c58c9c6
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ _None._
### New Features

- Exposed Sentry's `onCrashedLastRun` to `CrashLoggingDataProvider`.
- Tracks events are now reported in batches of up to 1000 events, for better performance under large loads. [#293]

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions Sources/Event Logging/TracksEventPersistenceService.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)persistTracksEvent:(TracksEvent *)tracksEvent;

- (NSArray *)fetchAllTracksEvents;
- (NSArray *)fetchTracksEventsWithLimit:(NSInteger)limit;

- (NSUInteger)countAllTracksEvents;

Expand Down
17 changes: 11 additions & 6 deletions Sources/Event Logging/TracksEventPersistenceService.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

@import Foundation;
@import CoreData;

@interface TracksEventPersistenceService ()

Expand Down Expand Up @@ -42,30 +43,34 @@ - (void)persistTracksEvent:(TracksEvent *)tracksEvent

- (NSArray *)fetchAllTracksEvents
{
return [self fetchTracksEventsWithLimit:0];
}

- (NSArray *)fetchTracksEventsWithLimit:(NSInteger)limit {
__block NSMutableArray *transformedResults;

[self.managedObjectContext performBlockAndWait:^{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"TracksEvent"];

fetchRequest.fetchLimit = limit;

NSError *error;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

if (error) {
TracksLogError(@"Error while fetching all TracksEvent: %@", error);
return;
}

transformedResults = [[NSMutableArray alloc] initWithCapacity:results.count];
for (TracksEventCoreData *eventCoreData in results) {
TracksEvent *tracksEvent = [self mapToTracksEventWithTracksEventCoreData:eventCoreData];
[transformedResults addObject:tracksEvent];
}
}];

return transformedResults;
}


- (NSUInteger)countAllTracksEvents
{
__block NSUInteger count = 0;
Expand Down
1 change: 1 addition & 0 deletions Sources/Event Logging/TracksEventService.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
withAnonymousUserID:(NSString *)anonymousUserID;

- (NSArray *)allTracksEvents;
- (NSArray *)tracksEventsWithLimit:(NSInteger)limit;

- (NSUInteger)numberOfTracksEvents;

Expand Down
5 changes: 5 additions & 0 deletions Sources/Event Logging/TracksEventService.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ - (NSArray *)allTracksEvents
return [self.persistenceService fetchAllTracksEvents];
}

- (NSArray *)tracksEventsWithLimit:(NSInteger)limit
{
return [self.persistenceService fetchTracksEventsWithLimit:limit];
}


- (void)removeTracksEvents:(NSArray *)tracksEvents
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/Event Logging/TracksService.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ - (void)sendQueuedEvents
[self.timer invalidate];
[[NSNotificationCenter defaultCenter] postNotificationName:TrackServiceWillSendQueuedEventsNotification object:nil];

NSArray *events = [self.tracksEventService allTracksEvents];
NSArray *events = [self.tracksEventService tracksEventsWithLimit:1000];

if (events.count == 0) {
[self resetTimer];
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests/ObjC/TracksServiceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ - (void)testSendQueuedEventsOneEvent
tracksEvent.eventName = @"Test";
tracksEvent.userID = @"anonymous123";
NSArray *events = @[tracksEvent];
OCMExpect([self.tracksEventService allTracksEvents]).andReturn(events);
OCMExpect([self.tracksEventService tracksEventsWithLimit:1000]).andReturn(events);

OCMExpect([self.subject.remote sendBatchOfEvents:[OCMArg checkWithBlock:^BOOL(id obj) {
XCTAssertTrue([obj isKindOfClass:[NSArray class]]);
Expand Down

0 comments on commit c58c9c6

Please sign in to comment.