Files
Aaru.Server/DiscImageChef.Server/Statistics.aspx.cs

306 lines
14 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Statistics.aspx.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Renders statistics and links to reports.
//
// --[ 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-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
2017-12-19 19:33:46 +00:00
using System.IO;
using System.Linq;
2017-12-19 19:33:46 +00:00
using System.Reflection;
2017-12-21 14:30:38 +00:00
using System.Threading;
2017-12-19 19:33:46 +00:00
using System.Web;
2017-12-21 14:30:38 +00:00
using System.Web.Hosting;
using System.Web.UI;
2017-12-19 19:33:46 +00:00
using System.Xml.Serialization;
2017-12-21 14:30:38 +00:00
using DiscImageChef.Interop;
2017-12-19 19:33:46 +00:00
using DiscImageChef.Metadata;
2017-12-21 14:30:38 +00:00
using PlatformID = DiscImageChef.Interop.PlatformID;
2017-06-03 01:10:46 +01:00
namespace DiscImageChef.Server
{
/// <summary>
/// Renders a page with statistics, list of media type, devices, etc
/// </summary>
2017-12-21 14:30:38 +00:00
public partial class Statistics : Page
2017-06-03 01:10:46 +01:00
{
class MediaItem
{
public string Type { get; set; }
public string SubType { get; set; }
public long Count { get; set; }
}
2017-06-03 01:10:46 +01:00
class DeviceItem
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public string Revision { get; set; }
public string Bus { get; set; }
public string ReportLink { get; set; }
}
Stats statistics;
List<MediaItem> realMedia;
List<MediaItem> virtualMedia;
List<NameValueStats> operatingSystems;
List<DeviceItem> devices;
2017-06-04 02:24:04 +01:00
List<NameValueStats> versions;
protected void Page_Load(object sender, EventArgs e)
{
lblVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
try
{
if(!File.Exists(Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(), "Statistics",
2017-12-19 20:33:03 +00:00
"Statistics.xml")))
{
#if DEBUG
content.InnerHtml =
$"<b>Sorry, cannot load data file \"{Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(), "Statistics", "Statistics.xml")}\"</b>";
#else
content.InnerHtml = "<b>Sorry, cannot load data file</b>";
#endif
return;
}
statistics = new Stats();
XmlSerializer xs = new XmlSerializer(statistics.GetType());
2017-12-19 20:33:03 +00:00
FileStream fs =
WaitForFile(Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(), "Statistics", "Statistics.xml"),
2017-12-19 20:33:03 +00:00
FileMode.Open, FileAccess.Read, FileShare.Read);
statistics = (Stats)xs.Deserialize(fs);
fs.Close();
if(statistics.OperatingSystems != null)
{
operatingSystems = new List<NameValueStats>();
foreach(OsStats nvs in statistics.OperatingSystems)
2017-12-19 20:33:03 +00:00
operatingSystems.Add(new NameValueStats
{
name =
$"{DetectOS.GetPlatformName((PlatformID)Enum.Parse(typeof(PlatformID), nvs.name), nvs.version)}{(string.IsNullOrEmpty(nvs.version) ? "" : " ")}{nvs.version}",
2017-12-19 20:33:03 +00:00
Value = nvs.Value
});
repOperatingSystems.DataSource = operatingSystems.OrderBy(os => os.name).ToList();
repOperatingSystems.DataBind();
}
2017-12-19 20:33:03 +00:00
else divOperatingSystems.Visible = false;
2017-06-04 02:24:04 +01:00
if(statistics.Versions != null)
{
versions = new List<NameValueStats>();
foreach(NameValueStats nvs in statistics.Versions)
versions.Add(nvs.name == "previous"
? new NameValueStats {name = "Previous than 3.4.99.0", Value = nvs.Value}
: nvs);
2017-12-19 20:33:03 +00:00
2017-06-04 02:24:04 +01:00
repVersions.DataSource = versions.OrderBy(ver => ver.name).ToList();
repVersions.DataBind();
}
2017-12-19 20:33:03 +00:00
else divVersions.Visible = false;
2017-06-04 02:24:04 +01:00
if(statistics.Commands != null)
{
lblAnalyze.Text = statistics.Commands.Analyze.ToString();
lblCompare.Text = statistics.Commands.Compare.ToString();
lblChecksum.Text = statistics.Commands.Checksum.ToString();
lblEntropy.Text = statistics.Commands.Entropy.ToString();
lblVerify.Text = statistics.Commands.Verify.ToString();
lblPrintHex.Text = statistics.Commands.PrintHex.ToString();
lblDecode.Text = statistics.Commands.Decode.ToString();
lblDeviceInfo.Text = statistics.Commands.DeviceInfo.ToString();
lblMediaInfo.Text = statistics.Commands.MediaInfo.ToString();
lblMediaScan.Text = statistics.Commands.MediaScan.ToString();
lblFormats.Text = statistics.Commands.Formats.ToString();
lblBenchmark.Text = statistics.Commands.Benchmark.ToString();
lblCreateSidecar.Text = statistics.Commands.CreateSidecar.ToString();
lblDumpMedia.Text = statistics.Commands.DumpMedia.ToString();
lblDeviceReport.Text = statistics.Commands.DeviceReport.ToString();
lblLs.Text = statistics.Commands.Ls.ToString();
lblExtractFiles.Text = statistics.Commands.ExtractFiles.ToString();
lblListDevices.Text = statistics.Commands.ListDevices.ToString();
2017-10-12 22:41:31 +01:00
lblListEncodings.Text = statistics.Commands.ListEncodings.ToString();
}
2017-12-19 20:33:03 +00:00
else divCommands.Visible = false;
if(statistics.Filters != null)
{
repFilters.DataSource = statistics.Filters.OrderBy(filter => filter.name).ToList();
repFilters.DataBind();
}
2017-12-19 20:33:03 +00:00
else divFilters.Visible = false;
if(statistics.MediaImages != null)
{
repMediaImages.DataSource = statistics.MediaImages.OrderBy(filter => filter.name).ToList();
repMediaImages.DataBind();
}
2017-12-19 20:33:03 +00:00
else divMediaImages.Visible = false;
if(statistics.Partitions != null)
{
repPartitions.DataSource = statistics.Partitions.OrderBy(filter => filter.name).ToList();
repPartitions.DataBind();
}
2017-12-19 20:33:03 +00:00
else divPartitions.Visible = false;
if(statistics.Filesystems != null)
{
repFilesystems.DataSource = statistics.Filesystems.OrderBy(filter => filter.name).ToList();
repFilesystems.DataBind();
}
2017-12-19 20:33:03 +00:00
else divFilesystems.Visible = false;
if(statistics.Medias != null)
{
realMedia = new List<MediaItem>();
virtualMedia = new List<MediaItem>();
foreach(MediaStats nvs in statistics.Medias)
{
2017-12-19 20:33:03 +00:00
MediaType
.MediaTypeToString((CommonTypes.MediaType)Enum.Parse(typeof(CommonTypes.MediaType), nvs.type),
out string type, out string subtype);
2017-12-19 20:33:03 +00:00
if(nvs.real) realMedia.Add(new MediaItem {Type = type, SubType = subtype, Count = nvs.Value});
else virtualMedia.Add(new MediaItem {Type = type, SubType = subtype, Count = nvs.Value});
}
if(realMedia.Count > 0)
{
2017-12-19 20:33:03 +00:00
repRealMedia.DataSource =
realMedia.OrderBy(media => media.Type).ThenBy(media => media.SubType).ToList();
repRealMedia.DataBind();
}
2017-12-19 20:33:03 +00:00
else divRealMedia.Visible = false;
if(virtualMedia.Count > 0)
{
2017-12-19 20:33:03 +00:00
repVirtualMedia.DataSource =
virtualMedia.OrderBy(media => media.Type).ThenBy(media => media.SubType).ToList();
repVirtualMedia.DataBind();
}
2017-12-19 20:33:03 +00:00
else divVirtualMedia.Visible = false;
}
else
{
divRealMedia.Visible = false;
divVirtualMedia.Visible = false;
}
if(statistics.Devices != null)
{
devices = new List<DeviceItem>();
foreach(DeviceStats device in statistics.Devices)
{
2017-12-21 16:07:20 +00:00
string url;
string xmlFile;
2017-12-19 20:33:03 +00:00
if(!string.IsNullOrWhiteSpace(device.Manufacturer) &&
!string.IsNullOrWhiteSpace(device.Model) && !string.IsNullOrWhiteSpace(device.Revision))
{
xmlFile = device.Manufacturer + "_" + device.Model + "_" + device.Revision + ".xml";
url =
$"ViewReport.aspx?manufacturer={HttpUtility.UrlPathEncode(device.Manufacturer)}&model={HttpUtility.UrlPathEncode(device.Model)}&revision={HttpUtility.UrlPathEncode(device.Revision)}";
}
2017-12-19 20:33:03 +00:00
else if(!string.IsNullOrWhiteSpace(device.Manufacturer) &&
!string.IsNullOrWhiteSpace(device.Model))
{
xmlFile = device.Manufacturer + "_" + device.Model + ".xml";
url =
$"ViewReport.aspx?manufacturer={HttpUtility.UrlPathEncode(device.Manufacturer)}&model={HttpUtility.UrlPathEncode(device.Model)}";
}
else if(!string.IsNullOrWhiteSpace(device.Model) && !string.IsNullOrWhiteSpace(device.Revision))
{
xmlFile = device.Model + "_" + device.Revision + ".xml";
url =
$"ViewReport.aspx?model={HttpUtility.UrlPathEncode(device.Model)}&revision={HttpUtility.UrlPathEncode(device.Revision)}";
}
else
{
xmlFile = device.Model + ".xml";
url = $"ViewReport.aspx?model={HttpUtility.UrlPathEncode(device.Model)}";
}
xmlFile = xmlFile.Replace('/', '_').Replace('\\', '_').Replace('?', '_');
2017-12-21 14:30:38 +00:00
if(!File.Exists(Path.Combine(HostingEnvironment.MapPath("~"), "Reports",
2017-12-20 23:07:46 +00:00
xmlFile))) url = null;
devices.Add(new DeviceItem
{
Manufacturer = device.Manufacturer,
Model = device.Model,
Revision = device.Revision,
Bus = device.Bus,
2017-12-19 20:33:03 +00:00
ReportLink =
url == null ? "No" : $"<a href=\"{url}\" target=\"_blank\">Yes</a>"
});
}
2017-12-19 20:33:03 +00:00
repDevices.DataSource = devices.OrderBy(device => device.Manufacturer)
.ThenBy(device => device.Model).ThenBy(device => device.Revision)
.ThenBy(device => device.Bus).ToList();
repDevices.DataBind();
}
2017-12-19 20:33:03 +00:00
else divDevices.Visible = false;
}
2017-12-21 16:37:35 +00:00
catch(Exception)
{
content.InnerHtml = "<b>Could not load statistics</b>";
#if DEBUG
throw;
#endif
}
}
static FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
2017-06-03 01:10:46 +01:00
{
for(int numTries = 0; numTries < 100; numTries++)
2017-06-03 01:10:46 +01:00
{
FileStream fs = null;
try
{
fs = new FileStream(fullPath, mode, access, share);
return fs;
}
catch(IOException)
{
fs?.Dispose();
2017-12-21 14:30:38 +00:00
Thread.Sleep(50);
}
2017-06-03 01:10:46 +01:00
}
return null;
2017-06-03 01:10:46 +01:00
}
}
2017-12-19 20:33:03 +00:00
}