public async Task SerializedWithoutIndentation() { var newData = new DownloadData(); newData.SetDownloadCount("nuget.versioning", "1.0.0", 1); newData.SetDownloadCount("NuGet.Versioning", "2.0.0", 5); newData.SetDownloadCount("EntityFramework", "3.0.0", 10); await Target.ReplaceLatestIndexedAsync(newData, AccessCondition.Object); var json = Assert.Single(SavedStrings); Assert.DoesNotContain("\n", json); }
public async Task Serializes() { var newData = new DownloadData(); newData.SetDownloadCount("ZZZ", "9.0.0", 23); newData.SetDownloadCount("YYY", "9.0.0", 0); newData.SetDownloadCount("nuget.versioning", "1.0.0", 1); newData.SetDownloadCount("NuGet.Versioning", "2.0.0", 5); newData.SetDownloadCount("EntityFramework", "3.0.0", 10); newData.SetDownloadCount("EntityFramework", "1.0.0", 0); await Target.ReplaceLatestIndexedAsync(newData, AccessCondition.Object); // Pretty-ify and sort the JSON to make the assertion clearer. var json = Assert.Single(SavedStrings); var dictionary = JsonConvert.DeserializeObject <SortedDictionary <string, SortedDictionary <string, int> > >(json); json = JsonConvert.SerializeObject(dictionary, Formatting.Indented); Assert.Equal(@"{ ""EntityFramework"": { ""3.0.0"": 10 }, ""NuGet.Versioning"": { ""1.0.0"": 1, ""2.0.0"": 5 }, ""ZZZ"": { ""9.0.0"": 23 } }", json); }
public async Task <AuxiliaryFileResult <DownloadData> > ReadLatestIndexedAsync( IAccessCondition accessCondition, StringCache stringCache) { var stopwatch = Stopwatch.StartNew(); var blobName = GetLatestIndexedBlobName(); var blobReference = Container.GetBlobReference(blobName); _logger.LogInformation("Reading the latest indexed downloads from {BlobName}.", blobName); bool modified; var downloads = new DownloadData(); AuxiliaryFileMetadata metadata; try { using (var stream = await blobReference.OpenReadAsync(accessCondition)) { ReadStream( stream, (id, version, downloadCount) => { id = stringCache.Dedupe(id); version = stringCache.Dedupe(version); downloads.SetDownloadCount(id, version, downloadCount); }); modified = true; metadata = new AuxiliaryFileMetadata( lastModified: new DateTimeOffset(blobReference.LastModifiedUtc, TimeSpan.Zero), loadDuration: stopwatch.Elapsed, fileSize: blobReference.Properties.Length, etag: blobReference.ETag); } } catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotModified) { _logger.LogInformation("The blob {BlobName} has not changed.", blobName); modified = false; downloads = null; metadata = null; } stopwatch.Stop(); _telemetryService.TrackReadLatestIndexedDownloads(downloads?.Count, modified, stopwatch.Elapsed); return(new AuxiliaryFileResult <DownloadData>( modified, downloads, metadata)); }
public static DownloadData ApplyDownloadOverrides( this DownloadData originalData, IReadOnlyDictionary <string, long> downloadOverrides, ILogger logger) { if (originalData == null) { throw new ArgumentNullException(nameof(originalData)); } if (downloadOverrides == null) { throw new ArgumentNullException(nameof(downloadOverrides)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } // Create a copy of the original data and apply overrides as we copy. var result = new DownloadData(); foreach (var downloadData in originalData) { var packageId = downloadData.Key; if (ShouldOverrideDownloads(packageId)) { logger.LogInformation( "Overriding downloads of package {PackageId} from {Downloads} to {DownloadsOverride}", packageId, originalData.GetDownloadCount(packageId), downloadOverrides[packageId]); var versions = downloadData.Value.Keys; result.SetDownloadCount( packageId, versions.First(), downloadOverrides[packageId]); } else { foreach (var versionData in downloadData.Value) { result.SetDownloadCount(downloadData.Key, versionData.Key, versionData.Value); } } } bool ShouldOverrideDownloads(string packageId) { if (!downloadOverrides.TryGetValue(packageId, out var downloadOverride)) { return(false); } // Apply the downloads override only if the package has fewer total downloads. // In effect, this removes a package's manual boost once its total downloads exceed the override. if (originalData[packageId].Total >= downloadOverride) { logger.LogInformation( "Skipping download override for package {PackageId} as its downloads of {Downloads} are " + "greater than its override of {DownloadsOverride}", packageId, originalData[packageId].Total, downloadOverride); return(false); } return(true); } return(result); }