Skip to content

Commit

Permalink
Fixing some mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Jan 10, 2024
1 parent 69a40aa commit 4fa0b18
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Agent/Harvester/DataStore/NRMAAgentConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

// Default max event buffer time is 10 minutes (600 seconds).
static NSUInteger __NRMA__maxEventBufferTime = 600;
static NSUInteger __NRMA__maxOfflineStorageSize = 1000000;
static NSUInteger __NRMA__maxOfflineStorageSize = 100000000; // 100 mb

@implementation NRMAAgentConfiguration

Expand Down
7 changes: 4 additions & 3 deletions Agent/Harvester/NRMAHarvesterConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ - (id) init
if (self) {
self.harvestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.offlineStorage = [[NRMAOfflineStorage alloc] initWithEndpoint:@"data"];
[_offlineStorage setMaxOfflineStorageSize:[NRMAAgentConfiguration getMaxOfflineStorageSize]];
}
return self;
}
Expand All @@ -42,7 +41,7 @@ -(void) sendOfflineStorage {
return;
}
NRLOG_VERBOSE(@"Number of offline data posts: %lu", (unsigned long)offlineData.count);

__block NSUInteger totalSize = 0;
[offlineData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSData *jsonData = (NSData *)obj;

Expand All @@ -51,14 +50,16 @@ -(void) sendOfflineStorage {
NRLOG_ERROR(@"Failed to create data POST");
return;
}
[NRMASupportMetricHelper enqueueOfflinePayloadMetric:[post.HTTPBody length]];

NRMAHarvestResponse* response = [self send:post];

if([NRMAOfflineStorage checkErrorToPersist:response.error]) {
[_offlineStorage persistDataToDisk:jsonData];
} else {
totalSize += [post.HTTPBody length];
}
}];
[NRMASupportMetricHelper enqueueOfflinePayloadMetric:totalSize];
}

- (NSURLRequest*) createPostWithURI:(NSString*)url message:(NSString*)message
Expand Down
15 changes: 9 additions & 6 deletions Agent/Utilities/NRMAOfflineStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#import "Constants.h"
#import "NRMAJSON.h"
#import "NRMASupportMetricHelper.h"
#import "NRMAAgentConfiguration.h"

#define kNRMAOfflineStorageCurrentSize @"com.newrelic.offlineStorageCurrentSize"
#define kNRMAOfflineStorageCurrentSizeKey @"com.newrelic.offlineStorageCurrentSize"

@implementation NRMAOfflineStorage {
NSUInteger maxOfflineStorageSize;
Expand All @@ -25,7 +26,7 @@ - (id)initWithEndpoint:(NSString*) name {
self = [super init];
if (self) {
_name = name;
maxOfflineStorageSize = 1000000;
maxOfflineStorageSize = [NRMAAgentConfiguration getMaxOfflineStorageSize];
}
return self;
}
Expand All @@ -44,7 +45,7 @@ - (BOOL) persistDataToDisk:(NSData*) data {
@synchronized (self) {
[self createDirectory];

NSUInteger currentOfflineStorageSize = [[NSUserDefaults standardUserDefaults] integerForKey:kNRMAOfflineStorageCurrentSize];
NSUInteger currentOfflineStorageSize = [[NSUserDefaults standardUserDefaults] integerForKey:kNRMAOfflineStorageCurrentSizeKey];
currentOfflineStorageSize += data.length;
if(currentOfflineStorageSize > maxOfflineStorageSize){
NRLOG_WARNING(@"Not saving to offline storage because max storage size has been reached.");
Expand All @@ -54,7 +55,7 @@ - (BOOL) persistDataToDisk:(NSData*) data {
NSError *error = nil;
if (data) {
if ([data writeToFile:[self newOfflineFilePath] options:NSDataWritingAtomic error:&error]) {
[[NSUserDefaults standardUserDefaults] setInteger:currentOfflineStorageSize forKey:kNRMAOfflineStorageCurrentSize]; // If we successfully save the data save the new current total size
[[NSUserDefaults standardUserDefaults] setInteger:currentOfflineStorageSize forKey:kNRMAOfflineStorageCurrentSizeKey]; // If we successfully save the data save the new current total size
NRLOG_VERBOSE(@"Successfully persisted failed upload data to disk for offline storage. Current offline storage: %lu", (unsigned long)currentOfflineStorageSize);
return YES;
}
Expand Down Expand Up @@ -88,10 +89,12 @@ - (BOOL) persistDataToDisk:(NSData*) data {
}

- (BOOL) clearAllOfflineFiles {
if ([[NSFileManager defaultManager] removeItemAtPath:[self offlineDirectoryPath] error:NULL]) {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kNRMAOfflineStorageCurrentSize];
NSError* error;
if ([[NSFileManager defaultManager] removeItemAtPath:[self offlineDirectoryPath] error:&error]) {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kNRMAOfflineStorageCurrentSizeKey];
return true;
}
NRLOG_ERROR(@"Failed to clear offline storage: %@", error);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ -(void) setUp {
[super setUp];
self.offlineStorage = [[NRMAOfflineStorage alloc] initWithEndpoint:@"Test"];
[self.offlineStorage clearAllOfflineFiles];
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"com.newrelic.offlineStorageCurrentSize"];
}

-(void) tearDown {
[super tearDown];
[self.offlineStorage clearAllOfflineFiles];
}

- (unsigned long long)folderSize:(NSString *)folderPath {
Expand Down

0 comments on commit 4fa0b18

Please sign in to comment.