Files
Chronos/src/Titano.App/Archive/ArchiveFigures.cs
T
Claude f7affe7840 feat(app): import and archive manager logic (plans, card pool, card gate, server sink, tree, charts, reports)
Plain classes behind the Import and Archive screens: plan totals, session
rows and the Show plan listing; duplicate and title choices without a new
scan; folder and server destinations with the protocol client's chunked,
hash-verified uploads; the after-import gate that allows deleting or
formatting a card only after a verified archive import and a dry run; the
archive tree, chart aggregation, mass rename/retag and maintenance reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 17:39:03 +00:00

169 lines
8.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using Titano.App.Infrastructure;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Naming;
using Titano.Data.Repositories;
namespace Titano.App.Archiving;
/// <summary>A bar of a chart: label, magnitude, the text after the bar and the hover text.</summary>
public sealed record BarValue(string Label, double Value, string ValueText, string ToolTip);
/// <summary>A heatmap row (one year): photos per month, NaN for months still to come.</summary>
public sealed record HeatRow(string Label, IReadOnlyList<double> Values, IReadOnlyList<string?> ToolTips);
/// <summary>A figure tile of the Archive screen ("618,204" over "photos").</summary>
public sealed record ArchiveTile(string Value, string Caption);
public enum HealthAction
{
None,
Verify,
Proxies,
Analyse,
Duplicates,
Orphans,
}
/// <summary>One line of "Index health": ok or warning, what it found, and the fix it offers.</summary>
public sealed record HealthLine(bool IsOk, string Text, HealthAction Action);
/// <summary>
/// Turns the index statistics into the Archive screen's figures (BRIEF §12.5, Archive.html): tiles, photos per month
/// (heatmap, year rows × 12 months), storage by year, the top cameras and formats by photos (the rest folded into one
/// bar), and the index health lines.
/// </summary>
public static class ArchiveFigures
{
public const int TopCameras = 5;
public const int TopFormats = 6;
public static IReadOnlyList<ArchiveTile> Tiles(ArchiveStats stats)
{
var years = stats.ByYear.Select(y => y.Year).ToList();
var range = years.Count == 0 ? "—"
: years.Min() == years.Max() ? Year(years.Min())
: Loc.L("Archive.Tile.YearRange", Year(years.Min()), Year(years.Max()));
var cameras = stats.ByCamera.Count(c => !string.IsNullOrWhiteSpace(c.Model) || !string.IsNullOrWhiteSpace(c.Make));
return
[
new(Formatting.Count(stats.Photos), Loc.Plural("Archive.Tile.Photos", stats.Photos)),
new(Formatting.Count(stats.Sequences), Loc.Plural("Archive.Tile.Sequences", stats.Sequences)),
new(Formatting.Bytes(stats.Bytes), Loc.L("Archive.Tile.Originals")),
new(Formatting.Count(cameras), Loc.Plural("Archive.Tile.Cameras", cameras) + " · " + Loc.Plural("Archive.Tile.Formats", stats.ByFormat.Count)),
new(range, Loc.L("Archive.Tile.Pending", Formatting.Count(stats.Health.NotAnalyzed), Formatting.Count(stats.Health.Unverified))),
];
}
/// <summary>Culture month initials for the heatmap columns ("J F M …", "G F M …").</summary>
public static IReadOnlyList<string> MonthLabels() =>
[.. Loc.Culture.DateTimeFormat.AbbreviatedMonthNames.Take(12).Select(m => m.Length == 0 ? string.Empty : char.ToUpper(m[0], Loc.Culture).ToString())];
/// <summary>One row per year from the first to the last with photos, oldest first; months after <paramref name="today"/> are NaN.</summary>
public static IReadOnlyList<HeatRow> PhotosPerMonth(ArchiveStats stats, DateTime today)
{
if (stats.ByMonth.Count == 0) return [];
var byKey = stats.ByMonth.ToDictionary(m => (m.Year, m.Month));
var first = stats.ByMonth.Min(m => m.Year);
var last = System.Math.Max(stats.ByMonth.Max(m => m.Year), first);
var rows = new List<HeatRow>(last - first + 1);
for (var year = first; year <= last; year++)
{
var values = new double[12];
var tips = new string?[12];
for (var month = 1; month <= 12; month++)
{
var future = year > today.Year || (year == today.Year && month > today.Month);
byKey.TryGetValue((year, month), out var bucket);
values[month - 1] = future ? double.NaN : bucket?.Photos ?? 0;
tips[month - 1] = future ? null : Loc.L("Archive.Chart.MonthTip", new DateTime(year, month, 1).ToString("Y", Loc.Culture),
Loc.Plural("Shell.Status.Photos", bucket?.Photos ?? 0), Loc.Plural("Shell.Status.Sequences", bucket?.Sequences ?? 0));
}
rows.Add(new HeatRow(Year(year), values, tips));
}
return rows;
}
/// <summary>Bytes of originals per capture year, oldest first.</summary>
public static IReadOnlyList<BarValue> StorageByYear(ArchiveStats stats) =>
[
.. stats.ByYear.OrderBy(y => y.Year).Select(y => new BarValue(Year(y.Year), y.Bytes, Formatting.Bytes(y.Bytes),
Loc.L("Archive.Chart.YearTip", Year(y.Year), Formatting.Bytes(y.Bytes), Loc.Plural("Shell.Status.Sequences", y.Sequences)))),
];
/// <summary>Photos per camera: the <see cref="TopCameras"/> largest, then "n others".</summary>
public static IReadOnlyList<BarValue> ByCamera(ArchiveStats stats)
{
var cameras = stats.ByCamera
.GroupBy(c => PathTemplate.CameraName(c.Make, c.Model) is { Length: > 0 } name ? name : Loc.L("Archive.Chart.UnknownCamera"), StringComparer.Ordinal)
.Select(g => (Name: g.Key, Photos: g.Sum(c => c.Photos)))
.OrderByDescending(c => c.Photos).ThenBy(c => c.Name, StringComparer.CurrentCultureIgnoreCase)
.ToList();
return Shares(cameras, TopCameras, rest => Loc.Plural("Archive.Chart.OtherCameras", rest), stats.Photos);
}
/// <summary>Photos per file format: the <see cref="TopFormats"/> largest, then "Other".</summary>
public static IReadOnlyList<BarValue> ByFormat(ArchiveStats stats)
{
var formats = stats.ByFormat
.GroupBy(f => FormatName(f.Format), StringComparer.Ordinal)
.Select(g => (Name: g.Key, Photos: g.Sum(f => f.Photos)))
.OrderByDescending(f => f.Photos).ThenBy(f => f.Name, StringComparer.Ordinal)
.ToList();
return Shares(formats, TopFormats, _ => Loc.L("Archive.Chart.OtherFormats"), formats.Sum(f => f.Photos));
}
/// <summary>"CR2", "JPEG", "3FR": the format as photographers name it.</summary>
public static string FormatName(ImageFileFormat format) => format switch
{
ImageFileFormat.Unknown => Loc.L("Archive.Chart.OtherFormats"),
ImageFileFormat.ThreeFr => "3FR",
ImageFileFormat.JpegXl => "JXL",
ImageFileFormat.JpegXr => "JXR",
ImageFileFormat.WebP => "WebP",
_ => format.ToString().ToUpperInvariant(),
};
/// <summary>Index health (Archive.html): what needs attention, each with its fix.</summary>
public static IReadOnlyList<HealthLine> Health(ArchiveStats stats, int withoutProxies)
{
var h = stats.Health;
var damaged = h.MissingFrames + h.CorruptFrames;
return
[
h.Unverified == 0
? new HealthLine(true, Loc.L("Archive.Health.AllVerified"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.Unverified", h.Unverified), HealthAction.Verify),
damaged == 0
? new HealthLine(true, Loc.L("Archive.Health.FramesOk"), HealthAction.None)
: new HealthLine(false, Loc.L("Archive.Health.Damaged", Formatting.Count(h.MissingFrames), Formatting.Count(h.CorruptFrames)), HealthAction.Orphans),
withoutProxies == 0
? new HealthLine(true, Loc.L("Archive.Health.ProxiesOk"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.NoProxies", withoutProxies), HealthAction.Proxies),
h.NotAnalyzed == 0
? new HealthLine(true, Loc.L("Archive.Health.AnalysedOk"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.NotAnalysed", h.NotAnalyzed), HealthAction.Analyse),
h.DuplicateFrames == 0
? new HealthLine(true, Loc.L("Archive.Health.NoDuplicates"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.Duplicates", h.DuplicateFrames), HealthAction.Duplicates),
];
}
private static IReadOnlyList<BarValue> Shares(List<(string Name, long Photos)> ranked, int top, Func<int, string> restLabel, long total)
{
var bars = new List<BarValue>(top + 1);
foreach (var (name, photos) in ranked.Take(top)) bars.Add(Bar(name, photos, total));
if (ranked.Count > top) bars.Add(Bar(restLabel(ranked.Count - top), ranked.Skip(top).Sum(c => c.Photos), total));
return bars;
}
private static BarValue Bar(string label, long photos, long total)
{
var share = Formatting.Percent(total > 0 ? photos / (double)total : 0);
return new BarValue(label, photos, share, Loc.L("Archive.Chart.ShareTip", label, Loc.Plural("Shell.Status.Photos", photos), share));
}
private static string Year(int year) => year.ToString(CultureInfo.InvariantCulture);
}