mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add consolidation for SCSI entities.
This commit is contained in:
Submodule DiscImageChef.CommonTypes updated: e95d90a932...40ce44b1d4
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DiscImageChef.CommonTypes.Metadata;
|
using DiscImageChef.CommonTypes.Metadata;
|
||||||
@@ -5,6 +6,7 @@ using DiscImageChef.Server.Models;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace DiscImageChef.Server.Areas.Admin.Controllers
|
namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||||
{
|
{
|
||||||
@@ -65,6 +67,88 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
|
|||||||
return RedirectToAction(nameof(Index));
|
return RedirectToAction(nameof(Index));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScsiExists(int id) => _context.Scsi.Any(e => e.Id == id);
|
public IActionResult Consolidate()
|
||||||
|
{
|
||||||
|
List<IdHashModel> hashes = _context.Scsi.Where(m => m.InquiryData != null).
|
||||||
|
Select(m => new IdHashModel(m.Id, Hash.Sha512(m.InquiryData))).ToList();
|
||||||
|
|
||||||
|
List<IdHashModel> dups = hashes.GroupBy(x => x.Hash).Where(g => g.Count() > 1).
|
||||||
|
Select(x => hashes.FirstOrDefault(y => y.Hash == x.Key)).ToList();
|
||||||
|
|
||||||
|
for(int i = 0; i < dups.Count; i++)
|
||||||
|
{
|
||||||
|
Scsi unique = _context.Scsi.First(a => a.Id == dups[i].Id);
|
||||||
|
|
||||||
|
dups[i].Description =
|
||||||
|
$"{StringHandlers.CToString(unique.Inquiry?.VendorIdentification)} {StringHandlers.CToString(unique.Inquiry?.ProductIdentification)}";
|
||||||
|
|
||||||
|
dups[i].Duplicates = hashes.Where(h => h.Hash == dups[i].Hash).Skip(1).Select(x => x.Id).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return View(new IdHashModelForView
|
||||||
|
{
|
||||||
|
List = dups, Json = JsonConvert.SerializeObject(dups)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
||||||
|
public IActionResult ConsolidateConfirmed(string models)
|
||||||
|
{
|
||||||
|
IdHashModel[] duplicates;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
duplicates = JsonConvert.DeserializeObject<IdHashModel[]>(models);
|
||||||
|
}
|
||||||
|
catch(JsonSerializationException)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(duplicates is null)
|
||||||
|
return BadRequest();
|
||||||
|
|
||||||
|
foreach(IdHashModel duplicate in duplicates)
|
||||||
|
{
|
||||||
|
Scsi master = _context.Scsi.FirstOrDefault(m => m.Id == duplicate.Id);
|
||||||
|
|
||||||
|
if(master is null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
foreach(int duplicateId in duplicate.Duplicates)
|
||||||
|
{
|
||||||
|
Scsi slave = _context.Scsi.FirstOrDefault(m => m.Id == duplicateId);
|
||||||
|
|
||||||
|
if(slave is null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
foreach(Device scsiDevice in _context.Devices.Where(d => d.SCSI.Id == duplicateId))
|
||||||
|
{
|
||||||
|
scsiDevice.SCSI = master;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(UploadedReport scsiReport in _context.Reports.Where(d => d.SCSI.Id == duplicateId))
|
||||||
|
{
|
||||||
|
scsiReport.SCSI = master;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(TestedMedia testedMedia in _context.TestedMedia.Where(d => d.ScsiId == duplicateId))
|
||||||
|
{
|
||||||
|
testedMedia.ScsiId = duplicate.Id;
|
||||||
|
_context.Update(testedMedia);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(master.ReadCapabilities is null &&
|
||||||
|
slave.ReadCapabilities != null)
|
||||||
|
master.ReadCapabilities = slave.ReadCapabilities;
|
||||||
|
|
||||||
|
_context.Scsi.Remove(slave);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
@model IdHashModelForView
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "Consolidate duplicate SCSIs";
|
||||||
|
Layout = "_Layout";
|
||||||
|
}
|
||||||
|
<h2>Consolidate duplicate SCSIs</h2>
|
||||||
|
<div>
|
||||||
|
The following SCSI INQUIRY have duplicates.
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach (var item in Model.List)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
@Html.DisplayFor(modelItem => item.Description)
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
Do you want to remove the duplicates?
|
||||||
|
<form asp-action="Consolidate" enctype="multipart/form-data">
|
||||||
|
<input type="hidden" asp-for="Json" name="models" />
|
||||||
|
<a asp-action="Index" class="btn btn-primary">Back to List</a>
|
||||||
|
<input class="btn btn-danger" type="submit" value="Consolidate" />
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
@@ -34,6 +34,10 @@
|
|||||||
// Copyright © 2011-2019 Natalia Portillo
|
// Copyright © 2011-2019 Natalia Portillo
|
||||||
// ****************************************************************************/
|
// ****************************************************************************/
|
||||||
}
|
}
|
||||||
|
SCSI INQUIRY responses
|
||||||
|
<div>
|
||||||
|
<a asp-action="Consolidate" class="btn btn-danger">Consolidate duplicates</a>
|
||||||
|
</div>
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user