Files
Aaru/Aaru.Gui/ViewModels/Dialogs/StatisticsViewModel.cs

529 lines
20 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : StatisticsViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the statistics dialog.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
using System.Collections.ObjectModel;
using System.Linq;
2025-08-20 21:19:43 +01:00
using System.Windows.Input;
using Aaru.Database;
using Aaru.Database.Models;
using Aaru.Gui.Models;
2020-04-16 20:40:25 +01:00
using Aaru.Gui.Views.Dialogs;
using Aaru.Localization;
using CommunityToolkit.Mvvm.ComponentModel;
2025-08-20 21:19:43 +01:00
using CommunityToolkit.Mvvm.Input;
2020-07-22 13:20:25 +01:00
using NameCountModel = Aaru.Gui.Models.NameCountModel;
namespace Aaru.Gui.ViewModels.Dialogs;
public sealed partial class StatisticsViewModel : ViewModelBase
{
2022-03-06 13:29:38 +00:00
readonly StatisticsDialog _view;
[ObservableProperty]
string _checksumText;
[ObservableProperty]
bool _checksumVisible;
[ObservableProperty]
bool _commandsVisible;
[ObservableProperty]
string _compareText;
[ObservableProperty]
bool _compareVisible;
[ObservableProperty]
string _convertImageText;
[ObservableProperty]
bool _convertImageVisible;
[ObservableProperty]
string _createSidecarText;
[ObservableProperty]
bool _createSidecarVisible;
[ObservableProperty]
string _decodeText;
[ObservableProperty]
bool _decodeVisible;
[ObservableProperty]
string _DeviceInfoText;
[ObservableProperty]
bool _deviceInfoVisible;
[ObservableProperty]
string _deviceReportText;
[ObservableProperty]
bool _deviceReportVisible;
[ObservableProperty]
bool _devicesVisible;
[ObservableProperty]
string _dumpMediaText;
[ObservableProperty]
bool _dumpMediaVisible;
[ObservableProperty]
string _entropyText;
[ObservableProperty]
bool _entropyVisible;
[ObservableProperty]
bool _filesystemsVisible;
[ObservableProperty]
bool _filtersVisible;
[ObservableProperty]
string _formatsCommandText;
[ObservableProperty]
bool _formatsCommandVisible;
[ObservableProperty]
bool _formatsVisible;
[ObservableProperty]
string _fsInfoText;
[ObservableProperty]
bool _fsInfoVisible;
[ObservableProperty]
string _imageInfoText;
[ObservableProperty]
bool _imageInfoVisible;
[ObservableProperty]
string _mediaInfoText;
[ObservableProperty]
bool _mediaInfoVisible;
[ObservableProperty]
string _mediaScanText;
[ObservableProperty]
bool _mediaScanVisible;
[ObservableProperty]
bool _mediasVisible;
[ObservableProperty]
bool _partitionsVisible;
[ObservableProperty]
string _printHexText;
[ObservableProperty]
bool _printHexVisible;
[ObservableProperty]
string _verifyText;
[ObservableProperty]
bool _verifyVisible;
2022-03-06 13:29:38 +00:00
public StatisticsViewModel(StatisticsDialog view)
{
_view = view;
2024-05-01 04:39:38 +01:00
Filters = [];
Formats = [];
Partitions = [];
Filesystems = [];
Devices = [];
Medias = [];
2025-08-20 21:19:43 +01:00
CloseCommand = new RelayCommand(Close);
using var ctx = AaruContext.Create(Settings.Settings.LocalDbPath);
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any())
{
if(ctx.Commands.Any(c => c.Name == "analyze"))
{
2022-03-06 13:29:38 +00:00
foreach(Command oldAnalyze in ctx.Commands.Where(c => c.Name == "analyze"))
{
2022-03-06 13:29:38 +00:00
oldAnalyze.Name = "fs-info";
ctx.Commands.Update(oldAnalyze);
2021-03-05 14:17:07 +00:00
}
2022-03-06 13:29:38 +00:00
ulong count = 0;
2022-03-06 13:29:38 +00:00
foreach(Command fsInfo in ctx.Commands.Where(c => c.Name == "fs-info" && c.Synchronized))
{
2022-03-06 13:29:38 +00:00
count += fsInfo.Count;
ctx.Remove(fsInfo);
}
2022-03-06 13:29:38 +00:00
if(count > 0)
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
ctx.Commands.Add(new Command
{
Count = count,
Name = "fs-info",
Synchronized = true
});
2023-10-03 23:27:57 +01:00
}
2022-03-06 13:29:38 +00:00
ctx.SaveChanges();
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "fs-info"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "fs-info" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "fs-info" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
FsInfoVisible = true;
FsInfoText = string.Format(UI.You_have_called_the_Filesystem_Info_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "checksum"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "checksum" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "checksum" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
ChecksumVisible = true;
ChecksumText = string.Format(UI.You_have_called_the_Checksum_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "compare"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "compare" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "compare" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
CompareVisible = true;
CompareText = string.Format(UI.You_have_called_the_Compare_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "convert-image"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "convert-image" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "convert-image" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
ConvertImageVisible = true;
ConvertImageText = string.Format(UI.You_have_called_the_Convert_Image_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "create-sidecar"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "create-sidecar" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "create-sidecar" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
CreateSidecarVisible = true;
CreateSidecarText = string.Format(UI.You_have_called_the_Create_Sidecar_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "decode"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "decode" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "decode" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
DecodeVisible = true;
DecodeText = string.Format(UI.You_have_called_the_Decode_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "device-info"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "device-info" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "device-info" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
DeviceInfoVisible = true;
DeviceInfoText = string.Format(UI.You_have_called_the_Device_Info_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "device-report"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "device-report" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "device-report" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
DeviceReportVisible = true;
DeviceReportText = string.Format(UI.You_have_called_the_Device_Report_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "dump-media"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "dump-media" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "dump-media" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
DumpMediaVisible = true;
DumpMediaText = string.Format(UI.You_have_called_the_Dump_Media_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "entropy"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "entropy" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "entropy" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
EntropyVisible = true;
EntropyText = string.Format(UI.You_have_called_the_Entropy_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "formats"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "formats" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "formats" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
FormatsCommandVisible = true;
FormatsCommandText = string.Format(UI.You_have_called_the_Formats_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "image-info"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "image-info" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "image-info" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
ImageInfoVisible = true;
ImageInfoText = string.Format(UI.You_have_called_the_Image_Info_command_0_times, count);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "media-info"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "media-info" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "media-info" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
MediaInfoVisible = true;
MediaInfoText = string.Format(UI.You_have_called_the_Media_Info_command_0_times, count);
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "media-scan"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "media-scan" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "media-scan" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
MediaScanVisible = true;
MediaScanText = string.Format(UI.You_have_called_the_Media_Scan_command_0_times, count);
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "printhex"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "printhex" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "printhex" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
PrintHexVisible = true;
PrintHexText = string.Format(UI.You_have_called_the_Print_Hex_command_0_times, count);
}
2022-03-06 13:29:38 +00:00
if(ctx.Commands.Any(c => c.Name == "verify"))
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Commands.Where(c => c.Name == "verify" && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Commands.LongCount(c => c.Name == "verify" && !c.Synchronized);
2022-03-06 13:29:38 +00:00
VerifyVisible = true;
VerifyText = string.Format(UI.You_have_called_the_Verify_command_0_times, count);
}
CommandsVisible = FsInfoVisible ||
ChecksumVisible ||
CompareVisible ||
ConvertImageVisible ||
CreateSidecarVisible ||
DecodeVisible ||
DeviceInfoVisible ||
DeviceReportVisible ||
DumpMediaVisible ||
EntropyVisible ||
FormatsCommandVisible ||
ImageInfoVisible ||
MediaInfoVisible ||
MediaScanVisible ||
PrintHexVisible ||
VerifyVisible;
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(ctx.Filters.Any())
{
FiltersVisible = true;
foreach(string nvs in ctx.Filters.Select(n => n.Name).Distinct())
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Filters.Where(c => c.Name == nvs && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Filters.LongCount(c => c.Name == nvs && !c.Synchronized);
2022-03-06 13:29:38 +00:00
Filters.Add(new NameCountModel
{
Name = nvs,
Count = count
});
}
2022-03-06 13:29:38 +00:00
}
if(ctx.MediaFormats.Any())
{
FormatsVisible = true;
2022-03-06 13:29:38 +00:00
foreach(string nvs in ctx.MediaFormats.Select(n => n.Name).Distinct())
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.MediaFormats.Where(c => c.Name == nvs && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.MediaFormats.LongCount(c => c.Name == nvs && !c.Synchronized);
2022-03-06 13:29:38 +00:00
Formats.Add(new NameCountModel
{
Name = nvs,
Count = count
});
}
}
2022-03-06 13:29:38 +00:00
if(ctx.Partitions.Any())
{
PartitionsVisible = true;
2022-03-06 13:29:38 +00:00
foreach(string nvs in ctx.Partitions.Select(n => n.Name).Distinct())
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Partitions.Where(c => c.Name == nvs && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Partitions.LongCount(c => c.Name == nvs && !c.Synchronized);
2022-03-06 13:29:38 +00:00
Partitions.Add(new NameCountModel
{
Name = nvs,
Count = count
});
}
}
2022-03-06 13:29:38 +00:00
if(ctx.Filesystems.Any())
{
FilesystemsVisible = true;
2022-03-06 13:29:38 +00:00
foreach(string nvs in ctx.Filesystems.Select(n => n.Name).Distinct())
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Filesystems.Where(c => c.Name == nvs && c.Synchronized)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Filesystems.LongCount(c => c.Name == nvs && !c.Synchronized);
2022-03-06 13:29:38 +00:00
Filesystems.Add(new NameCountModel
{
2022-03-06 13:29:38 +00:00
Name = nvs,
Count = count
});
}
}
2022-03-06 13:29:38 +00:00
if(ctx.SeenDevices.Any())
{
2022-03-06 13:29:38 +00:00
DevicesVisible = true;
2024-05-01 04:05:22 +01:00
foreach(DeviceStat ds in ctx.SeenDevices.OrderBy(n => n.Manufacturer)
.ThenBy(n => n.Manufacturer)
.ThenBy(n => n.Revision)
.ThenBy(n => n.Bus))
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
Devices.Add(new DeviceStatsModel
{
Model = ds.Model,
Manufacturer = ds.Manufacturer,
Revision = ds.Revision,
Bus = ds.Bus
});
2023-10-03 23:27:57 +01:00
}
}
2024-05-01 04:05:22 +01:00
if(!ctx.Medias.Any()) return;
2022-03-06 13:29:38 +00:00
MediasVisible = true;
2022-03-06 13:29:38 +00:00
foreach(string media in ctx.Medias.OrderBy(ms => ms.Type).Select(ms => ms.Type).Distinct())
{
2024-05-01 04:05:22 +01:00
ulong count = ctx.Medias.Where(c => c.Type == media && c.Synchronized && c.Real)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Medias.LongCount(c => c.Type == media && !c.Synchronized && c.Real);
2022-03-06 13:29:38 +00:00
if(count > 0)
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
Medias.Add(new MediaStatsModel
{
Name = media,
Count = count,
Type = UI.Media_found_type_real
2022-03-06 13:29:38 +00:00
});
2023-10-03 23:27:57 +01:00
}
2024-05-01 04:05:22 +01:00
count = ctx.Medias.Where(c => c.Type == media && c.Synchronized && !c.Real)
.Select(c => c.Count)
.FirstOrDefault();
2022-03-06 13:29:38 +00:00
count += (ulong)ctx.Medias.LongCount(c => c.Type == media && !c.Synchronized && !c.Real);
2024-05-01 04:05:22 +01:00
if(count == 0) continue;
2022-03-06 13:29:38 +00:00
Medias.Add(new MediaStatsModel
{
Name = media,
Count = count,
Type = UI.Media_found_type_image
2022-03-06 13:29:38 +00:00
});
}
2022-03-06 13:29:38 +00:00
}
2025-08-20 21:19:43 +01:00
public ICommand CloseCommand { get; }
2022-03-06 13:29:38 +00:00
public ObservableCollection<NameCountModel> Filters { get; }
public ObservableCollection<NameCountModel> Formats { get; }
public ObservableCollection<NameCountModel> Partitions { get; }
public ObservableCollection<NameCountModel> Filesystems { get; }
public ObservableCollection<DeviceStatsModel> Devices { get; }
public ObservableCollection<MediaStatsModel> Medias { get; }
2025-08-20 21:19:43 +01:00
void Close() => _view.Close();
}