Commits

José Manuel Sánchez Peñarroja committed 6b664ac

Added effort to average time. Fixed issues

Comments (0)

Files changed (8)

Example/Example.xcworkspace/xcuserdata/josanchez.xcuserdatad/UserInterfaceState.xcuserstate

Binary file modified.

Example/Example.xcworkspace/xcuserdata/josanchez.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

             endingColumnNumber = "9223372036854775807"
             startingLineNumber = "225"
             endingLineNumber = "225"
-            landmarkName = "-setFailedError:"
+            landmarkName = "-completedUnitCount"
             landmarkType = "5">
          </BreakpointContent>
       </BreakpointProxy>

bmf/shared/data/BMFProgress.h

 /// This key is used to identify the task. The estimated time will be averaged based on it
 @property (nonatomic, strong) NSString *key;
 
-
 /// Clears completed, total unit counts and estimated time
 - (void) clear;
 
 - (void) addChild:(BMFProgress *) child;
 
 - (void) start:(NSString *) key;
+
 - (void) stop: (NSError *) error;
 
 @end

bmf/shared/data/BMFProgress.m

 	dispatch_sync(serialQueue, ^{
 		_totalUnitCount = totalUnitCount;
 	});
+
+	if (self.key.length>0) [BMFAverageTime setEffort:totalUnitCount forKey:self.key];
 }
 
 - (CGFloat) fractionCompleted {
 	});
 	
 	if (key.length>0) {
-		self.estimatedTime = [BMFAverageTime averageTime:key];	
+		self.estimatedTime = [BMFAverageTime averageTime:key effort:self.totalUnitCount];
 	}
 }
 
 	
 	[self clear];
 
-	[BMFAverageTime startTime:key];
+	[BMFAverageTime startTime:key effort:self.totalUnitCount];
 	
 	self.running = YES;
 }
 
+- (void) start:(NSString *) key effort:(CGFloat) effort {
+	[self start:key];
+}
+
 - (void) stop: (NSError *) error {
 	BMFAssertReturn(self.children.count==0);
 

bmf/shared/data/loaders/BMFAFURLSessionLoader.m

 		}];
 		
 		[sessionManager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
+			
 			_progress.completedUnitCount = task.countOfBytesSent+task.countOfBytesReceived;
 		}];
 		

bmf/shared/stats/BMFAverageTime.h

 @interface BMFAverageTime : NSObject
 
 + (void) startTime:(NSString *) key;
+
+/// Effort is a measure of how hard it is the task. You can use the number of objects, the number of bytes, the nerwork condition, etc
++ (void) startTime:(NSString *) key effort:(CGFloat) effort;
++ (void) setEffort:(CGFloat) effort forKey:(NSString *) key;
+
 + (void) stopTime:(NSString *) key;
-+ (void) cancelTime:(NSString *) key;
+
 + (double) averageTime:(NSString *) key;
++ (double) averageTime:(NSString *) key effort:(CGFloat) effort;
+
++ (void) cancelTime:(NSString *) key;
 
 @end

bmf/shared/stats/BMFAverageTime.m

 
 @property (nonatomic, strong) dispatch_queue_t serialQueue;
 @property (nonatomic, strong) NSMutableDictionary *timesDic;
+@property (nonatomic, strong) NSMutableDictionary *effortsDic;
+
 @property (nonatomic, strong) BMFAverageStats *stats;
+@property (nonatomic, strong) BMFAverageStats *effortStats;
 
 @end
 
 		_serialQueue = dispatch_queue_create("com.bmf.AverageTime", DISPATCH_QUEUE_SERIAL);
 		dispatch_sync(_serialQueue, ^{
 			_timesDic = [NSMutableDictionary dictionary];
+			_effortsDic = [NSMutableDictionary dictionary];
 			NSURL *url = [NSURL fileURLWithPath:[[BMFUtils applicationCacheDirectory] stringByAppendingPathComponent:@"com.bmf.AverageTime"]];
 			_stats = [[BMFAverageStats alloc] initWithFileUrl:url];
+			NSURL *effortUrl = [NSURL fileURLWithPath:[[BMFUtils applicationCacheDirectory] stringByAppendingPathComponent:@"com.bmf.AverageTime.effort"]];
+			_effortStats = [[BMFAverageStats alloc] initWithFileUrl:effortUrl];
 		});
     }
     return self;
 	
 	dispatch_async(averageTime.serialQueue, ^{
 		averageTime.timesDic[key] = [NSDate date];
-//		DDLogDebug(@"Added start date for key: %@ %@",key,averageTime.timesDic[key]);
+	});
+}
+
++ (void) startTime:(NSString *) key effort:(CGFloat) effort {
+	BMFAssertReturn(key.length>0);
+	BMFAssertReturn(effort>0);
+	
+	dispatch_async(averageTime.serialQueue, ^{
+		averageTime.timesDic[key] = [NSDate date];
+		averageTime.effortsDic[key] = @(effort);
+	});
+}
+
++ (void) setEffort:(CGFloat) effort forKey:(NSString *) key {
+	BMFAssertReturn(key.length>0);
+	BMFAssertReturn(effort>0);
+	dispatch_async(averageTime.serialQueue, ^{
+		averageTime.effortsDic[key] = @(effort);
 	});
 }
 
 		[averageTime.timesDic removeObjectForKey:key];
 		
 		[averageTime.stats addValue:time forKey:key];
+		
+		NSNumber *effort = averageTime.effortsDic[key];
+		[averageTime.effortStats addValue:effort.floatValue forKey:key];
 	});
 }
 
 	return average;
 }
 
++ (double) averageTime:(NSString *) key effort:(CGFloat)effort {
+	BMFAssertReturnZero(effort>0);
+	
+	double average = [self averageTime:key];
+	double averageEffort = [averageTime.effortStats averageValueForKey:key];
+	if (averageEffort==-DBL_MAX || averageEffort==0) {
+		return average;
+	}
+	
+	return average*effort/averageEffort;
+}
 
 @end

bmf/shared/subspecs/coredata/BMFCoreDataObjectParser.m

 	return NO;
 }
 
+- (BOOL) saveChanges {
+	NSError *error = nil;
+	BOOL result = [self.context save:&error];
+	if (!result) {
+		DDLogError(@"Error saving context: %@",error);
+	}
+	return result;
+}
+
 @end