Skip to content

Instantly share code, notes, and snippets.

@nikeee
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikeee/34dc2cfd449c399c13ad to your computer and use it in GitHub Desktop.
Save nikeee/34dc2cfd449c399c13ad to your computer and use it in GitHub Desktop.
/// <remarks>
/// Proposal. May change.
/// May tie ETA calculation to object instances of (in this case) T.
/// </remarks>
public abstract class EtaCalculator<T>
{
public abstract TimeSpan CalculateEta(T processedUnits, T totalUnits, DateTime startTime);
public abstract TimeSpan CalculateEta(T processedUnits, T totalUnits, DateTime startTime, DateTime now);
}
public class ByteSizeEtaCalculator : EtaCalculator<ByteSize>
{
private static NumberEtaCalculator _internalCalculator = new NumberEtaCalculator();
public TimeSpan CalculateEta(ByteSize processedBytes, ByteSize totalBytes, DateTime startTime)
{
return _internalCalculator.CalculateEta(processedBytes.ByteCount, totalBytes.ByteCount, startTime);
}
public TimeSpan CalculateEta(ByteSize processedBytes, ByteSize totalBytes, DateTime startTime, DateTime now)
{
return _internalCalculator.CalculateEta(processedBytes.ByteCount, totalBytes.ByteCount, startTime, now);
}
}
public class NumberEtaCalculator : EtaCalculator<long>
{
public TimeSpan CalculateEta(long processedUnits, long totalUnits, DateTime startTime)
{
return CalculateEta(processedUnits, totalUnits, startTime, DateTime.Now);
}
public TimeSpan CalculateEta(long processedUnits, long totalUnits, DateTime startTime, DateTime now)
{
var passedSeconds = (startTime - now).TotalSeconds;
if(passedSeconds == 0.0)
return TimeSpan.MaxValue;
double unitsPerSecond = processedUnits / passedSeconds;
if(unitsPerSecond == 0.0)
return TimeSpan.MaxValue;
double secondsRemaining = (totalUnits - processedUnits) / unitsPerSecond;
return TimeSpan.FromSeconds(secondsRemaining);
// if(processedBytes == 0)
// return TimeSpan.MaxValue;
// var millisecondsLeft = startTime.TotalMilliseconds / processedBytes.ByteCount * totalBytes.ByteCount;
// return TimeSpan.FromMilliseconds(millisecondsLeft);
}
}
public class ByteSizeSpeedCalulator
{
private static readonly DefaultInterval = TimeSpan.FromSeconds(1.0);
private DateTime _lastUpdate;
private ByteSize _lastProcessedBytes;
public SpeedItem<ByteSize> Update(ByteSize processedBytes)
{
var now = DateTime.Now;
var delta = now - _lastUpdate;
if(_lastProcessedBytes == null)
_lastProcessedBytes = new ByteSize(0);
var byteDiff = processedBytes.ByteCount - _lastProcessedBytes.ByteCount;
var speed = byteDiff / delta.TotalSeconds;
_lastUpdate = now;
return new SpeedItem<ByteSize>(new ByteSize((long)speed), DefaultInterval);
}
}
public class SpeedItem<T>
{
public T Units { get; private set; }
public TimeSpan Interval { get; private set; }
public SpeedItem(T units, TimeSpan interval)
{
Units = units;
Interval = interval;
}
}
interface ISpeedFormatter<T>
{
string Format(SpeedItem<T> SpeedItem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment