public Facts(ITestOutputHelper output)
            {
                CloudBlobClient    = new Mock <ICloudBlobClient>();
                CloudBlobContainer = new Mock <ICloudBlobContainer>();
                CloudBlob          = new Mock <ISimpleCloudBlob>();
                Options            = new Mock <IOptionsSnapshot <AzureSearchJobConfiguration> >();
                TelemetryService   = new Mock <IAzureSearchTelemetryService>();
                Logger             = output.GetLogger <PopularityTransferDataClient>();
                Config             = new AzureSearchJobConfiguration
                {
                    StorageContainer = "unit-test-container",
                };

                ETag            = "\"some-etag\"";
                AccessCondition = new Mock <IAccessCondition>();
                StringCache     = new StringCache();
                ReplaceLatestIndexedPopularityTransfersDurationMetric = new Mock <IDisposable>();

                Options
                .Setup(x => x.Value)
                .Returns(() => Config);
                CloudBlobClient
                .Setup(x => x.GetContainerReference(It.IsAny <string>()))
                .Returns(() => CloudBlobContainer.Object);
                CloudBlobContainer
                .Setup(x => x.GetBlobReference(It.IsAny <string>()))
                .Returns(() => CloudBlob.Object)
                .Callback <string>(x => BlobNames.Add(x));
                CloudBlob
                .Setup(x => x.ETag)
                .Returns(ETag);
                CloudBlob
                .Setup(x => x.OpenWriteAsync(It.IsAny <AccessCondition>()))
                .ReturnsAsync(() => new RecordingStream(bytes =>
                {
                    SavedBytes.Add(bytes);
                    SavedStrings.Add(Encoding.UTF8.GetString(bytes));
                }));
                CloudBlob
                .Setup(x => x.Properties)
                .Returns(new CloudBlockBlob(new Uri("https://example/blob")).Properties);

                TelemetryService
                .Setup(x => x.TrackReplaceLatestIndexedPopularityTransfers(It.IsAny <int>()))
                .Returns(ReplaceLatestIndexedPopularityTransfersDurationMetric.Object);

                Target = new PopularityTransferDataClient(
                    CloudBlobClient.Object,
                    Options.Object,
                    TelemetryService.Object,
                    Logger);
            }
        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));
        }
Exemple #3
0
        public async Task <AuxiliaryFileResult <HashSet <string> > > ReadLatestAsync(
            IAccessCondition accessCondition,
            StringCache stringCache)
        {
            var stopwatch     = Stopwatch.StartNew();
            var blobName      = GetLatestIndexedBlobName();
            var blobReference = Container.GetBlobReference(blobName);

            _logger.LogInformation("Reading the latest verified packages from {BlobName}.", blobName);

            bool modified;
            var  data = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            AuxiliaryFileMetadata metadata;

            try
            {
                using (var stream = await blobReference.OpenReadAsync(accessCondition))
                {
                    ReadStream(stream, id => data.Add(stringCache.Dedupe(id)));
                    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;
                data     = null;
                metadata = null;
            }

            stopwatch.Stop();
            _telemetryService.TrackReadLatestVerifiedPackages(data?.Count, modified, stopwatch.Elapsed);

            return(new AuxiliaryFileResult <HashSet <string> >(
                       modified,
                       data,
                       metadata));
        }
 public Facts()
 {
     Target = new StringCache();
 }