mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 11:14:27 +00:00
Add ATA IDENTIFY DEVICE responses management view with delete and consolidate functionality
This commit is contained in:
@@ -5,8 +5,13 @@ namespace Aaru.Server.Database.Models;
|
||||
|
||||
public class IdHashModel : BaseModel<int>
|
||||
{
|
||||
string? _description;
|
||||
public string Hash { get; set; }
|
||||
public string Description => string.Join(' ', VendorIdentification, ProductIdentification, ProductRevisionLevel);
|
||||
public string? Description
|
||||
{
|
||||
get => _description ?? string.Join(' ', VendorIdentification, ProductIdentification, ProductRevisionLevel);
|
||||
set => _description = value;
|
||||
}
|
||||
public int[] Duplicates { get; set; }
|
||||
public string VendorIdentification => StringHandlers.CToString(Inquiry?.VendorIdentification);
|
||||
public string ProductIdentification => StringHandlers.CToString(Inquiry?.ProductIdentification);
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
<NavLink class="nav-link" href="/admin" Match="NavLinkMatch.All">
|
||||
<span aria-hidden="true" class="bi bi-house-door-fill-nav-menu"></span> Dashboard
|
||||
</NavLink>
|
||||
<NavLink class="nav-link" href="/admin/ata">
|
||||
ATA IDENTIFY DEVICE responses
|
||||
</NavLink>
|
||||
<NavLink class="nav-link" href="/admin/block-descriptors">
|
||||
Block descriptors
|
||||
</NavLink>
|
||||
|
||||
103
Aaru.Server/Components/Admin/Pages/Ata/List.razor
Normal file
103
Aaru.Server/Components/Admin/Pages/Ata/List.razor
Normal file
@@ -0,0 +1,103 @@
|
||||
@page "/admin/ata"
|
||||
@attribute [Authorize]
|
||||
@layout AdminLayout
|
||||
|
||||
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
|
||||
|
||||
<PageTitle>ATA IDENTIFY DEVICE responses</PageTitle>
|
||||
|
||||
@if(!_initialized)
|
||||
{
|
||||
<div class="stats-section">
|
||||
<h1 style="color: red; align-content: center; padding: 2rem">Loading...</h1>
|
||||
</div>
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
<section class="stats-section">
|
||||
<h4>ATA IDENTIFY DEVICE responses</h4>
|
||||
<table class="table table-dark table-striped table-bordered mt-4 mb-4">
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th class="fw-bold bg-secondary text-light">
|
||||
@DisplayNameHelper.GetDisplayName(typeof(Ata), nameof(Ata.Id))
|
||||
</th>
|
||||
<th class="fw-bold bg-secondary text-light">
|
||||
@DisplayNameHelper.GetDisplayName(typeof(Ata), nameof(Ata.IdentifyDevice.Value.Model))
|
||||
</th>
|
||||
<th class="fw-bold bg-secondary text-light">
|
||||
Firmware revision
|
||||
</th>
|
||||
<th class="fw-bold bg-secondary text-light">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach(Ata item in _items)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@item.Id
|
||||
</td>
|
||||
<td>
|
||||
@item.IdentifyDevice?.Model
|
||||
</td>
|
||||
<td>
|
||||
@item.IdentifyDevice?.FirmwareRevision
|
||||
</td>
|
||||
<td>
|
||||
<a href="/admin/ata/@item.Id" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-eye"></i> Details
|
||||
</a>
|
||||
<button class="btn btn-danger btn-sm" @onclick="async () => await ShowDeleteModal(item.Id)">
|
||||
<i class="bi bi-trash"></i> Delete
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@if(_duplicates.Count > 0)
|
||||
{
|
||||
<div>
|
||||
The following ATA IDENTIFY DEVICE responses have duplicates.
|
||||
<table class="table table-dark table-striped table-bordered mt-4 mb-4">
|
||||
<tbody>
|
||||
@foreach(IdHashModel item in _duplicates)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@item.Description
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<Button class="btn btn-danger" @onclick="ConsolidateDuplicatesAsync">Consolidate Duplicates</Button>
|
||||
}
|
||||
</section>
|
||||
|
||||
<BlazorBootstrap.Modal @ref="_consolidateModal" Title="Consolidate duplicates" Size="ModalSize.Small">
|
||||
<BodyTemplate>
|
||||
<div class="text-danger">Are you sure you want to delete the duplicates?</div>
|
||||
</BodyTemplate>
|
||||
<FooterTemplate>
|
||||
<button class="btn btn-secondary" @onclick="HideConsolidateModalAsync">Cancel</button>
|
||||
<button class="btn btn-danger" @onclick="ConfirmConsolidateAsync">Delete</button>
|
||||
</FooterTemplate>
|
||||
</BlazorBootstrap.Modal>
|
||||
|
||||
<BlazorBootstrap.Modal @ref="_deleteModal" Title="Delete ATA IDENTIFY DEVICE response" Size="ModalSize.Small">
|
||||
<BodyTemplate>
|
||||
<div class="text-danger">Are you sure you want to delete this ATA IDENTIFY DEVICE response?</div>
|
||||
</BodyTemplate>
|
||||
<FooterTemplate>
|
||||
<button class="btn btn-secondary" @onclick="HideDeleteModal">Cancel</button>
|
||||
<button class="btn btn-danger" @onclick="ConfirmDelete">Delete</button>
|
||||
</FooterTemplate>
|
||||
</BlazorBootstrap.Modal>
|
||||
150
Aaru.Server/Components/Admin/Pages/Ata/List.razor.cs
Normal file
150
Aaru.Server/Components/Admin/Pages/Ata/List.razor.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using Aaru.Server.Core;
|
||||
using Aaru.Server.Database.Models;
|
||||
using BlazorBootstrap;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
|
||||
namespace Aaru.Server.Components.Admin.Pages.Ata;
|
||||
|
||||
public partial class List
|
||||
{
|
||||
private Modal? _consolidateModal;
|
||||
private int _deleteId;
|
||||
private Modal? _deleteModal;
|
||||
List<IdHashModel?> _duplicates;
|
||||
bool _initialized;
|
||||
List<CommonTypes.Metadata.Ata> _items;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
|
||||
StateHasChanged();
|
||||
|
||||
await RefreshItemsAsync();
|
||||
|
||||
_initialized = true;
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
async Task RefreshItemsAsync()
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
_items = ctx.Ata.AsEnumerable()
|
||||
.OrderBy(static m => m.IdentifyDevice?.Model)
|
||||
.ThenBy(static m => m.IdentifyDevice?.FirmwareRevision)
|
||||
.ToList();
|
||||
|
||||
List<IdHashModel> hashes = await ctx.Ata.Select(static m => new IdHashModel
|
||||
{
|
||||
Id = m.Id,
|
||||
Hash = Hash.Sha512(m.Identify)
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
_duplicates = hashes.GroupBy(static x => x.Hash)
|
||||
.Where(static g => g.Count() > 1)
|
||||
.Select(x => hashes.FirstOrDefault(y => y.Hash == x.Key))
|
||||
.ToList();
|
||||
|
||||
for(int i = 0; i < _duplicates.Count; i++)
|
||||
{
|
||||
CommonTypes.Metadata.Ata unique = ctx.Ata.First(a => a.Id == _duplicates[i].Id);
|
||||
|
||||
_duplicates[i].Description = unique.IdentifyDevice?.Model;
|
||||
|
||||
_duplicates[i].Duplicates = hashes.Where(h => h.Hash == _duplicates[i]?.Hash)
|
||||
.Skip(1)
|
||||
.Select(static x => x.Id)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
Task ConsolidateDuplicatesAsync() => _consolidateModal?.ShowAsync();
|
||||
|
||||
Task HideConsolidateModalAsync() => _consolidateModal?.HideAsync();
|
||||
|
||||
async Task ConfirmConsolidateAsync()
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
foreach(IdHashModel? duplicate in _duplicates)
|
||||
{
|
||||
CommonTypes.Metadata.Ata? master = ctx.Ata.FirstOrDefault(m => duplicate != null && m.Id == duplicate.Id);
|
||||
|
||||
if(master is null) continue;
|
||||
|
||||
if(duplicate?.Duplicates == null) continue;
|
||||
|
||||
foreach(int duplicateId in duplicate.Duplicates)
|
||||
{
|
||||
CommonTypes.Metadata.Ata? slave = ctx.Ata.Include(static ata => ata.ReadCapabilities)
|
||||
.FirstOrDefault(m => m.Id == duplicateId);
|
||||
|
||||
if(slave is null) continue;
|
||||
|
||||
foreach(Device ataDevice in ctx.Devices.Where(d => d.ATA.Id == duplicateId)) ataDevice.ATA = master;
|
||||
|
||||
foreach(Device atapiDevice in ctx.Devices.Where(d => d.ATAPI.Id == duplicateId))
|
||||
atapiDevice.ATAPI = master;
|
||||
|
||||
foreach(UploadedReport ataReport in ctx.Reports.Where(d => d.ATA.Id == duplicateId))
|
||||
ataReport.ATA = master;
|
||||
|
||||
foreach(UploadedReport atapiReport in ctx.Reports.Where(d => d.ATAPI.Id == duplicateId))
|
||||
atapiReport.ATAPI = master;
|
||||
|
||||
foreach(CommonTypes.Metadata.TestedMedia testedMedia in
|
||||
ctx.TestedMedia.Where(d => d.AtaId == duplicateId))
|
||||
{
|
||||
testedMedia.AtaId = duplicate.Id;
|
||||
ctx.Update(testedMedia);
|
||||
}
|
||||
|
||||
if(master.ReadCapabilities is null && slave.ReadCapabilities != null)
|
||||
master.ReadCapabilities = slave.ReadCapabilities;
|
||||
|
||||
ctx.Ata.Remove(slave);
|
||||
}
|
||||
}
|
||||
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
await RefreshItemsAsync();
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task ShowDeleteModal(int id)
|
||||
{
|
||||
_deleteId = id;
|
||||
if(_deleteModal != null) await _deleteModal.ShowAsync();
|
||||
}
|
||||
|
||||
private async Task HideDeleteModal()
|
||||
{
|
||||
if(_deleteModal != null) await _deleteModal.HideAsync();
|
||||
}
|
||||
|
||||
private async Task ConfirmDelete()
|
||||
{
|
||||
await DeleteAsync(_deleteId);
|
||||
await HideDeleteModal();
|
||||
await RefreshItemsAsync();
|
||||
}
|
||||
|
||||
private async Task DeleteAsync(int id)
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
CommonTypes.Metadata.Ata? ata = await ctx.Ata.FindAsync(id);
|
||||
|
||||
if(ata is not null)
|
||||
{
|
||||
ctx.Ata.Remove(ata);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user