Files
Aaru.Server/DiscImageChef.Server/App_Start/StatsConverter.cs

368 lines
16 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : StatsConverter.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Reads a statistics XML and stores it in the database context.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System.Linq;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Server.Models;
using Version = DiscImageChef.Server.Models.Version;
namespace DiscImageChef.Server
{
public static class StatsConverter
{
public static void Convert(Stats newStats)
{
2019-11-02 21:37:09 +00:00
var ctx = new DicServerContext();
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (newStats.Commands != null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Analyze > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "analyze");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Analyze, Name = "analyze"});
else existing.Count += newStats.Commands.Analyze;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Benchmark > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "benchmark");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Benchmark, Name = "benchmark"});
else existing.Count += newStats.Commands.Benchmark;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Checksum > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "checksum");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Checksum, Name = "checksum"});
else existing.Count += newStats.Commands.Checksum;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Compare > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "compare");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Compare, Name = "compare"});
else existing.Count += newStats.Commands.Compare;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.CreateSidecar > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "create-sidecar");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command
{
Count = newStats.Commands.CreateSidecar, Name = "create-sidecar"
});
else existing.Count += newStats.Commands.CreateSidecar;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Decode > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "decode");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Decode, Name = "decode"});
else existing.Count += newStats.Commands.Decode;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.DeviceInfo > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "device-info");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.DeviceInfo, Name = "device-info"});
else existing.Count += newStats.Commands.DeviceInfo;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.DeviceReport > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "device-report");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.DeviceReport, Name = "device-report"});
else existing.Count += newStats.Commands.DeviceReport;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.DumpMedia > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "dump-media");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.DumpMedia, Name = "dump-media"});
else existing.Count += newStats.Commands.DumpMedia;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Entropy > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "entropy");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Entropy, Name = "entropy"});
else existing.Count += newStats.Commands.Entropy;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Formats > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "formats");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Formats, Name = "formats"});
else existing.Count += newStats.Commands.Formats;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.MediaInfo > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "media-info");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.MediaInfo, Name = "media-info"});
else existing.Count += newStats.Commands.MediaInfo;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.MediaScan > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "media-scan");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.MediaScan, Name = "media-scan"});
else existing.Count += newStats.Commands.MediaScan;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.PrintHex > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "printhex");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.PrintHex, Name = "printhex"});
else existing.Count += newStats.Commands.PrintHex;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Verify > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "verify");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.Verify, Name = "verify"});
else existing.Count += newStats.Commands.Verify;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.Ls > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "ls");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.Commands.Add(new Command {Count = newStats.Commands.Ls, Name = "ls"});
2019-11-02 01:40:41 +00:00
else existing.Count += newStats.Commands.Ls;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.ExtractFiles > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "extract-files");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.ExtractFiles, Name = "extract-files"});
else existing.Count += newStats.Commands.ExtractFiles;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.ListDevices > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "list-devices");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.ListDevices, Name = "list-devices"});
else existing.Count += newStats.Commands.ListDevices;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.ListEncodings > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "list-encodings");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command
{
Count = newStats.Commands.ListEncodings, Name = "list-encodings"
});
else existing.Count += newStats.Commands.ListEncodings;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.ConvertImage > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "convert-image");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.ConvertImage, Name = "convert-image"});
else existing.Count += newStats.Commands.ConvertImage;
}
2019-11-02 21:37:09 +00:00
if (newStats.Commands.ImageInfo > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Commands.FirstOrDefault(c => c.Name == "image-info");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Commands.Add(new Command {Count = newStats.Commands.ImageInfo, Name = "image-info"});
else existing.Count += newStats.Commands.ImageInfo;
}
}
2019-11-02 21:37:09 +00:00
if (newStats.OperatingSystems != null)
{
foreach (var operatingSystem in newStats.OperatingSystems)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(operatingSystem.name) ||
string.IsNullOrWhiteSpace(operatingSystem.version)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing =
ctx.OperatingSystems.FirstOrDefault(c => c.Name == operatingSystem.name &&
2019-11-02 01:40:41 +00:00
c.Version == operatingSystem.version);
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.OperatingSystems.Add(new OperatingSystem
{
2019-11-02 21:37:09 +00:00
Count = operatingSystem.Value,
Name = operatingSystem.name,
2019-11-02 01:40:41 +00:00
Version = operatingSystem.version
});
else existing.Count += operatingSystem.Value;
}
2019-11-02 21:37:09 +00:00
}
2019-11-02 01:40:41 +00:00
else
{
2019-11-02 21:37:09 +00:00
var existing =
2019-11-02 01:40:41 +00:00
ctx.OperatingSystems.FirstOrDefault(c => c.Name == "Linux" && c.Version == null);
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.OperatingSystems.Add(new OperatingSystem {Count = 1, Name = "Linux"});
2019-11-02 01:40:41 +00:00
else existing.Count++;
}
2019-11-02 21:37:09 +00:00
if (newStats.Versions != null)
{
foreach (var nvs in newStats.Versions)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(nvs.name)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing = ctx.Versions.FirstOrDefault(c => c.Value == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.Versions.Add(new Version {Count = nvs.Value, Value = nvs.name});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
2019-11-02 21:37:09 +00:00
}
2019-11-02 01:40:41 +00:00
else
{
2019-11-02 21:37:09 +00:00
var existing = ctx.Versions.FirstOrDefault(c => c.Value == "previous");
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.Versions.Add(new Version {Count = 1, Value = "previous"});
2019-11-02 01:40:41 +00:00
else existing.Count++;
}
2019-11-02 21:37:09 +00:00
if (newStats.Filesystems != null)
foreach (var nvs in newStats.Filesystems)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(nvs.name)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing = ctx.Filesystems.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.Filesystems.Add(new Filesystem {Count = nvs.Value, Name = nvs.name});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
2019-11-02 21:37:09 +00:00
if (newStats.Partitions != null)
foreach (var nvs in newStats.Partitions)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(nvs.name)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing = ctx.Partitions.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.Partitions.Add(new Partition {Count = nvs.Value, Name = nvs.name});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
2019-11-02 21:37:09 +00:00
if (newStats.MediaImages != null)
foreach (var nvs in newStats.MediaImages)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(nvs.name)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing = ctx.MediaFormats.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.MediaFormats.Add(new MediaFormat {Count = nvs.Value, Name = nvs.name});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
2019-11-02 21:37:09 +00:00
if (newStats.Filters != null)
foreach (var nvs in newStats.Filters)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(nvs.name)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing = ctx.Filters.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null) ctx.Filters.Add(new Filter {Count = nvs.Value, Name = nvs.name});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
2019-11-02 21:37:09 +00:00
if (newStats.Devices != null)
foreach (var device in newStats.Devices)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(device.Model)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (!ctx.DeviceStats.Any(c => c.Bus == device.Bus && c.Manufacturer == device.Manufacturer &&
c.Model == device.Model && c.Revision == device.Revision))
2019-11-02 01:40:41 +00:00
ctx.DeviceStats.Add(new DeviceStat
{
2019-11-02 21:37:09 +00:00
Bus = device.Bus,
2019-11-02 01:40:41 +00:00
Manufacturer = device.Manufacturer,
2019-11-02 21:37:09 +00:00
Model = device.Model,
Revision = device.Revision
2019-11-02 01:40:41 +00:00
});
}
2019-11-02 21:37:09 +00:00
if (newStats.Medias != null)
foreach (var media in newStats.Medias)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (string.IsNullOrWhiteSpace(media.type)) continue;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var existing = ctx.Medias.FirstOrDefault(c => c.Type == media.type && c.Real == media.real);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 01:40:41 +00:00
ctx.Medias.Add(new Media {Count = media.Value, Real = media.real, Type = media.type});
else existing.Count += media.Value;
}
ctx.SaveChanges();
}
}
}