/// <summary> /// /// </summary> /// <param name="State"></param> /// <returns></returns> public bool HasDuration(ManifestDownloadProgressState State) { foreach (DownloadStateDurationValue val in Values) { if (val.State == State) { return(true); } } return(false); }
/// <summary> /// /// </summary> /// <param name="State"></param> /// <returns></returns> public ulong GetDuration(ManifestDownloadProgressState State) { foreach (DownloadStateDurationValue val in Values) { if (val.State == State) { return(val.Elapsed); } } return(0); }
/// <summary> /// /// </summary> /// <param name="State"></param> /// <returns></returns> public void SetDuration(ManifestDownloadProgressState State, ulong NewValue) { foreach (DownloadStateDurationValue val in Values) { if (val.State == State) { val.Elapsed = NewValue; return; } } DownloadStateDurationValue New = new DownloadStateDurationValue(); New.State = State; New.Elapsed = NewValue; Values.Add(New); }
/// <summary> /// /// </summary> /// <param name="State"></param> /// <param name="TotalDownloadSize"></param> /// <param name="CurrentProgress"></param> public long GetEstimatedTimeRemainingForState(DownloadState State, ManifestDownloadProgressState ProgressState) { double DurationSum = 0; int SumCount = 0; long TotalSize = 0; double RawEstimateSeconds = 0.0f; double RawProgress = 0.0f; double CurrentStateDuration = 0.0f; if (State.PendingDurationHistory != null) { CurrentStateDuration = State.PendingDurationHistory.GetDuration(ProgressState); } ManifestDownloadState Downloader = ManifestDownloader.GetDownload(State.ActiveManifestId); if (Downloader != null && Downloader.Manifest != null) { TotalSize = Downloader.Manifest.GetTotalSize(); // Calculate raw estimate based on speeds and data remaining. switch (Downloader.State) { case ManifestDownloadProgressState.Initializing: { /*RawEstimateSeconds = Downloader.InitializeBytesRemaining / (double)Downloader.InitializeRateStats.RateIn; * RawProgress = Downloader.InitializeProgress; * if (Downloader.InitializeRateStats.RateIn == 0) * { * RawEstimateSeconds = 0; * } * break;*/ Downloader.InitializeRateEstimater.SetProgress(Downloader.InitializeProgress); Downloader.InitializeRateEstimater.Poll(); RawEstimateSeconds = Downloader.InitializeRateEstimater.EstimatedSeconds; RawProgress = Downloader.InitializeRateEstimater.EstimatedProgress; break; } case ManifestDownloadProgressState.DeltaCopying: { /*RawEstimateSeconds = Downloader.DeltaCopyBytesRemaining / (double)Downloader.DeltaCopyRateStats.RateIn; * RawProgress = Downloader.DeltaCopyProgress; * if (Downloader.DeltaCopyRateStats.RateIn == 0) * { * RawEstimateSeconds = 0; * } * break;*/ Downloader.DeltaCopyRateEstimater.SetProgress(Downloader.DeltaCopyProgress); Downloader.DeltaCopyRateEstimater.Poll(); RawEstimateSeconds = Downloader.DeltaCopyRateEstimater.EstimatedSeconds; RawProgress = Downloader.DeltaCopyRateEstimater.EstimatedProgress; break; } case ManifestDownloadProgressState.Validating: { /*RawEstimateSeconds = Downloader.ValidateBytesRemaining / (double)Downloader.ValidateRateStats.RateOut; * RawProgress = Downloader.ValidateProgress; * if (Downloader.ValidateRateStats.RateOut == 0) * { * RawEstimateSeconds = 0; * } * break;*/ Downloader.ValidateRateEstimater.SetProgress(Downloader.ValidateProgress); Downloader.ValidateRateEstimater.Poll(); RawEstimateSeconds = Downloader.ValidateRateEstimater.EstimatedSeconds; RawProgress = Downloader.ValidateRateEstimater.EstimatedProgress; break; } case ManifestDownloadProgressState.Downloading: { /*RawEstimateSeconds = Downloader.BytesRemaining / (double)Downloader.BandwidthStats.RateIn; * RawProgress = Downloader.Progress; * if (Downloader.BandwidthStats.RateIn == 0) * { * RawEstimateSeconds = 0; * } * break;*/ Downloader.DownloadRateEstimater.SetProgress(Downloader.Progress); Downloader.DownloadRateEstimater.Poll(); RawEstimateSeconds = Downloader.DownloadRateEstimater.EstimatedSeconds; RawProgress = Downloader.DownloadRateEstimater.EstimatedProgress; break; } case ManifestDownloadProgressState.Installing: { Downloader.InstallRateEstimater.SetProgress(Downloader.InstallProgress); Downloader.InstallRateEstimater.Poll(); RawEstimateSeconds = Downloader.InstallRateEstimater.EstimatedSeconds; RawProgress = Downloader.InstallRateEstimater.EstimatedProgress; break; } } } // Calculate average historic duration adjusted to the difference in download size. foreach (DownloadStateDuration Duration in State.DurationHistory) { if (Duration.HasDuration(ProgressState)) { ulong Unadjusted = Duration.GetDuration(ProgressState); float Adjustment = TotalSize == 0 ? 1.0f : (float)Duration.TotalSize / (float)TotalSize; ulong AdjustedDuration = (ulong)(Unadjusted * Adjustment); DurationSum += AdjustedDuration; SumCount++; } } if (SumCount != 0) { double Historic = Math.Max(0, (DurationSum / SumCount) - CurrentStateDuration) / 1000.0f;// ((DurationSum / SumCount) * (1.0f - RawProgress)) / 1000.0f; if (Historic == 0.0f) { return((long)RawEstimateSeconds); } else { double Combined = (Historic * 0.75f) + (RawEstimateSeconds * 0.25f); return((long)Combined); } } else { return((long)RawEstimateSeconds); } }