Statistics Collection / Writing Overhaul (#35)

* Add DatStatistics class

* Add isDirectory setting

* Add CalculateStatistics method (nw)

* Add separate stats writing

* Use new methods

* Rename Write -> WriteIndividual

* Naive implementation of new writing (nw)

* Remove unncessary calls

* Make writing more DatFile-like

* Add console flag to constructor

* Remove unused stream constructors

* Move to local writers

* Remove inherent filename

* Fix invocation

* Use SeparatedValueWriter

* Fix final directory stats output

* Use XmlTextWriter for HTML

* Don't output separator on last stat output

* Remove now-completed TODOs

* Remove unused using
This commit is contained in:
Matt Nadareski
2021-02-18 11:13:11 -08:00
committed by GitHub
parent 10d8387883
commit 873431080d
9 changed files with 691 additions and 394 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Collections.Generic;
using SabreTools.DatFiles;
using SabreTools.Logging;
using SabreTools.Reports.Formats;
namespace SabreTools.Reports
@@ -9,110 +8,57 @@ namespace SabreTools.Reports
/// <summary>
/// Base class for a report output format
/// </summary>
/// TODO: Can this be overhauled to have all types write like DatFiles?
public abstract class BaseReport
{
protected string _name;
protected long _machineCount;
protected ItemDictionary _stats;
#region Logging
protected StreamWriter _writer;
protected bool _baddumpCol;
protected bool _nodumpCol;
/// <summary>
/// Logging object
/// </summary>
protected readonly Logger logger = new Logger();
#endregion
public List<DatStatistics> Statistics { get; set; }
/// <summary>
/// Create a new report from the filename
/// </summary>
/// <param name="filename">Name of the file to write out to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public BaseReport(string filename, bool baddumpCol = false, bool nodumpCol = false)
/// <param name="statsList">List of statistics objects to set</param>
public BaseReport(List<DatStatistics> statsList)
{
var fs = File.Create(filename);
if (fs != null)
_writer = new StreamWriter(fs) { AutoFlush = true };
_baddumpCol = baddumpCol;
_nodumpCol = nodumpCol;
}
/// <summary>
/// Create a new report from the stream
/// </summary>
/// <param name="stream">Output stream to write to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public BaseReport(Stream stream, bool baddumpCol = false, bool nodumpCol = false)
{
if (!stream.CanWrite)
throw new ArgumentException(nameof(stream));
_writer = new StreamWriter(stream) { AutoFlush = true };
_baddumpCol = baddumpCol;
_nodumpCol = nodumpCol;
Statistics = statsList;
}
/// <summary>
/// Create a specific type of BaseReport to be used based on a format and user inputs
/// </summary>
/// <param name="statReportFormat">Format of the Statistics Report to be created</param>
/// <param name="filename">Name of the file to write out to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
/// <param name="statsList">List of statistics objects to set</param>
/// <returns>BaseReport of the specific internal type that corresponds to the inputs</returns>
public static BaseReport Create(StatReportFormat statReportFormat, string filename, bool baddumpCol, bool nodumpCol)
public static BaseReport Create(StatReportFormat statReportFormat, List<DatStatistics> statsList)
{
return statReportFormat switch
{
StatReportFormat.None => new Textfile(Console.OpenStandardOutput(), baddumpCol, nodumpCol),
StatReportFormat.Textfile => new Textfile(filename, baddumpCol, nodumpCol),
StatReportFormat.CSV => new SeparatedValue(filename, ',', baddumpCol, nodumpCol),
StatReportFormat.HTML => new Html(filename, baddumpCol, nodumpCol),
StatReportFormat.SSV => new SeparatedValue(filename, ';', baddumpCol, nodumpCol),
StatReportFormat.TSV => new SeparatedValue(filename, '\t', baddumpCol, nodumpCol),
StatReportFormat.None => new Textfile(statsList, true),
StatReportFormat.Textfile => new Textfile(statsList, false),
StatReportFormat.CSV => new SeparatedValue(statsList, ','),
StatReportFormat.HTML => new Html(statsList),
StatReportFormat.SSV => new SeparatedValue(statsList, ';'),
StatReportFormat.TSV => new SeparatedValue(statsList, '\t'),
_ => null,
};
}
/// <summary>
/// Replace the statistics that is being output
/// Create and open an output file for writing direct from a set of statistics
/// </summary>
public void ReplaceStatistics(string datName, long machineCount, ItemDictionary datStats)
{
_name = datName;
_machineCount = machineCount;
_stats = datStats;
}
/// <summary>
/// Write the report to the output stream
/// </summary>
public abstract void Write();
/// <summary>
/// Write out the header to the stream, if any exists
/// </summary>
public abstract void WriteHeader();
/// <summary>
/// Write out the mid-header to the stream, if any exists
/// </summary>
public abstract void WriteMidHeader();
/// <summary>
/// Write out the separator to the stream, if any exists
/// </summary>
public abstract void WriteMidSeparator();
/// <summary>
/// Write out the footer-separator to the stream, if any exists
/// </summary>
public abstract void WriteFooterSeparator();
/// <summary>
/// Write out the footer to the stream, if any exists
/// </summary>
public abstract void WriteFooter();
/// <param name="outfile">Name of the file to write to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the report was written correctly, false otherwise</returns>
public abstract bool WriteToFile(string outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false);
/// <summary>
/// Returns the human-readable file size for an arbitrary, 64-bit file size