-
Notifications
You must be signed in to change notification settings - Fork 276
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 #624 from dabutvin/threshold
configuration option for compression threshold (fixes #562)
- Loading branch information
Showing
8 changed files
with
174 additions
and
11 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
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,26 @@ | ||
using System.Linq; | ||
using Common; | ||
|
||
namespace CompressImagesFunction | ||
{ | ||
public class Threshold | ||
{ | ||
/// <summary> | ||
/// Using the compressionResults and the repoConfiguration determine whether | ||
/// the optimization warrants a PR at this time. | ||
/// </summary> | ||
/// <returns>True when the images are compressed enough to warrant a PR.</returns> | ||
public static bool MeetsThreshold(RepoConfiguration repoConfiguration, CompressionResult[] compressionResults) | ||
{ | ||
if (repoConfiguration.MinKBReduced == null || repoConfiguration.MinKBReduced <= 0) | ||
{ | ||
// no threshold specified - let's continue | ||
return true; | ||
} | ||
|
||
// determine total KB reduced | ||
var totalKBReduced = compressionResults.Sum(x => x.SizeBefore - x.SizeAfter); | ||
return repoConfiguration.MinKBReduced <= totalKBReduced; | ||
} | ||
} | ||
} |
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,23 @@ | ||
You can set a space saved threshold using the `.imgbotconfig` file. | ||
|
||
- This configuration is optional and is only required if you want to change the default threshold | ||
- Default setting is 10KB | ||
- Accepts only numbers as input (e.g. `"minKBReduced": 500` for a 500 KB threshold) | ||
- Can be used to limit the frequency of PRs Imgbot will open over time | ||
|
||
`.imgbotconfig` | ||
|
||
Setting 500 KB threshold | ||
|
||
``` | ||
{ | ||
"minKBReduced": 500 | ||
} | ||
``` | ||
|
||
To disable this threshold and always open a PR no matter how much size is reduced unset the default | ||
``` | ||
{ | ||
"minKBReduced": null | ||
} | ||
``` |
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,92 @@ | ||
using Common; | ||
using CompressImagesFunction; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Test | ||
{ | ||
[TestClass] | ||
public class ThresholdTests | ||
{ | ||
/// We have a default threshold set so it won't meet it by default | ||
[TestMethod] | ||
public void GivenDefaultConfiguration_ShouldNotOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] { }; | ||
var configuration = new RepoConfiguration(); | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsFalse(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void GivenDisabledConfiguration_ShouldOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] { }; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = null | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsTrue(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void Given0_ShouldOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] { }; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = 0 | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsTrue(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void GivenBelowThreshold_ShouldOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] | ||
{ | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4000, | ||
}, | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4999, | ||
}, | ||
}; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = 500 | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsTrue(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void GivenAboveThreshold_ShouldNotOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] | ||
{ | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4900, | ||
}, | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4999, | ||
}, | ||
}; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = 500 | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsFalse(shouldOptimize); | ||
} | ||
} | ||
} |
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