Add option to consolidate duplicate ATA entities.

This commit is contained in:
2019-11-09 19:23:49 +00:00
parent d3b59108df
commit f7ea40cfb0
8 changed files with 172 additions and 1 deletions

View File

@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DiscImageChef.Server.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Areas.Admin.Controllers
{
@@ -36,7 +38,6 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
return View(ata);
}
// GET: Admin/Atas/Delete/5
public async Task<IActionResult> Delete(int? id)
{
@@ -67,5 +68,77 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
}
bool AtaExists(int id) => _context.Ata.Any(e => e.Id == id);
public IActionResult Consolidate()
{
List<IdHashModel> hashes =
_context.Ata.Select(m => new IdHashModel(m.Id, Hash.Sha512(m.Identify))).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++)
{
CommonTypes.Metadata.Ata unique = _context.Ata.First(a => a.Id == dups[i].Id);
dups[i].Description = unique.IdentifyDevice?.Model;
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)
{
foreach(int duplicateId in duplicate.Duplicates)
{
foreach(Device ataDevice in _context.Devices.Where(d => d.ATAId == duplicateId))
{
ataDevice.ATAId = duplicate.Id;
}
foreach(Device atapiDevice in _context.Devices.Where(d => d.ATAPIId == duplicateId))
{
atapiDevice.ATAPIId = duplicate.Id;
}
foreach(UploadedReport ataReport in _context.Reports.Where(d => d.ATAId == duplicateId))
{
ataReport.ATAId = duplicate.Id;
}
foreach(UploadedReport atapiReport in _context.Reports.Where(d => d.ATAPIId == duplicateId))
{
atapiReport.ATAPIId = duplicate.Id;
}
_context.Ata.Remove(_context.Ata.First(d => d.Id == duplicateId));
}
}
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
}
}

View File

@@ -0,0 +1,31 @@
@model IdHashModelForView
@{
ViewBag.Title = "Consolidate duplicate ATAs";
Layout = "_Layout";
}
<h2>Consolidate duplicate ATAs</h2>
<div>
The following ATA IDENTIFY DEVICE 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>

View File

@@ -0,0 +1,36 @@
using System.Security.Cryptography;
namespace DiscImageChef.Server
{
public static class Hash
{
public static string Sha512(byte[] data)
{
byte[] hash;
using(var sha = new SHA512Managed())
{
sha.Initialize();
hash = sha.ComputeHash(data);
}
char[] chars = new char[hash.Length * 2];
int j = 0;
foreach(byte b in hash)
{
int nibble1 = (b & 0xF0) >> 4;
int nibble2 = b & 0x0F;
nibble1 += nibble1 > 9 ? 0x57 : 0x30;
nibble2 += nibble2 > 9 ? 0x57 : 0x30;
chars[j++] = (char)nibble1;
chars[j++] = (char)nibble2;
}
return new string(chars);
}
}
}

View File

@@ -96,6 +96,7 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Device\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Device\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Device\Index.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Atas\EditorTemplates\IdHashModel.cshtml" />
</ItemGroup>
<ItemGroup>

View File

@@ -65,5 +65,7 @@ namespace DiscImageChef.Server.Models
[DefaultValue(0)]
public int OptimalMultipleSectorsRead { get; set; }
public int ATAId { get; set; }
public int ATAPIId { get; set; }
}
}

View File

@@ -0,0 +1,16 @@
namespace DiscImageChef.Server.Models
{
public class IdHashModel
{
public IdHashModel(int id, string hash)
{
Id = id;
Hash = hash;
}
public int Id { get; set; }
public string Hash { get; set; }
public string Description { get; set; }
public int[] Duplicates { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace DiscImageChef.Server.Models
{
public class IdHashModelForView
{
public List<IdHashModel> List { get; set; }
public string Json { get; set; }
}
}

View File

@@ -58,5 +58,7 @@ namespace DiscImageChef.Server.Models
}
public DateTime UploadedWhen { get; set; }
public int ATAId { get; set; }
public int ATAPIId { get; set; }
}
}