Skip to content

Commit

Permalink
configuration option for compression threshold (fixes #562)
Browse files Browse the repository at this point in the history
  • Loading branch information
dabutvin committed Apr 5, 2020
1 parent 395d89c commit a56b2db
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Common/RepoConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class RepoConfiguration
public bool AggressiveCompression { get; set; }

public bool CompressWiki { get; set; }

public int? MinKBReduced { get; set; } = 10;
}
}
3 changes: 3 additions & 0 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public static bool Run(CompressimagesParameters parameters, ICollector<CompressI
if (optimizedImages.Length == 0)
return false;

if (!Threshold.MeetsThreshold(repoConfiguration, optimizedImages))
return false;

// create commit message based on optimizations
foreach (var image in optimizedImages)
{
Expand Down
26 changes: 26 additions & 0 deletions CompressImagesFunction/Threshold.cs
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;
}
}
}
5 changes: 3 additions & 2 deletions Docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Here is an example .imgbotconfig setup that shows some of the options.
"image1.png", // ignore by filename
"public/special_images/*", // ignore by folderpath
],
"aggressiveCompression": "true" // true|false
"compressWiki": "true" // true|false
"aggressiveCompression": "true", // true|false
"compressWiki": "true", // true|false
"minKBReduced": 500 // delay new prs until size reduction meets this threshold (default to 10)
}
```

Expand Down
23 changes: 23 additions & 0 deletions Docs/min-kb-threshold.md
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
}
```
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# ImgBot
# Imgbot

ImgBot crawls all your image files in GitHub and submits pull requests after applying a lossless compression.
Imgbot crawls all your image files in GitHub and submits pull requests after applying a lossless compression.
This will make the file size go down, but leave the dimensions and quality just as good.

![screenshot](https://imgbot.net/images/screen.png?cache=2)

## Configuration

ImgBot supports optional configuration through a `.imgbotconfig` json file.
This is not a required step to using ImgBot and is only for more advanced scenarios.
Imgbot supports optional configuration through a `.imgbotconfig` json file.
This is not a required step to using Imgbot and is only for more advanced scenarios.
This file should be placed in the root of the repository and set to your liking.

```
Expand All @@ -20,7 +20,8 @@ This file should be placed in the root of the repository and set to your liking.
"public/special_images/*", // by folderpath
],
"aggressiveCompression": "true", // true|false
"compressWiki": "true" // true|false
"compressWiki": "true", // true|false
"minKBReduced": 500 // set reduction threshold (default to 10)
}
```

Expand All @@ -33,14 +34,14 @@ to help@imgbot.net

- optional
- Accepts: daily|weekly|monthly
- Limits the PRs from ImgBot to once a day, once a week, or once a month respectively
- The default behavior is to receive ImgBot PRs as images require optimization
- Limits the PRs from Imgbot to once a day, once a week, or once a month respectively
- The default behavior is to receive Imgbot PRs as images require optimization

**ignoredFiles**

- optional
- Accepts the syntax for searchPattern on [Directory.EnumerateFiles()](https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles)
- Limits the images optimized by ImgBot by esentially ignoring them
- Limits the images optimized by Imgbot by esentially ignoring them
- When ignoring by filename no path is necessary, when ignoring by foldername full path from root is necessary

**aggressiveCompression**
Expand All @@ -58,10 +59,18 @@ to help@imgbot.net
- Example: `https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.wiki.git`
- The default behavior is opt out


**minKBReduced**

- optional
- 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
- The default setting is 10

Find out more: https://imgbot.net/docs

## Contributing

All the code for ImgBot is available on GitHub. We will gladly accept contributions for the service, the website, and the documentation. This is where you can find out how to get set up to run locally as well as detailed information on exactly how ImgBot works.
All the code for Imgbot is available on GitHub. We will gladly accept contributions for the service, the website, and the documentation. This is where you can find out how to get set up to run locally as well as detailed information on exactly how Imgbot works.

https://imgbot.net/docs#contributing
92 changes: 92 additions & 0 deletions Test/ThresholdTests.cs
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);
}
}
}
4 changes: 4 additions & 0 deletions Web/src/docs/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"slug": "compress-wiki",
"title": "Compress wiki"
},
{
"slug": "min-kb-threshold",
"title": "Min KB threshold"
},
{
"slug": "authorization",
"title": "Authorization"
Expand Down

0 comments on commit a56b2db

Please sign in to comment.