| | | 1 | | using Anichron.Core.Work; |
| | | 2 | | using Anichron.Worker.Ingestion; |
| | | 3 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 4 | | |
| | | 5 | | namespace Anichron.Worker.Crawling; |
| | | 6 | | |
| | | 7 | | /// <summary>A crawl's results summary. A Live Photo pair counts as one item.</summary> |
| | | 8 | | /// <param name="Found">Items the walk queued for ingestion.</param> |
| | | 9 | | /// <param name="Processed">Items handled without an error, unchanged ones included.</param> |
| | | 10 | | /// <param name="Failed">Items whose ingestion failed.</param> |
| | | 11 | | public sealed record CrawlResult(long Found, long Processed, long Failed) |
| | | 12 | | { |
| | | 13 | | /// <summary>Processed items whose content was read: new, edited, or restored with changed content.</summary> |
| | | 14 | | public long Ingested { get; init; } |
| | | 15 | | |
| | | 16 | | /// <summary>Files the walk reached and found no media type for; excluded folders and files are not counted.</summar |
| | | 17 | | public int SkippedUnsupported { get; init; } |
| | | 18 | | |
| | | 19 | | /// <summary>The duplicate report for the crawled config, or null when the report failed.</summary> |
| | | 20 | | public DuplicateCounts? Duplicates { get; init; } |
| | | 21 | | |
| | | 22 | | public ReconciliationCounts? Reconciliation { get; init; } |
| | | 23 | | |
| | | 24 | | /// <summary>Links the walk skipped, to files or folders: a crawl never follows a link.</summary> |
| | | 25 | | public int SkippedLinks { get; init; } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary>How a crawl brought the config's rows in line with its files.</summary> |
| | | 29 | | /// <param name="SoftDeleted">Rows soft-deleted, while ingesting or because the walk did not find their file.</param> |
| | | 30 | | /// <param name="AutoRestored">Soft-deleted rows made active again because the crawl found their file.</param> |
| | | 31 | | /// <param name="SkippedBecause">Why missing files were not looked for, or null when they were.</param> |
| | | 32 | | public sealed record ReconciliationCounts(int SoftDeleted, int AutoRestored, MissingFileSkip? SkippedBecause); |
| | | 33 | | |
| | | 34 | | /// <summary>What one crawl's walk and ingestion counted.</summary> |
| | | 35 | | /// <param name="Result">The crawl's results summary, before the duplicate report.</param> |
| | | 36 | | /// <param name="DuplicateCopies">Copies within the crawled config, counted by this crawl only.</param> |
| | | 37 | | internal sealed record CrawlRun(CrawlResult Result, int DuplicateCopies) |
| | | 38 | | { |
| | | 39 | | /// <summary>Rows soft-deleted while ingesting, because their path now holds other stored content.</summary> |
| | | 40 | | public int SoftDeleted { get; init; } |
| | | 41 | | |
| | | 42 | | public int AutoRestored { get; init; } |
| | | 43 | | |
| | | 44 | | /// <summary>Relative paths of every media file the walk queued, both halves of a Live Photo included.</summary> |
| | | 45 | | public IReadOnlySet<string> FoundPaths { get; init; } = new HashSet<string>(StringComparer.Ordinal); |
| | | 46 | | |
| | | 47 | | /// <summary>Whether the walk read every folder under the root.</summary> |
| | | 48 | | public bool WalkComplete { get; init; } |
| | | 49 | | } |
| | | 50 | | |
| | 42 | 51 | | internal sealed class CrawlTally(IWorkProgress progress) |
| | | 52 | | { |
| | 42 | 53 | | private readonly Lock gate = new(); |
| | 42 | 54 | | private readonly HashSet<string> foundPaths = new(StringComparer.Ordinal); |
| | | 55 | | private long found; |
| | | 56 | | private long processed; |
| | | 57 | | private long failed; |
| | | 58 | | private long ingested; |
| | | 59 | | private int duplicateCopies; |
| | | 60 | | private int softDeleted; |
| | | 61 | | private int autoRestored; |
| | | 62 | | private int skippedLinks; |
| | | 63 | | private int skippedUnsupported; |
| | | 64 | | private bool walked; |
| | | 65 | | private bool folderSkipped; |
| | | 66 | | |
| | | 67 | | public CrawlRun Run |
| | | 68 | | { |
| | | 69 | | get |
| | 39 | 70 | | { |
| | | 71 | | lock (gate) |
| | 39 | 72 | | { |
| | 39 | 73 | | var result = new CrawlResult(found, processed, failed) |
| | 39 | 74 | | { |
| | 39 | 75 | | Ingested = ingested, |
| | 39 | 76 | | SkippedLinks = skippedLinks, |
| | 39 | 77 | | SkippedUnsupported = skippedUnsupported, |
| | 39 | 78 | | }; |
| | 39 | 79 | | return new CrawlRun(result, duplicateCopies) |
| | 39 | 80 | | { |
| | 39 | 81 | | SoftDeleted = softDeleted, |
| | 39 | 82 | | AutoRestored = autoRestored, |
| | 39 | 83 | | FoundPaths = new HashSet<string>(foundPaths, StringComparer.Ordinal), |
| | 39 | 84 | | WalkComplete = walked && !folderSkipped, |
| | 39 | 85 | | }; |
| | | 86 | | } |
| | 39 | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | 68 | 90 | | public void Found(IngestionItem item) => Update(() => |
| | 68 | 91 | | { |
| | 68 | 92 | | found++; |
| | 68 | 93 | | foundPaths.Add(item.RelativePath); |
| | 68 | 94 | | if (item.SecondaryFile is { } secondary) |
| | 68 | 95 | | foundPaths.Add(secondary.RelativePath); |
| | 68 | 96 | | }); |
| | | 97 | | |
| | | 98 | | public void FolderSkipped() |
| | 2 | 99 | | { |
| | | 100 | | lock (gate) |
| | 2 | 101 | | folderSkipped = true; |
| | 2 | 102 | | } |
| | | 103 | | |
| | | 104 | | public void LinkSkipped() |
| | 5 | 105 | | { |
| | | 106 | | lock (gate) |
| | 5 | 107 | | skippedLinks++; |
| | 5 | 108 | | } |
| | | 109 | | |
| | | 110 | | public void UnsupportedSkipped() |
| | 8 | 111 | | { |
| | | 112 | | lock (gate) |
| | 8 | 113 | | skippedUnsupported++; |
| | 8 | 114 | | } |
| | | 115 | | |
| | 41 | 116 | | public void Walked() => Update(() => walked = true); |
| | | 117 | | |
| | 52 | 118 | | public void Processed(IngestionContext context) => Update(() => |
| | 52 | 119 | | { |
| | 52 | 120 | | processed++; |
| | 52 | 121 | | if (context.Classification?.NeedsProcessing == true) |
| | 52 | 122 | | ingested++; |
| | 52 | 123 | | duplicateCopies += context.Classification?.DuplicateCopies ?? 0; |
| | 52 | 124 | | softDeleted += context.SoftDeleted; |
| | 52 | 125 | | autoRestored += context.AutoRestored; |
| | 52 | 126 | | }); |
| | | 127 | | |
| | 12 | 128 | | public void Failed() => Update(() => failed++); |
| | | 129 | | |
| | | 130 | | private void Update(Action change) |
| | 173 | 131 | | { |
| | | 132 | | lock (gate) |
| | 173 | 133 | | { |
| | 173 | 134 | | change(); |
| | 173 | 135 | | progress.Report( |
| | 173 | 136 | | processed + failed, |
| | 173 | 137 | | walked ? found : null, |
| | 173 | 138 | | $"{found} found, {processed} processed, {failed} failed"); |
| | 173 | 139 | | } |
| | 173 | 140 | | } |
| | | 141 | | } |