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; /// A bar of a chart: label, magnitude, the text after the bar and the hover text. public sealed record BarValue(string Label, double Value, string ValueText, string ToolTip); /// A heatmap row (one year): photos per month, NaN for months still to come. public sealed record HeatRow(string Label, IReadOnlyList Values, IReadOnlyList ToolTips); /// A figure tile of the Archive screen ("618,204" over "photos"). public sealed record ArchiveTile(string Value, string Caption); public enum HealthAction { None, Verify, Proxies, Analyse, Duplicates, Orphans, } /// One line of "Index health": ok or warning, what it found, and the fix it offers. public sealed record HealthLine(bool IsOk, string Text, HealthAction Action); /// /// 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. /// public static class ArchiveFigures { public const int TopCameras = 5; public const int TopFormats = 6; public static IReadOnlyList 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))), ]; } /// Culture month initials for the heatmap columns ("J F M …", "G F M …"). public static IReadOnlyList MonthLabels() => [.. Loc.Culture.DateTimeFormat.AbbreviatedMonthNames.Take(12).Select(m => m.Length == 0 ? string.Empty : char.ToUpper(m[0], Loc.Culture).ToString())]; /// One row per year from the first to the last with photos, oldest first; months after are NaN. public static IReadOnlyList 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(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; } /// Bytes of originals per capture year, oldest first. public static IReadOnlyList 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)))), ]; /// Photos per camera: the largest, then "n others". public static IReadOnlyList 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); } /// Photos per file format: the largest, then "Other". public static IReadOnlyList 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)); } /// "CR2", "JPEG", "3FR": the format as photographers name it. 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(), }; /// Index health (Archive.html): what needs attention, each with its fix. public static IReadOnlyList 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 Shares(List<(string Name, long Photos)> ranked, int top, Func restLabel, long total) { var bars = new List(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); }