-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1656 from GeertvanHorrik/pr/progress-reporting
Add percentage calculation
- Loading branch information
Showing
8 changed files
with
197 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Squirrel | ||
{ | ||
using System; | ||
|
||
internal class ApplyReleasesProgress : Progress<int> | ||
{ | ||
private readonly int _releasesToApply; | ||
private int _appliedReleases; | ||
private int _currentReleaseProgress; | ||
|
||
public ApplyReleasesProgress(int releasesToApply, Action<int> handler) | ||
: base(handler) | ||
{ | ||
_releasesToApply = releasesToApply; | ||
} | ||
|
||
public void ReportReleaseProgress(int progressOfCurrentRelease) | ||
{ | ||
_currentReleaseProgress = progressOfCurrentRelease; | ||
|
||
CalculateProgress(); | ||
} | ||
|
||
public void FinishRelease() | ||
{ | ||
_appliedReleases++; | ||
_currentReleaseProgress = 0; | ||
|
||
CalculateProgress(); | ||
} | ||
|
||
private void CalculateProgress() | ||
{ | ||
// Per release progress | ||
var perReleaseProgressRange = 100 / _releasesToApply; | ||
|
||
var appliedReleases = Math.Min(_appliedReleases, _releasesToApply); | ||
var basePercentage = appliedReleases * perReleaseProgressRange; | ||
|
||
var currentReleasePercentage = (perReleaseProgressRange / 100d) * _currentReleaseProgress; | ||
|
||
var percentage = basePercentage + currentReleasePercentage; | ||
OnReport((int)percentage); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Squirrel.Tests | ||
{ | ||
public class ApplyReleasesProgressTests | ||
{ | ||
|
||
[Fact] | ||
public async void CalculatesPercentageCorrectly() | ||
{ | ||
// Just 1 complex situation should be enough to cover this | ||
|
||
var percentage = 0; | ||
var progress = new ApplyReleasesProgress(5, x => percentage = x); | ||
|
||
// 2 releases already finished | ||
progress.FinishRelease(); | ||
progress.FinishRelease(); | ||
|
||
// Report 40 % in current release | ||
progress.ReportReleaseProgress(50); | ||
|
||
// Required for callback to be invoked | ||
await Task.Delay(50); | ||
|
||
// 20 per release | ||
// 10 because we are half-way the 3rd release | ||
var expectedProgress = 20 + 20 + 10; | ||
|
||
Assert.Equal(expectedProgress, percentage); | ||
} | ||
} | ||
} |
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