-
Notifications
You must be signed in to change notification settings - Fork 54
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
Upload duration logic #281
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e35852
android upload duration logic
parveshneedhoo db50edb
upload duration logic ios
parveshneedhoo f9225cd
fix migration crash
parveshneedhoo 5005ce3
save start and end upload time
parveshneedhoo ae16170
refactor android side plugin
parveshneedhoo 318075c
refactor
parveshneedhoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#import "FileUploader.h" | ||
@interface FileUploader() | ||
@property (nonatomic, strong) NSMutableDictionary* responsesData; | ||
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDate *> *uploadStartTimes; | ||
@property (nonatomic, strong) NSMutableDictionary *responsesData; | ||
@property (nonatomic, strong) AFURLSessionManager *manager; | ||
@end | ||
|
||
|
@@ -20,32 +21,48 @@ -(id)init{ | |
return nil; | ||
[UploadEvent setupStorage]; | ||
self.responsesData = [[NSMutableDictionary alloc] init]; | ||
self.uploadStartTimes = [[NSMutableDictionary alloc] init]; | ||
NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[[NSBundle mainBundle] bundleIdentifier]]; | ||
configuration.HTTPMaximumConnectionsPerHost = FileUploader.parallelUploadsLimit; | ||
configuration.sessionSendsLaunchEvents = NO; | ||
self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; | ||
__weak FileUploader *weakSelf = self; | ||
[self.manager setTaskDidCompleteBlock:^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSError * _Nullable error) { | ||
NSString* uploadId = [NSURLProtocol propertyForKey:kUploadUUIDStrPropertyKey inRequest:task.originalRequest]; | ||
NSDate *startTime = weakSelf.uploadStartTimes[uploadId]; | ||
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:startTime]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format/accuracy is same as android? |
||
NSLog(@"[BackgroundUpload] Task %@ completed with error %@", uploadId, error); | ||
if (!error){ | ||
NSData* serverData = weakSelf.responsesData[@(task.taskIdentifier)]; | ||
NSString* serverResponse = serverData ? [[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding] : @""; | ||
[weakSelf.responsesData removeObjectForKey:@(task.taskIdentifier)]; | ||
[weakSelf saveAndSendEvent:@{ | ||
@"id" : uploadId, | ||
@"state" : @"UPLOADED", | ||
@"statusCode" : @(((NSHTTPURLResponse *)task.response).statusCode), | ||
@"serverResponse" : serverResponse | ||
}]; | ||
if (isnumber(duration)) { | ||
[weakSelf saveAndSendEvent:@{ | ||
@"id" : uploadId, | ||
@"state" : @"UPLOADED", | ||
@"statusCode" : @(((NSHTTPURLResponse *)task.response).statusCode), | ||
@"serverResponse" : serverResponse, | ||
@"uploadDuration" : @(duration) | ||
}]; | ||
} else { | ||
[weakSelf saveAndSendEvent:@{ | ||
@"id" : uploadId, | ||
@"state" : @"UPLOADED", | ||
@"statusCode" : @(((NSHTTPURLResponse *)task.response).statusCode), | ||
@"serverResponse" : serverResponse, | ||
@"uploadDuration" : @"N/A" | ||
}]; | ||
} | ||
} else { | ||
[weakSelf.responsesData removeObjectForKey:@(task.taskIdentifier)]; | ||
[weakSelf saveAndSendEvent:@{ | ||
@"id" : uploadId, | ||
@"state" : @"FAILED", | ||
@"error" : error.localizedDescription, | ||
@"errorCode" : @(error.code) | ||
@"errorCode" : @(error.code), | ||
}]; | ||
} | ||
[weakSelf.uploadStartTimes removeObjectForKey:uploadId]; // Clean up | ||
}]; | ||
|
||
[self.manager setDataTaskDidReceiveDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSData * _Nonnull data) { | ||
|
@@ -60,7 +77,7 @@ -(id)init{ | |
} | ||
|
||
-(void)saveAndSendEvent:(NSDictionary*)data{ | ||
UploadEvent*event = [UploadEvent create:data]; | ||
UploadEvent* event = [UploadEvent create:data]; | ||
[self sendEvent:[event dataRepresentation]]; | ||
} | ||
|
||
|
@@ -89,6 +106,9 @@ -(void)addUpload:(NSDictionary *)payload completionHandler:(void (^)(NSError* er | |
completionHandler:^(NSError *error, NSMutableURLRequest *request) { | ||
if (error) | ||
return handler(error); | ||
|
||
weakSelf.uploadStartTimes[payload[@"id"]] = [NSDate date]; | ||
|
||
__block double lastProgressTimeStamp = 0; | ||
|
||
[[weakSelf.manager uploadTaskWithRequest:request | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of storing
uploadDuration
, why don't we store thestartTime
andendTime
. TheuploadDuration
is calculated from these values. Normally it is better to save the values from which it calculated rather than the value itself. (Example saving the date of birth in a db rather than the age as age can be calculated with the dob)If we save the the starting and ending times, we can then calculate the uploadDuration and return it alongside the ending time also to the JS and set our
uploadedAt
to the returned ending time value.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DOB is saved instead of age because age changes, here the
uploadDuration
will not change. I agree that we need to send theuploadedAt
value also but I think it is better that we calculate the uploadDuration on the plugin side which will be calculated immediately after the upload instead of waiting for the JS side to wait for the values to do the calculations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes my suggestion was to calculate the
uploadDuration
on the plugin side itself and not calculate it on the JS side but to save the values from which we derive theuploadDuration
in the db and return theuploadDuration
AND theuploadedAt
time also.I gave the age as an example. Other examples can be keeping the
buyingPrice
andsellingPrice
then derive theprofit
from it althought the profit won't change. In this case theuploadDuration
won't change but saving the start and end times gives more information than just theuploadDuration
.