Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
Return streams from exporters and write in core method (#72)
Browse files Browse the repository at this point in the history
* Release v1.0.0 (#41)

* Change formats type to `string` and split with comma separator

* Use root configuration instead of configuration section

* Fix exceptions not properly displaying in the console

* Add all module item types according to documentation

* Update application demo GIF

* Create code-analysis.yml

* Remove comments and enable additional queries

* Removed push from code-analysis.yml workflow

* Typo code-analysis.yml

* Added support for a Word export option

* Added footer Epsilon Credits

* Created helper class with reformat functions

* Reformat code

* Improved helper functions, Found that they can be added to records :-)

* Reformat files

* string interpolation

* Feature/cleanup (#40)

* Remove obsolete Epsilon.Http.Abstractions project

* Fix invalid serializable implementation

* Disable unused method return value hint

* Change exception to more appropriate one

* Update Grade score description

* Prevent null grades from exporting

* Reduce cognitive complexity to acceptable level

* Move logging call

* Reduce nesting

* Move project name and repository uri to constants

* Fix nullability warnings

* Remove unused class

* Use ?: operator and move constants to top level of class

* Reduce loop complexity

* Use project name constant in output name export option

* Update README.md application demo gif

* Add supported formats to README.md

Co-authored-by: Jelle Maas <typiqally@gmail.com>

* POC Open XML SDK

* Update

* Add KPI format and structure

* Added module names to document

* Testing the build and structure of exel files.

* Testing release deployment script.

* Delete file

* POC for excel export

* Code reformat

* Abstracted format logic from exporters to main logic

* Remove typo

* Remove unused variable

* Headers added

* Code cleanup

* Merge

* Removal nullability warnings

* Removal nullability warnings

* Removal tasks/awaits because they dont have nay use atm.

* Removal tasks/awaits because they dont have nay use atm.

* Add exporters return type

* Added memoryStream return type for ConsoleModuleExporter

* Remove unused ExportOptions from ConsoleModuleExporter

* Clean up old merge files

* 'Var' is everything

* Refactor and cleanup code

---------

Co-authored-by: Neal Geilen <info@nealgeilen.nl>
Co-authored-by: Jelle Maas <typiqally@gmail.com>
Co-authored-by: Sven Hansen <76601644+1SvenHansen@users.noreply.github.com>
  • Loading branch information
4 people committed May 26, 2023
1 parent 8c21885 commit 6de73b2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 51 deletions.
4 changes: 3 additions & 1 deletion Epsilon.Abstractions/Export/IExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public interface IExporter<in T>
{
public IEnumerable<string> Formats { get; }

void Export(T data, string format);
public string FileExtension { get; }

Task<Stream> Export(T data, string format);
}
10 changes: 7 additions & 3 deletions Epsilon.Cli/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private async Task ExecuteAsync()
_lifetime.StopApplication();
return;
}

var modules = _exportOptions.Modules?.Split(",");
_logger.LogInformation("Targeting Canvas course: {CourseId}, at {Url}", _canvasSettings.CourseId,
_canvasSettings.ApiUrl);
Expand All @@ -80,8 +80,12 @@ private async Task ExecuteAsync()
foreach (var (format, exporter) in exporters)
{
_logger.LogInformation("Exporting to {Format} using {Exporter}...", format, exporter.GetType().Name);
// ReSharper disable once PossibleMultipleEnumeration
exporter.Export(formattedItems, format);
var stream = await exporter.Export(formattedItems, format);

await using var fileStream = new FileStream($"{_exportOptions.FormattedOutputName}.{exporter.FileExtension}", FileMode.Create, FileAccess.Write);

stream.Position = 0; // Reset position to zero to prepare for copy
await stream.CopyToAsync(fileStream);
}
}
catch (Exception ex)
Expand Down
34 changes: 25 additions & 9 deletions Epsilon/Export/Exporters/ConsoleModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,43 @@ public ConsoleModuleExporter(ILogger<ConsoleModuleExporter> logger)
{
_logger = logger;
}

public IEnumerable<string> Formats { get; } = new[] { "console", "logs" };

public void Export(ExportData data, string format)
public IEnumerable<string> Formats { get; } = new[] { "console", "logs", "txt" };

public string FileExtension => "txt";


public async Task<Stream> Export(ExportData data, string format)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);

foreach (var module in data.CourseModules)
{
_logger.LogInformation("--------------------------------");
_logger.LogInformation("Module: {Module}", module.Name);
await WriteLineAndLog(writer, "--------------------------------");
await WriteLineAndLog(writer, $"Module: {module.Name}");

foreach (var kpi in module.Kpis)
{
_logger.LogInformation("");
_logger.LogInformation("KPI: {Kpi}", kpi.Name);
await WriteLineAndLog(writer, "");
await WriteLineAndLog(writer, $"KPI: {kpi.Name}");

foreach (var assignment in kpi.Assignments)
{
_logger.LogInformation("- Assignment: {Assignment}", assignment.Name);
_logger.LogInformation(" Score: {Score}", assignment.Score);
await WriteLineAndLog(writer, $"- Assignment: {assignment.Name}");
await WriteLineAndLog(writer, $" Score: {assignment.Score}");
}
}
}

await writer.FlushAsync();

return stream;
}

private async Task WriteLineAndLog(TextWriter writer, string line)
{
await writer.WriteLineAsync(line);
_logger.LogInformation(line);
}
}
25 changes: 11 additions & 14 deletions Epsilon/Export/Exporters/CsvModuleExporter.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System.Data;
using Epsilon.Abstractions.Export;
using Epsilon.Abstractions.Model;
using Microsoft.Extensions.Options;

namespace Epsilon.Export.Exporters;

public class CsvModuleExporter : ICanvasModuleExporter
{
private readonly ExportOptions _options;

public CsvModuleExporter(IOptions<ExportOptions> options)
{
_options = options.Value;
}

public IEnumerable<string> Formats { get; } = new[] { "csv" };

public void Export(ExportData data, string format)
public string FileExtension => "csv";

public async Task<Stream> Export(ExportData data, string format)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);

var dt = CreateDataTable(data.CourseModules);
WriteHeader(writer, dt);
WriteRows(writer, dt);

var stream = new StreamWriter($"{_options.FormattedOutputName}.{format}", false);
WriteHeader(stream, dt);
WriteRows(stream, dt);
await writer.FlushAsync();

stream.Close();
return stream;
}

private static DataTable CreateDataTable(IEnumerable<CourseModule> data)
{
var dataTable = new DataTable();

dataTable.Columns.Add("Module", typeof(string));
dataTable.Columns.Add("KPI", typeof(string));
dataTable.Columns.Add("Assignment", typeof(string));
Expand Down
21 changes: 7 additions & 14 deletions Epsilon/Export/Exporters/ExcelModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@
using DocumentFormat.OpenXml.Spreadsheet;
using Epsilon.Abstractions.Export;
using Epsilon.Abstractions.Model;
using Microsoft.Extensions.Options;

namespace Epsilon.Export.Exporters;

public class ExcelModuleExporter : ICanvasModuleExporter
{
private readonly ExportOptions _options;

public ExcelModuleExporter(IOptions<ExportOptions> options)
{
_options = options.Value;
}

public IEnumerable<string> Formats { get; } = new[] { "xls", "xlsx", "excel" };

public void Export(ExportData data, string format)
public string FileExtension => "xlsx";

public async Task<Stream> Export(ExportData data, string format)
{
using var spreadsheetDocument =
SpreadsheetDocument.Create($"{_options.FormattedOutputName}.xlsx", SpreadsheetDocumentType.Workbook);
var stream = new MemoryStream();
using var spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
var workbookPart = spreadsheetDocument.AddWorkbookPart();
workbookPart.Workbook = new Workbook();

// Add Sheets to the Workbook.
spreadsheetDocument.WorkbookPart!.Workbook.AppendChild(new Sheets());
spreadsheetDocument.WorkbookPart!.Workbook.AppendChild<Sheets>(new Sheets());

var cellValueBuilder = new StringBuilder();
var cellValueOutComeResultsBuilder = new StringBuilder();
Expand Down Expand Up @@ -75,8 +69,7 @@ public void Export(ExportData data, string format)
}
}

workbookPart.Workbook.Save();
spreadsheetDocument.Close();
return stream;
}

// Given a WorkbookPart, inserts a new worksheet.
Expand Down
17 changes: 7 additions & 10 deletions Epsilon/Export/Exporters/WordModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
using DocumentFormat.OpenXml.Wordprocessing;
using Epsilon.Abstractions.Export;
using Epsilon.Abstractions.Model;
using Microsoft.Extensions.Options;

namespace Epsilon.Export.Exporters;

public class WordModuleExporter : ICanvasModuleExporter
{
private readonly ExportOptions _options;

private static readonly TableBorders s_defaultBorders = new(
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
Expand All @@ -29,16 +26,14 @@ public class WordModuleExporter : ICanvasModuleExporter
CreateTextCell("Score")
);

public WordModuleExporter(IOptions<ExportOptions> options)
{
_options = options.Value;
}

public IEnumerable<string> Formats { get; } = new[] { "word", "docx" };

public void Export(ExportData data, string format)
public string FileExtension => "docx";

public async Task<Stream> Export(ExportData data, string format)
{
using var document = WordprocessingDocument.Create($"{_options.FormattedOutputName}.docx", WordprocessingDocumentType.Document);
var stream = new MemoryStream();
using var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);

document.AddMainDocumentPart();
document.MainDocumentPart!.Document = new Document(new Body());
Expand Down Expand Up @@ -83,6 +78,8 @@ public void Export(ExportData data, string format)

document.Save();
document.Close();

return stream;
}

private static Paragraph CreateText(string text) => new(new Run(new Text(text)));
Expand Down

0 comments on commit 6de73b2

Please sign in to comment.