private async ValueTask <Stats.TrackerUrlCollection> GetOrCreateTrackerUrlCollectionAsync (ITorrentStatisticsRepository torrentStatisticsRepository, Dictionary <byte[], Stats.TrackerUrlCollection> trackerUrlCollectionsDict, Shared.TorrentClient.Torrent clientTorrent) { var trackerUrls = clientTorrent.TrackerUrls .Select(tu => tu.ToLower()) .OrderBy(tu => tu) .Distinct() .ToArray(); var hashString = string.Join(";", trackerUrls); var hash = _hasher.ComputeHash(Encoding.UTF8.GetBytes(hashString)); _logger.LogTrace("Retrieving or creating tracker url collection for torrent with hash {0}", clientTorrent.InfoHash); if (!trackerUrlCollectionsDict.TryGetValue(hash, out var trackerUrlCollection)) { _logger.LogDebug("Didn't find any tracker url collection for torrent with hash {0}, creating one (urls are: {1})", clientTorrent.InfoHash, trackerUrls); trackerUrlCollection = new Stats.TrackerUrlCollection() { CollectionHash = hash, TrackerUrls = trackerUrls.Select(tu => new Stats.TrackerUrl(tu)).ToList() }; await torrentStatisticsRepository.AddAsync(trackerUrlCollection); trackerUrlCollectionsDict[hash] = trackerUrlCollection; } else { _logger.LogTrace("Found tracker url collection for torrent with hash {0}", clientTorrent.InfoHash); } return(trackerUrlCollection); }
private static Stats.TorrentUploadDeltaSnapshot CreateTorrentUploadDeltaSnapshot(DateTime jobDateTime, Shared.TorrentClient.Torrent clientTorrent, Stats.Torrent persistedTorrent, Stats.TrackerUrlCollection trackerUrlCollection, long delta, long totalUpload) { return(new Stats.TorrentUploadDeltaSnapshot { DateTime = jobDateTime, TorrentId = persistedTorrent.Id, TotalUploadForThisTorrentInBytes = clientTorrent.TotalUploadInBytes, TrackerUrlCollection = trackerUrlCollection, TotalUploadInBytes = totalUpload, UploadDeltaSinceLastSnapshotInBytes = delta, }); }
private Stats.TorrentUploadDeltaSnapshot CreateUploadDeltaSnapshotForExistingTorrent(DateTime jobDateTime, Shared.TorrentClient.Torrent clientTorrent, Stats.Torrent persistedTorrent, Stats.TorrentUploadDeltaSnapshot lastUploadDeltaSnapshot, Stats.TrackerUrlCollection trackerUrlCollection) { var delta = clientTorrent.TotalUploadInBytes - lastUploadDeltaSnapshot.TotalUploadForThisTorrentInBytes; if (delta == 0) { _logger.LogDebug("Torrent with hash {0} has a delta of 0, skipping", clientTorrent.InfoHash); return(null); } if (delta < 0) { _logger.LogWarning("Upload delta for torrent with hash {0} is {1} which shouldn't happen, this could be a bug or mean that the torrent client db was restored. Truncating to 0", clientTorrent.InfoHash, delta); delta = 0; } var totalUpload = lastUploadDeltaSnapshot.TotalUploadInBytes + delta; return(CreateTorrentUploadDeltaSnapshot(jobDateTime, clientTorrent, persistedTorrent, trackerUrlCollection, delta, totalUpload)); }
private Stats.TorrentUploadDeltaSnapshot CreateNewUploadDeltaSnapshotForReAddedTorrent(DateTime jobDateTime, Shared.TorrentClient.Torrent clientTorrent, Stats.Torrent persistedTorrent, Stats.TorrentUploadDeltaSnapshot lastUploadDeltaSnapshot, Stats.TrackerUrlCollection trackerUrlCollection) { _logger.LogInformation("Torrent with hash {0} seems the be re-added", clientTorrent.InfoHash); persistedTorrent.LatestAddedDateTime = clientTorrent.AddedDateTime; var delta = clientTorrent.TotalUploadInBytes; var totalUpload = lastUploadDeltaSnapshot.TotalUploadInBytes + delta; return(CreateTorrentUploadDeltaSnapshot(jobDateTime, clientTorrent, persistedTorrent, trackerUrlCollection, delta, totalUpload)); }
private Stats.TorrentUploadDeltaSnapshot CreateUploadDeltaSnapshotForNewTorrent(DateTime jobDateTime, Shared.TorrentClient.Torrent clientTorrent, Stats.Torrent persistedTorrent, Stats.TrackerUrlCollection trackerUrlCollection) { _logger.LogDebug("Creating first upload delta snapshot for torrent with hash {0}", clientTorrent.InfoHash); return(CreateTorrentUploadDeltaSnapshot(jobDateTime, clientTorrent, persistedTorrent, trackerUrlCollection, delta: clientTorrent.TotalUploadInBytes, totalUpload: clientTorrent.TotalUploadInBytes)); }
/// <summary> /// Returns null if nothing relevant has changed /// </summary> private Stats.TorrentUploadDeltaSnapshot CreateNewUploadDeltaSnapshot(DateTime jobDateTime, Shared.TorrentClient.Torrent clientTorrent, Stats.Torrent persistedTorrent, Stats.TorrentUploadDeltaSnapshot lastUploadDeltaSnapshot, Stats.TrackerUrlCollection trackerUrlCollection) { _logger.LogDebug("Creating new upload delta snapshot for torrent with hash {0} and name '{1}' if there are any relevant changes", clientTorrent.InfoHash, clientTorrent.Name); if (lastUploadDeltaSnapshot == null) { return(CreateUploadDeltaSnapshotForNewTorrent(jobDateTime, clientTorrent, persistedTorrent, trackerUrlCollection)); } if (clientTorrent.AddedDateTime != persistedTorrent.LatestAddedDateTime) { return(CreateNewUploadDeltaSnapshotForReAddedTorrent(jobDateTime, clientTorrent, persistedTorrent, lastUploadDeltaSnapshot, trackerUrlCollection)); } else { return(CreateUploadDeltaSnapshotForExistingTorrent(jobDateTime, clientTorrent, persistedTorrent, lastUploadDeltaSnapshot, trackerUrlCollection)); } }