| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Crawling; |
| | | 5 | | |
| | | 6 | | /// <summary>Duplicate content found by one config's crawl.</summary> |
| | | 7 | | /// <param name="CopiesWithin">Second copies of stored content at another path in the crawled config.</param> |
| | | 8 | | /// <param name="PairsAcross">Pairs of the same content in the crawled config and another of the user's configs.</param> |
| | | 9 | | public sealed record DuplicateCounts(int CopiesWithin, int PairsAcross); |
| | | 10 | | |
| | | 11 | | internal interface ISharedContentReporter |
| | | 12 | | { |
| | | 13 | | Task<DuplicateCounts> ReportAsync( |
| | | 14 | | UserStorageConfig crawled, IReadOnlyCollection<UserStorageConfig> usersConfigs, int copiesWithin, CancellationTo |
| | | 15 | | } |
| | | 16 | | |
| | 11 | 17 | | internal sealed partial class SharedContentReporter( |
| | 11 | 18 | | IMediaAssetRepository repository, |
| | 11 | 19 | | ILogger<SharedContentReporter> logger) : ISharedContentReporter |
| | | 20 | | { |
| | | 21 | | public async Task<DuplicateCounts> ReportAsync( |
| | | 22 | | UserStorageConfig crawled, IReadOnlyCollection<UserStorageConfig> usersConfigs, int copiesWithin, CancellationTo |
| | 10 | 23 | | { |
| | 10 | 24 | | var configIds = usersConfigs.Select(config => config.Id).Union([crawled.Id]).ToList(); |
| | 10 | 25 | | var locations = await repository.FindSharedContentAsync(configIds, ct); |
| | | 26 | | |
| | 10 | 27 | | var pairs = 0; |
| | 40 | 28 | | foreach (var content in locations.GroupBy(location => location.ContentHash)) |
| | 5 | 29 | | { |
| | 5 | 30 | | var places = content.OrderBy(location => location.StorageConfigId).ToList(); |
| | 5 | 31 | | var crawledPairs = places |
| | 5 | 32 | | .SelectMany((first, index) => places.Skip(index + 1).Select(second => (first, second))) |
| | 5 | 33 | | .Where(pair => pair.first.StorageConfigId == crawled.Id || pair.second.StorageConfigId == crawled.Id); |
| | 27 | 34 | | foreach (var (first, second) in crawledPairs) |
| | 6 | 35 | | { |
| | 6 | 36 | | Log.SharedContent( |
| | 6 | 37 | | logger, first.ContentHash, first.UserId, |
| | 6 | 38 | | first.FilePath, first.StorageConfigId, second.FilePath, second.StorageConfigId); |
| | 6 | 39 | | pairs++; |
| | 6 | 40 | | } |
| | 5 | 41 | | } |
| | | 42 | | |
| | 10 | 43 | | if (copiesWithin + pairs > 0) |
| | 6 | 44 | | Log.Summary(logger, crawled.Id, crawled.UserId, copiesWithin, pairs); |
| | 10 | 45 | | return new DuplicateCounts(copiesWithin, pairs); |
| | 10 | 46 | | } |
| | | 47 | | |
| | | 48 | | private static partial class Log |
| | | 49 | | { |
| | | 50 | | [LoggerMessage( |
| | | 51 | | Level = LogLevel.Debug, |
| | | 52 | | Message = "Duplicate content {ContentHash} for user {UserId}: {FilePath} in storage config {StorageConfigId} |
| | | 53 | | + "and {OtherFilePath} in storage config {OtherStorageConfigId}.")] |
| | | 54 | | public static partial void SharedContent( |
| | | 55 | | ILogger logger, string contentHash, Guid userId, |
| | | 56 | | string filePath, Guid storageConfigId, string otherFilePath, Guid otherStorageConfigId); |
| | | 57 | | |
| | | 58 | | [LoggerMessage( |
| | | 59 | | Level = LogLevel.Information, |
| | | 60 | | Message = "Duplicate content in storage config {StorageConfigId} of user {UserId}: copies within it: " |
| | | 61 | | + "{WithinConfigCopies}, pairs with the user's other storage configs: {AcrossConfigPairs}.")] |
| | | 62 | | public static partial void Summary( |
| | | 63 | | ILogger logger, Guid storageConfigId, Guid userId, int withinConfigCopies, int acrossConfigPairs); |
| | | 64 | | } |
| | | 65 | | } |