Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Progress.Samples.Bar.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Progress;
using Progress.Descriptors;
using Progress.Samples;
using Progress.Settings;

var onProgress = (Stats stats) =>
{
Expand All @@ -21,6 +22,7 @@
.DisplayingItemsOverview()
.NotifyingProgress(onProgress)
.NotifyingCompletion(onCompletion)
.ExportingTo("output.json", FileType.Json)
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
.UsingComponentDescriptor(BarDescriptor.Default)
.Build(Worker.AllItems);
Expand Down
2 changes: 2 additions & 0 deletions src/Progress.Samples.HearthBeat.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Progress;
using Progress.Descriptors;
using Progress.Samples;
using Progress.Settings;

var onProgress = (Stats stats) =>
{
Expand All @@ -21,6 +22,7 @@
.DisplayingItemsOverview()
.NotifyingProgress(onProgress)
.NotifyingCompletion(onCompletion)
.ExportingTo("output.csv", FileType.Csv)
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
.UsingComponentDescriptor(HearthBeatDescriptor.Default)
.Build(Worker.AllItems);
Expand Down
2 changes: 2 additions & 0 deletions src/Progress.Samples.Pulse.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Progress;
using Progress.Descriptors;
using Progress.Samples;
using Progress.Settings;

var onProgress = (Stats stats) =>
{
Expand All @@ -22,6 +23,7 @@
.DisplayingItemsOverview()
.NotifyingProgress(onProgress)
.NotifyingCompletion(onCompletion)
.ExportingTo("output.txt", FileType.Text)
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
.UsingComponentDescriptor(PulseDescriptor.Default)
.Build(Worker.AllItems);
Expand Down
1 change: 1 addition & 0 deletions src/Progress.Samples.Spinner.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
.DisplayingItemsOverview()
.NotifyingProgress(onProgress)
.NotifyingCompletion(onCompletion)
.ExportingTo("output.xml", Progress.Settings.FileType.Xml)
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
.UsingComponentDescriptor(SpinnerDescriptor.Default)
.Build(Worker.AllItems);
Expand Down
38 changes: 38 additions & 0 deletions src/Progress.UnitTest/ExporterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Progress.Settings;

namespace Progress.UnitTest;

public class ExporterTests
{
private readonly Stats _stats = new()
{
StartedOn = DateTimeOffset.Now.AddMinutes(-1),
CurrentCount = 1000,
CurrentPercent = 100,
ElapsedTime = TimeSpan.FromMinutes(1),
EstTimeOfArrival = DateTimeOffset.UtcNow,
ExpectedItems = 1000,
FailureCount = 30,
SuccessCount = 970,
RemainingTime = TimeSpan.Zero
};


[Theory]
[InlineData("output.csv", FileType.Csv)]
[InlineData("output.json", FileType.Json)]
[InlineData("output.txt", FileType.Text)]
[InlineData("output.xml", FileType.Xml)]
public void GivenStats_WhenExporting_ThenExportsSuccessfully(string fileName, FileType fileType)
{
// Arrange
ExportSettings settings = new(fileName, fileType);
Exporter exporter = new(settings);

// Act
exporter.Export(_stats);

// Assert
File.Exists(fileName).Should().BeTrue();
}
}
20 changes: 20 additions & 0 deletions src/Progress.UnitTest/PrinterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Progress.Descriptors;

namespace Progress.UnitTest;

public class PrinterTests
{
[Fact]
public void GivenDefaults_WhenPrinting_ThenGetsOutput()
{
// Arrange
Printer printer = new(new Settings.ReportingOptions(), BarDescriptor.Default.Build());
Stats stats = new();

// Act
var actual = printer.Print(stats);

// Assert
actual.Should().NotBeNullOrEmpty();
}
}
38 changes: 30 additions & 8 deletions src/Progress.UnitTest/ReporterBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Progress.UnitTest;
using Progress.Settings;

namespace Progress.UnitTest;

public class ReporterBuilderTests
{
Expand Down Expand Up @@ -37,7 +39,7 @@ public void GivenReportingFrequency_WhenBuilding_ThenReporterIsSetUp()
.UsingReportingFrequency(expectedFrequency);

// Act
var actual = builder.Build(100);
var actual = builder.Build(100).Configuration;

// Assert
actual.ReportFrequency.Should().Be(expectedFrequency);
Expand All @@ -51,7 +53,7 @@ public void GivenElapsedTime_WhenBuilding_ThenReporterIsSetUp()
.DisplayingElapsedTime();

// Act
var actual = builder.Build(100);
var actual = builder.Build(100).Configuration.Options;

// Assert
actual.DisplayElapsedTime.Should().BeTrue();
Expand All @@ -65,7 +67,7 @@ public void GivenStartingTime_WhenBuilding_ThenReporterIsSetUp()
.DisplayingStartingTime();

// Act
var actual = builder.Build(100);
var actual = builder.Build(100).Configuration.Options;

// Assert
actual.DisplayStartingTime.Should().BeTrue();
Expand All @@ -79,7 +81,7 @@ public void GivenItemsOverview_WhenBuilding_ThenReporterIsSetUp()
.DisplayingItemsOverview();

// Act
var actual = builder.Build(100);
var actual = builder.Build(100).Configuration.Options;

// Assert
actual.DisplayItemsOverview.Should().BeTrue();
Expand All @@ -93,7 +95,7 @@ public void GivenSummary_WhenBuilding_ThenReporterIsSetUp()
.DisplayingItemsSummary();

// Act
var actual = builder.Build(100);
var actual = builder.Build(100).Configuration.Options;

// Assert
actual.DisplayItemsSummary.Should().BeTrue();
Expand All @@ -113,7 +115,7 @@ public void GivenProgressNotifications_WhenBuilding_ThenReporterIsSetUp()
// Arrange
actual.OnProgress.Should().NotBeNull();
actual.OnProgress.Should().Be(callback);
actual.StatsFrequency.Should().Be(TimeSpan.FromSeconds(5));
actual.Configuration.StatsFrequency.Should().Be(TimeSpan.FromSeconds(5));
}

[Fact]
Expand All @@ -131,7 +133,8 @@ public void GivenProgressNotificationsWithFrequency_WhenBuilding_ThenReporterIsS
// Arrange
actual.OnProgress.Should().NotBeNull();
actual.OnProgress.Should().Be(callback);
actual.StatsFrequency.Should().Be(frequency);
actual.Configuration.StatsFrequency.Should().Be(frequency);
actual.Configuration.Options.NotifyProgressStats.Should().BeTrue();
}

[Fact]
Expand All @@ -148,6 +151,25 @@ public void GivenCompletionNotifications_WhenBuilding_ThenReporterIsSetUp()
// Arrange
actual.OnCompletion.Should().NotBeNull();
actual.OnCompletion.Should().Be(callback);
actual.Configuration.Options.NotifyCompletionStats.Should().BeTrue();
}

[Fact]
public void GivenExporting_WhenBuilding_ThenReporterIsSetUp()
{
// Arrange
const string fileName = "output.txt";
const FileType type = FileType.Text;
var builder = new ReporterBuilder()
.ExportingTo(fileName, type);

// Act
var actual = builder.Build(100);

// Assert
actual.Configuration.ExportSettings.Should().NotBeNull();
actual.Configuration.ExportSettings.FileName.Should().Be(fileName);
actual.Configuration.ExportSettings.FileType.Should().Be(FileType.Text);
actual.Configuration.Options.ExportCompletionStats.Should().BeTrue();
}
}
32 changes: 18 additions & 14 deletions src/Progress.UnitTest/ReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ public void GivenDefaults_WhenInitializing_ThenExpectedSettings()
var reporter = new Reporter(100, BarDescriptor.Default.Build());

// Assert
reporter.DisplayStartingTime.Should().BeTrue();
reporter.DisplayRemainingTime.Should().BeTrue();
reporter.DisplayElapsedTime.Should().BeTrue();
reporter.DisplayEstimatedTimeOfArrival.Should().BeTrue();
reporter.DisplayItemsOverview.Should().BeTrue();
reporter.DisplayItemsSummary.Should().BeTrue();
reporter.NotifyProgressStats.Should().BeTrue();
reporter.NotifyCompletionStats.Should().BeTrue();
reporter.ReportFrequency.Should().Be(TimeSpan.FromSeconds(1));
reporter.StatsFrequency.Should().Be(TimeSpan.FromSeconds(5));
reporter.Configuration.Options.DisplayStartingTime.Should().BeTrue();
reporter.Configuration.Options.DisplayRemainingTime.Should().BeTrue();
reporter.Configuration.Options.DisplayElapsedTime.Should().BeTrue();
reporter.Configuration.Options.DisplayEstimatedTimeOfArrival.Should().BeTrue();
reporter.Configuration.Options.DisplayItemsOverview.Should().BeTrue();
reporter.Configuration.Options.DisplayItemsSummary.Should().BeTrue();
reporter.Configuration.Options.NotifyProgressStats.Should().BeFalse();
reporter.Configuration.Options.NotifyCompletionStats.Should().BeFalse();
reporter.Configuration.Options.ExportCompletionStats.Should().BeFalse();
reporter.Configuration.ReportFrequency.Should().Be(TimeSpan.FromSeconds(1));
reporter.Configuration.StatsFrequency.Should().Be(TimeSpan.FromSeconds(5));
reporter.Configuration.ExportSettings.Should().BeNull();
reporter.OnProgress.Should().BeNull();
reporter.OnCompletion.Should().BeNull();
}
Expand Down Expand Up @@ -123,10 +125,11 @@ public async Task GivenProgressNotifications_WhenRunning_ThenCallbackIsCalled()
bool isCalled = false;
var reporter = new Reporter(100, BarDescriptor.Default.Build())
{
NotifyProgressStats = true,
StatsFrequency = TimeSpan.FromSeconds(1),
OnProgress = (stats) => isCalled = true
};

reporter.Configuration.Options.NotifyProgressStats = true;
reporter.Configuration.StatsFrequency = TimeSpan.FromSeconds(1);


// Act
Expand All @@ -144,10 +147,11 @@ public async Task GivenCompletionNotifications_WhenFinished_ThenCallbackIsCalled
bool isCalled = false;
var reporter = new Reporter(1, BarDescriptor.Default.Build())
{
NotifyCompletionStats = true,
ReportFrequency = TimeSpan.FromMicroseconds(500),
OnCompletion = (stats) => isCalled = true
};

reporter.Configuration.Options.NotifyCompletionStats = true;
reporter.Configuration.ReportFrequency = TimeSpan.FromMicroseconds(500);

reporter.Start();

Expand Down
4 changes: 3 additions & 1 deletion src/Progress/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ internal abstract class Component
{
private static readonly CultureInfo Culture = new("en-US");

public Percent CurrentPercent { get; protected set; } = default!;
public Percent CurrentPercent { get; protected set; } = Percent.Zero;

public abstract Component Next(ulong availableItems, ulong currentCount);

protected static Percent Calculate(ulong availableItems, ulong currentCount) => new(availableItems, currentCount);

internal class Percent
{
public static Percent Zero => new(0, 0);

private readonly double _percent;

public Percent(ulong items, ulong count)
Expand Down
26 changes: 26 additions & 0 deletions src/Progress/Exporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Progress.Exporters;
using Progress.Settings;
using System.Text;

namespace Progress;

/// <summary>
/// Exports the operation result in different formats
/// </summary>
internal class Exporter(ExportSettings settings)
{
private readonly ExportSettings _settings = settings;
private readonly IEnumerable<IContentExporter> _exporters = [ new CsvExporter(), new TextExporter(), new JsonExporter(), new XmlExporter() ];

public void Export(Stats stats)
{
using FileStream stream = File.Exists(_settings.FileName)
? File.OpenWrite(_settings.FileName)
: File.Create(_settings.FileName);

IContentExporter contentExporter = _exporters.FirstOrDefault(e => e.FileType == _settings.FileType) ?? throw new NotSupportedException("Not supported export type");
string content = contentExporter.Export(stats);
byte[] result = Encoding.Default.GetBytes(content);
stream.Write(result, 0, result.Length);
}
}
30 changes: 30 additions & 0 deletions src/Progress/Exporters/CsvExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Progress.Settings;
using System.Text;

namespace Progress.Exporters
{
internal class CsvExporter : IContentExporter
{
private const char Separator = ';';

public FileType FileType => FileType.Csv;

public string Export(Stats stats)
{
var type = stats.GetType();
string[] properyNames = type.GetProperties().Select(p => p.Name).Order().ToArray();

StringBuilder sBuilder = new();
sBuilder.AppendLine(string.Join(Separator, properyNames));

foreach(string properyName in properyNames)
{
object value = type.GetProperty(properyName)!.GetValue(stats)!;
sBuilder.Append(value.ToString());
sBuilder.Append(Separator);
}

return sBuilder.ToString();
}
}
}
10 changes: 10 additions & 0 deletions src/Progress/Exporters/IContentExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Progress.Settings;

namespace Progress.Exporters
{
internal interface IContentExporter
{
FileType FileType { get; }
string Export(Stats stats);
}
}
12 changes: 12 additions & 0 deletions src/Progress/Exporters/JsonExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Progress.Settings;
using System.Text.Json;

namespace Progress.Exporters
{
internal class JsonExporter : IContentExporter
{
public FileType FileType => FileType.Json;

public string Export(Stats stats) => JsonSerializer.Serialize(stats);
}
}
Loading