Files
Aaru.Server/DiscImageChef.Server/Controllers/UploadStatsController.cs

263 lines
10 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : UploadStatsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles statistics uploads.
//
// --[ 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;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
2019-11-02 21:01:25 +00:00
using System.Threading.Tasks;
2019-11-02 01:40:41 +00:00
using System.Xml.Serialization;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Server.Models;
2019-11-02 21:01:25 +00:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
2019-11-02 01:40:41 +00:00
using Newtonsoft.Json;
using OperatingSystem = DiscImageChef.Server.Models.OperatingSystem;
using Version = DiscImageChef.Server.Models.Version;
namespace DiscImageChef.Server.Controllers
{
2019-11-02 21:01:25 +00:00
public class UploadStatsController : Controller
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
private readonly DicServerContext _ctx;
2019-11-02 21:01:25 +00:00
private IWebHostEnvironment _environment;
public UploadStatsController(IWebHostEnvironment environment, DicServerContext ctx)
{
_environment = environment;
_ctx = ctx;
}
2019-11-02 21:37:09 +00:00
2019-11-02 01:40:41 +00:00
/// <summary>
/// Receives statistics from DiscImageChef.Core, processes them and adds them to a server-side global statistics XML
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/uploadstats")]
[HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadStats()
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var response = new ContentResult {StatusCode = (int) HttpStatusCode.OK, ContentType = "text/plain"};
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 21:37:09 +00:00
var newStats = new Stats();
var request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var xs = new XmlSerializer(newStats.GetType());
newStats = (Stats) xs.Deserialize(
new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (newStats == null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
response.Content = "notstats";
2019-11-02 01:40:41 +00:00
return response;
}
StatsConverter.Convert(newStats);
2019-11-02 21:01:25 +00:00
response.Content = "ok";
2019-11-02 01:40:41 +00:00
return response;
}
2019-11-02 21:37:09 +00:00
catch (Exception ex)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
#if DEBUG
if (Debugger.IsAttached) throw;
#endif
2019-11-02 21:01:25 +00:00
response.Content = "error";
2019-11-02 01:40:41 +00:00
return response;
}
}
/// <summary>
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/uploadstatsv2")]
[HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadStatsV2()
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var response = new ContentResult {StatusCode = (int) HttpStatusCode.OK, ContentType = "text/plain"};
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 21:37:09 +00:00
var request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var sr = new StreamReader(request.Body);
2019-11-02 21:01:25 +00:00
var statsString = await sr.ReadToEndAsync();
2019-11-02 21:37:09 +00:00
var newstats = JsonConvert.DeserializeObject<StatsDto>(statsString);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (newstats == null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
response.Content = "notstats";
2019-11-02 01:40:41 +00:00
return response;
}
2019-11-02 21:37:09 +00:00
if (newstats.Commands != null)
foreach (var nvs in newstats.Commands)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var existing = _ctx.Commands.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.Commands.Add(new Command {Name = nvs.name, Count = nvs.Value});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
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
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 {Value = nvs.name, Count = nvs.Value});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
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
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 {Name = nvs.name, Count = nvs.Value});
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
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 {Name = nvs.name, Count = nvs.Value});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
2019-11-02 21:37:09 +00:00
if (newstats.MediaFormats != null)
foreach (var nvs in newstats.MediaFormats)
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 {Name = nvs.name, Count = nvs.Value});
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
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 {Name = nvs.name, Count = nvs.Value});
2019-11-02 01:40:41 +00:00
else existing.Count += nvs.Value;
}
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
var existing =
_ctx.OperatingSystems.FirstOrDefault(c => c.Name == operatingSystem.name &&
c.Version == operatingSystem.version);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 21:01:25 +00:00
_ctx.OperatingSystems.Add(new OperatingSystem
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
Name = operatingSystem.name,
2019-11-02 01:40:41 +00:00
Version = operatingSystem.version,
2019-11-02 21:37:09 +00:00
Count = operatingSystem.Value
2019-11-02 01:40:41 +00:00
});
else existing.Count += operatingSystem.Value;
}
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
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 21:01:25 +00:00
_ctx.Medias.Add(new Media {Type = media.type, Real = media.real, Count = media.Value});
2019-11-02 01:40:41 +00:00
else existing.Count += media.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
var existing =
_ctx.DeviceStats.FirstOrDefault(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
2019-11-02 21:37:09 +00:00
if (existing == null)
2019-11-02 21:01:25 +00:00
_ctx.DeviceStats.Add(new DeviceStat
2019-11-02 01:40:41 +00:00
{
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:01:25 +00:00
_ctx.SaveChanges();
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
response.Content = "ok";
2019-11-02 01:40:41 +00:00
return response;
}
// ReSharper disable once RedundantCatchClause
catch
{
2019-11-02 21:37:09 +00:00
#if DEBUG
if (Debugger.IsAttached) throw;
#endif
2019-11-02 21:01:25 +00:00
response.Content = "error";
2019-11-02 01:40:41 +00:00
return response;
}
}
2019-11-02 21:37:09 +00:00
private FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
for (var numTries = 0; numTries < 100; numTries++)
2019-11-02 01:40:41 +00:00
{
FileStream fs = null;
try
{
fs = new FileStream(fullPath, mode, access, share);
return fs;
}
2019-11-02 21:37:09 +00:00
catch (IOException)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
if (fs != null) fs.Dispose();
2019-11-02 01:40:41 +00:00
Thread.Sleep(50);
}
}
return null;
}
}
}