Refactor.

This commit is contained in:
2019-11-02 21:37:09 +00:00
parent f653af2059
commit d4020f1a3f
52 changed files with 4207 additions and 4095 deletions

View File

@@ -44,11 +44,7 @@ using DiscImageChef.Server.Models;
using Highsoft.Web.Mvc.Charts;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting.Internal;
using Filter = DiscImageChef.Server.Models.Filter;
using OperatingSystem = DiscImageChef.Server.Models.OperatingSystem;
using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID;
using Version = DiscImageChef.Server.Models.Version;
namespace DiscImageChef.Server.Controllers
{
@@ -57,13 +53,13 @@ namespace DiscImageChef.Server.Controllers
/// </summary>
public class StatsController : Controller
{
DicServerContext ctx;
List<DeviceItem> devices;
List<NameValueStats> operatingSystems;
List<MediaItem> realMedia;
List<NameValueStats> versions;
List<MediaItem> virtualMedia;
private IWebHostEnvironment _environment;
private readonly IWebHostEnvironment _environment;
private readonly DicServerContext ctx;
private List<DeviceItem> devices;
private List<NameValueStats> operatingSystems;
private List<MediaItem> realMedia;
private List<NameValueStats> versions;
private List<MediaItem> virtualMedia;
public StatsController(IWebHostEnvironment environment, DicServerContext context)
{
@@ -77,118 +73,120 @@ namespace DiscImageChef.Server.Controllers
try
{
if(
if (
System.IO.File
.Exists(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Statistics", "Statistics.xml")))
.Exists(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Statistics", "Statistics.xml")))
try
{
Stats statistics = new Stats();
var statistics = new Stats();
XmlSerializer xs = new XmlSerializer(statistics.GetType());
FileStream fs =
WaitForFile(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(), "Statistics", "Statistics.xml"),
FileMode.Open, FileAccess.Read, FileShare.Read);
statistics = (Stats)xs.Deserialize(fs);
var xs = new XmlSerializer(statistics.GetType());
var fs =
WaitForFile(
Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Statistics", "Statistics.xml"),
FileMode.Open, FileAccess.Read, FileShare.Read);
statistics = (Stats) xs.Deserialize(fs);
fs.Close();
StatsConverter.Convert(statistics);
System.IO.File
.Delete(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Statistics", "Statistics.xml"));
.Delete(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Statistics", "Statistics.xml"));
}
catch(XmlException)
catch (XmlException)
{
// Do nothing
}
if(ctx.OperatingSystems.Any())
if (ctx.OperatingSystems.Any())
{
operatingSystems = new List<NameValueStats>();
foreach(OperatingSystem nvs in ctx.OperatingSystems)
foreach (var nvs in ctx.OperatingSystems)
operatingSystems.Add(new NameValueStats
{
name =
$"{DetectOS.GetPlatformName((PlatformID)Enum.Parse(typeof(PlatformID), nvs.Name), nvs.Version)}{(string.IsNullOrEmpty(nvs.Version) ? "" : " ")}{nvs.Version}",
$"{DetectOS.GetPlatformName((PlatformID) Enum.Parse(typeof(PlatformID), nvs.Name), nvs.Version)}{(string.IsNullOrEmpty(nvs.Version) ? "" : " ")}{nvs.Version}",
Value = nvs.Count
});
ViewBag.repOperatingSystems = operatingSystems.OrderBy(os => os.name).ToList();
List<PieSeriesData> osPieData = new List<PieSeriesData>();
var osPieData = new List<PieSeriesData>();
decimal totalOsCount = ctx.OperatingSystems.Sum(o => o.Count);
foreach(string os in ctx.OperatingSystems.Select(o => o.Name).Distinct().ToList())
foreach (var os in ctx.OperatingSystems.Select(o => o.Name).Distinct().ToList())
{
decimal osCount = ctx.OperatingSystems.Where(o => o.Name == os).Sum(o => o.Count);
osPieData.Add(new PieSeriesData
{
Name =
DetectOS.GetPlatformName((PlatformID)Enum.Parse(typeof(PlatformID),
os)),
Y = (double?)(osCount / totalOsCount),
Sliced = os == "Linux",
DetectOS.GetPlatformName((PlatformID) Enum.Parse(typeof(PlatformID),
os)),
Y = (double?) (osCount / totalOsCount),
Sliced = os == "Linux",
Selected = os == "Linux"
});
}
ViewData["osPieData"] = osPieData;
List<PieSeriesData> linuxPieData = new List<PieSeriesData>();
var linuxPieData = new List<PieSeriesData>();
decimal linuxCount = ctx.OperatingSystems.Where(o => o.Name == PlatformID.Linux.ToString())
.Sum(o => o.Count);
foreach(OperatingSystem version in
.Sum(o => o.Count);
foreach (var version in
ctx.OperatingSystems.Where(o => o.Name == PlatformID.Linux.ToString()))
linuxPieData.Add(new PieSeriesData
{
Name =
$"{DetectOS.GetPlatformName(PlatformID.Linux, version.Version)}{(string.IsNullOrEmpty(version.Version) ? "" : " ")}{version.Version}",
Y = (double?)(version.Count / linuxCount)
Y = (double?) (version.Count / linuxCount)
});
ViewData["linuxPieData"] = linuxPieData;
List<PieSeriesData> macosPieData = new List<PieSeriesData>();
var macosPieData = new List<PieSeriesData>();
decimal macosCount = ctx.OperatingSystems.Where(o => o.Name == PlatformID.MacOSX.ToString())
.Sum(o => o.Count);
foreach(OperatingSystem version in
.Sum(o => o.Count);
foreach (var version in
ctx.OperatingSystems.Where(o => o.Name == PlatformID.MacOSX.ToString()))
macosPieData.Add(new PieSeriesData
{
Name =
$"{DetectOS.GetPlatformName(PlatformID.MacOSX, version.Version)}{(string.IsNullOrEmpty(version.Version) ? "" : " ")}{version.Version}",
Y = (double?)(version.Count / macosCount)
Y = (double?) (version.Count / macosCount)
});
ViewData["macosPieData"] = macosPieData;
List<PieSeriesData> windowsPieData = new List<PieSeriesData>();
var windowsPieData = new List<PieSeriesData>();
decimal windowsCount = ctx.OperatingSystems.Where(o => o.Name == PlatformID.Win32NT.ToString())
.Sum(o => o.Count);
foreach(OperatingSystem version in
.Sum(o => o.Count);
foreach (var version in
ctx.OperatingSystems.Where(o => o.Name == PlatformID.Win32NT.ToString()))
windowsPieData.Add(new PieSeriesData
{
Name =
$"{DetectOS.GetPlatformName(PlatformID.Win32NT, version.Version)}{(string.IsNullOrEmpty(version.Version) ? "" : " ")}{version.Version}",
Y = (double?)(version.Count / windowsCount)
Y = (double?) (version.Count / windowsCount)
});
ViewData["windowsPieData"] = windowsPieData;
}
if(ctx.Versions.Any())
if (ctx.Versions.Any())
{
versions = new List<NameValueStats>();
foreach(Version nvs in ctx.Versions)
foreach (var nvs in ctx.Versions)
versions.Add(new NameValueStats
{
name = nvs.Value == "previous" ? "Previous than 3.4.99.0" : nvs.Value,
name = nvs.Value == "previous" ? "Previous than 3.4.99.0" : nvs.Value,
Value = nvs.Count
});
@@ -202,210 +200,211 @@ namespace DiscImageChef.Server.Controllers
version.Value == "previous"
? "Previous than 3.4.99.0"
: version.Value,
Y = (double?)(version.Count /
totalVersionCount),
Sliced = version.Value == "previous",
Y = (double?) (version.Count /
totalVersionCount),
Sliced = version.Value == "previous",
Selected = version.Value == "previous"
}).ToList();
}
if(ctx.Commands.Any())
if (ctx.Commands.Any())
{
ViewBag.repCommands = ctx.Commands.OrderBy(c => c.Name).ToList();
decimal totalCommandCount = ctx.Commands.Sum(o => o.Count);
ViewData["commandsPieData"] = ctx
.Commands.Select(command => new PieSeriesData
{
Name = command.Name,
Y = (double?)(command.Count /
totalCommandCount),
Sliced = command.Name == "analyze",
Selected = command.Name == "analyze"
}).ToList();
.Commands.Select(command => new PieSeriesData
{
Name = command.Name,
Y = (double?) (command.Count /
totalCommandCount),
Sliced = command.Name == "analyze",
Selected = command.Name == "analyze"
}).ToList();
}
if(ctx.Filters.Any())
if (ctx.Filters.Any())
{
ViewBag.repFilters = ctx.Filters.OrderBy(filter => filter.Name).ToList();
List<PieSeriesData> filtersPieData = new List<PieSeriesData>();
var filtersPieData = new List<PieSeriesData>();
decimal totalFiltersCount = ctx.Filters.Sum(o => o.Count);
foreach(Filter filter in ctx.Filters.ToList())
foreach (var filter in ctx.Filters.ToList())
filtersPieData.Add(new PieSeriesData
{
Name = filter.Name,
Y = (double?)(filter.Count / totalFiltersCount),
Sliced = filter.Name == "No filter",
Name = filter.Name,
Y = (double?) (filter.Count / totalFiltersCount),
Sliced = filter.Name == "No filter",
Selected = filter.Name == "No filter"
});
ViewData["filtersPieData"] = filtersPieData;
}
if(ctx.MediaFormats.Any())
if (ctx.MediaFormats.Any())
{
ViewBag.repMediaImages = ctx.MediaFormats.OrderBy(filter => filter.Name).ToList();
List<PieSeriesData> formatsPieData = new List<PieSeriesData>();
var formatsPieData = new List<PieSeriesData>();
decimal totalFormatsCount = ctx.MediaFormats.Sum(o => o.Count);
decimal top10FormatCount = 0;
decimal top10FormatCount = 0;
foreach(MediaFormat format in ctx.MediaFormats.OrderByDescending(o => o.Count).Take(10))
foreach (var format in ctx.MediaFormats.OrderByDescending(o => o.Count).Take(10))
{
top10FormatCount += format.Count;
formatsPieData.Add(new PieSeriesData
{
Name = format.Name, Y = (double?)(format.Count / totalFormatsCount)
Name = format.Name, Y = (double?) (format.Count / totalFormatsCount)
});
}
formatsPieData.Add(new PieSeriesData
{
Name = "Other",
Y = (double?)((totalFormatsCount - top10FormatCount) /
totalFormatsCount),
Sliced = true,
Y = (double?) ((totalFormatsCount - top10FormatCount) /
totalFormatsCount),
Sliced = true,
Selected = true
});
ViewData["formatsPieData"] = formatsPieData;
}
if(ctx.Partitions.Any())
if (ctx.Partitions.Any())
{
ViewBag.repPartitions = ctx.Partitions.OrderBy(filter => filter.Name).ToList();
List<PieSeriesData> partitionsPieData = new List<PieSeriesData>();
var partitionsPieData = new List<PieSeriesData>();
decimal totalPartitionsCount = ctx.Partitions.Sum(o => o.Count);
decimal top10PartitionCount = 0;
decimal top10PartitionCount = 0;
foreach(Partition partition in ctx.Partitions.OrderByDescending(o => o.Count).Take(10))
foreach (var partition in ctx.Partitions.OrderByDescending(o => o.Count).Take(10))
{
top10PartitionCount += partition.Count;
partitionsPieData.Add(new PieSeriesData
{
Name = partition.Name,
Y = (double?)(partition.Count / totalPartitionsCount)
Y = (double?) (partition.Count / totalPartitionsCount)
});
}
partitionsPieData.Add(new PieSeriesData
{
Name = "Other",
Y = (double?)((totalPartitionsCount - top10PartitionCount) /
totalPartitionsCount),
Sliced = true,
Y = (double?) ((totalPartitionsCount - top10PartitionCount) /
totalPartitionsCount),
Sliced = true,
Selected = true
});
ViewData["partitionsPieData"] = partitionsPieData;
}
if(ctx.Filesystems.Any())
if (ctx.Filesystems.Any())
{
ViewBag.repFilesystems = ctx.Filesystems.OrderBy(filter => filter.Name).ToList();
List<PieSeriesData> filesystemsPieData = new List<PieSeriesData>();
var filesystemsPieData = new List<PieSeriesData>();
decimal totalFilesystemsCount = ctx.Filesystems.Sum(o => o.Count);
decimal top10FilesystemCount = 0;
decimal top10FilesystemCount = 0;
foreach(Filesystem filesystem in ctx.Filesystems.OrderByDescending(o => o.Count).Take(10))
foreach (var filesystem in ctx.Filesystems.OrderByDescending(o => o.Count).Take(10))
{
top10FilesystemCount += filesystem.Count;
filesystemsPieData.Add(new PieSeriesData
{
Name = filesystem.Name,
Y = (double?)(filesystem.Count / totalFilesystemsCount)
Y = (double?) (filesystem.Count / totalFilesystemsCount)
});
}
filesystemsPieData.Add(new PieSeriesData
{
Name = "Other",
Y = (double?)((totalFilesystemsCount - top10FilesystemCount) /
totalFilesystemsCount),
Sliced = true,
Y = (double?) ((totalFilesystemsCount - top10FilesystemCount) /
totalFilesystemsCount),
Sliced = true,
Selected = true
});
ViewData["filesystemsPieData"] = filesystemsPieData;
}
if(ctx.Medias.Any())
if (ctx.Medias.Any())
{
realMedia = new List<MediaItem>();
realMedia = new List<MediaItem>();
virtualMedia = new List<MediaItem>();
foreach(Media nvs in ctx.Medias)
foreach (var nvs in ctx.Medias)
try
{
MediaType
.MediaTypeToString((CommonTypes.MediaType)Enum.Parse(typeof(CommonTypes.MediaType), nvs.Type),
out string type, out string subtype);
.MediaTypeToString(
(CommonTypes.MediaType) Enum.Parse(typeof(CommonTypes.MediaType), nvs.Type),
out var type, out var subtype);
if(nvs.Real)
realMedia.Add(new MediaItem {Type = type, SubType = subtype, Count = nvs.Count});
if (nvs.Real)
realMedia.Add(new MediaItem {Type = type, SubType = subtype, Count = nvs.Count});
else virtualMedia.Add(new MediaItem {Type = type, SubType = subtype, Count = nvs.Count});
}
catch
{
if(nvs.Real)
realMedia.Add(new MediaItem {Type = nvs.Type, SubType = null, Count = nvs.Count});
if (nvs.Real)
realMedia.Add(new MediaItem {Type = nvs.Type, SubType = null, Count = nvs.Count});
else virtualMedia.Add(new MediaItem {Type = nvs.Type, SubType = null, Count = nvs.Count});
}
if(realMedia.Count > 0)
if (realMedia.Count > 0)
{
ViewBag.repRealMedia =
realMedia.OrderBy(media => media.Type).ThenBy(media => media.SubType).ToList();
List<PieSeriesData> realMediaPieData = new List<PieSeriesData>();
var realMediaPieData = new List<PieSeriesData>();
decimal totalRealMediaCount = realMedia.Sum(o => o.Count);
decimal top10RealMediaCount = 0;
foreach(MediaItem realMediaItem in realMedia.OrderByDescending(o => o.Count).Take(10))
foreach (var realMediaItem in realMedia.OrderByDescending(o => o.Count).Take(10))
{
top10RealMediaCount += realMediaItem.Count;
realMediaPieData.Add(new PieSeriesData
{
Name = $"{realMediaItem.Type} ({realMediaItem.SubType})",
Y = (double?)(realMediaItem.Count / totalRealMediaCount)
Y = (double?) (realMediaItem.Count / totalRealMediaCount)
});
}
realMediaPieData.Add(new PieSeriesData
{
Name = "Other",
Y = (double?)((totalRealMediaCount - top10RealMediaCount) /
totalRealMediaCount),
Sliced = true,
Y = (double?) ((totalRealMediaCount - top10RealMediaCount) /
totalRealMediaCount),
Sliced = true,
Selected = true
});
ViewData["realMediaPieData"] = realMediaPieData;
}
if(virtualMedia.Count > 0)
if (virtualMedia.Count > 0)
{
ViewBag.repVirtualMedia =
virtualMedia.OrderBy(media => media.Type).ThenBy(media => media.SubType).ToList();
List<PieSeriesData> virtualMediaPieData = new List<PieSeriesData>();
var virtualMediaPieData = new List<PieSeriesData>();
decimal totalVirtualMediaCount = virtualMedia.Sum(o => o.Count);
decimal top10VirtualMediaCount = 0;
foreach(MediaItem virtualMediaItem in virtualMedia.OrderByDescending(o => o.Count).Take(10))
foreach (var virtualMediaItem in virtualMedia.OrderByDescending(o => o.Count).Take(10))
{
top10VirtualMediaCount += virtualMediaItem.Count;
@@ -413,8 +412,8 @@ namespace DiscImageChef.Server.Controllers
{
Name =
$"{virtualMediaItem.Type} ({virtualMediaItem.SubType})",
Y = (double?)(virtualMediaItem.Count /
totalVirtualMediaCount)
Y = (double?) (virtualMediaItem.Count /
totalVirtualMediaCount)
});
}
@@ -424,7 +423,7 @@ namespace DiscImageChef.Server.Controllers
Y = (double?)
((totalVirtualMediaCount - top10VirtualMediaCount) /
totalVirtualMediaCount),
Sliced = true,
Sliced = true,
Selected = true
});
@@ -432,93 +431,100 @@ namespace DiscImageChef.Server.Controllers
}
}
if(ctx.DeviceStats.Any())
if (ctx.DeviceStats.Any())
{
devices = new List<DeviceItem>();
foreach(DeviceStat device in ctx.DeviceStats.ToList())
foreach (var device in ctx.DeviceStats.ToList())
{
string xmlFile;
if(!string.IsNullOrWhiteSpace(device.Manufacturer) &&
!string.IsNullOrWhiteSpace(device.Model) &&
!string.IsNullOrWhiteSpace(device.Revision))
if (!string.IsNullOrWhiteSpace(device.Manufacturer) &&
!string.IsNullOrWhiteSpace(device.Model) &&
!string.IsNullOrWhiteSpace(device.Revision))
xmlFile = device.Manufacturer + "_" + device.Model + "_" + device.Revision + ".xml";
else if(!string.IsNullOrWhiteSpace(device.Manufacturer) &&
!string.IsNullOrWhiteSpace(device.Model))
else if (!string.IsNullOrWhiteSpace(device.Manufacturer) &&
!string.IsNullOrWhiteSpace(device.Model))
xmlFile = device.Manufacturer + "_" + device.Model + ".xml";
else if(!string.IsNullOrWhiteSpace(device.Model) && !string.IsNullOrWhiteSpace(device.Revision))
xmlFile = device.Model + "_" + device.Revision + ".xml";
else xmlFile = device.Model + ".xml";
else if (!string.IsNullOrWhiteSpace(device.Model) &&
!string.IsNullOrWhiteSpace(device.Revision))
xmlFile = device.Model + "_" + device.Revision + ".xml";
else xmlFile = device.Model + ".xml";
xmlFile = xmlFile.Replace('/', '_').Replace('\\', '_').Replace('?', '_');
if(System.IO.File.Exists(Path.Combine(_environment.ContentRootPath, "Reports", xmlFile)))
if (System.IO.File.Exists(Path.Combine(_environment.ContentRootPath, "Reports", xmlFile)))
{
DeviceReport deviceReport = new DeviceReport();
var deviceReport = new DeviceReport();
XmlSerializer xs = new XmlSerializer(deviceReport.GetType());
FileStream fs =
WaitForFile(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(), "Reports", xmlFile),
FileMode.Open, FileAccess.Read, FileShare.Read);
deviceReport = (DeviceReport)xs.Deserialize(fs);
var xs = new XmlSerializer(deviceReport.GetType());
var fs =
WaitForFile(
Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Reports", xmlFile),
FileMode.Open, FileAccess.Read, FileShare.Read);
deviceReport = (DeviceReport) xs.Deserialize(fs);
fs.Close();
DeviceReportV2 deviceReportV2 = new DeviceReportV2(deviceReport);
var deviceReportV2 = new DeviceReportV2(deviceReport);
device.Report = ctx.Devices.Add(new Device(deviceReportV2)).Entity;
ctx.SaveChanges();
System.IO.File
.Delete(Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Reports", xmlFile));
.Delete(Path.Combine(
_environment.ContentRootPath ?? throw new InvalidOperationException(),
"Reports", xmlFile));
}
devices.Add(new DeviceItem
{
Manufacturer = device.Manufacturer,
Model = device.Model,
Revision = device.Revision,
Bus = device.Bus,
Model = device.Model,
Revision = device.Revision,
Bus = device.Bus,
ReportId = device.Report != null && device.Report.Id != 0
? device.Report.Id
: 0
? device.Report.Id
: 0
});
}
ViewBag.repDevices = devices.OrderBy(device => device.Manufacturer).ThenBy(device => device.Model)
.ThenBy(device => device.Revision).ThenBy(device => device.Bus)
.ToList();
.ThenBy(device => device.Revision).ThenBy(device => device.Bus)
.ToList();
ViewData["devicesBusPieData"] = (from deviceBus in devices.Select(d => d.Bus).Distinct()
let deviceBusCount = devices.Count(d => d.Bus == deviceBus)
select new PieSeriesData
{
Name = deviceBus,
Y = deviceBusCount / (double)devices.Count
}).ToList();
let deviceBusCount = devices.Count(d => d.Bus == deviceBus)
select new PieSeriesData
{
Name = deviceBus,
Y = deviceBusCount / (double) devices.Count
}).ToList();
ViewData["devicesManufacturerPieData"] =
(from manufacturer in
devices.Where(d => d.Manufacturer != null).Select(d => d.Manufacturer.ToLowerInvariant())
devices.Where(d => d.Manufacturer != null)
.Select(d => d.Manufacturer.ToLowerInvariant())
.Distinct()
let manufacturerCount = devices.Count(d => d.Manufacturer?.ToLowerInvariant() == manufacturer)
select new PieSeriesData {Name = manufacturer, Y = manufacturerCount / (double)devices.Count})
.ToList();
let manufacturerCount =
devices.Count(d => d.Manufacturer?.ToLowerInvariant() == manufacturer)
select new PieSeriesData
{Name = manufacturer, Y = manufacturerCount / (double) devices.Count})
.ToList();
}
}
catch(Exception)
catch (Exception)
{
#if DEBUG
#if DEBUG
throw;
#endif
#endif
return Content("Could not read statistics");
}
return View();
}
static FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
private static FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
{
for(int numTries = 0; numTries < 100; numTries++)
for (var numTries = 0; numTries < 100; numTries++)
{
FileStream fs = null;
try
@@ -526,7 +532,7 @@ namespace DiscImageChef.Server.Controllers
fs = new FileStream(fullPath, mode, access, share);
return fs;
}
catch(IOException)
catch (IOException)
{
fs?.Dispose();
Thread.Sleep(50);