Example #1
0
        public async Task Run()
        {
            // Gather download count data from statistics warehouse
            IReadOnlyCollection <ToolDownloadCountData> data;

            _logger.LogInformation("Gathering Tools Download Counts from {DataSource}/{InitialCatalog}...", StatisticsDatabase.DataSource, StatisticsDatabase.InitialCatalog);
            using (var connection = await StatisticsDatabase.ConnectTo())
                using (var transaction = connection.BeginTransaction(IsolationLevel.Snapshot))
                {
                    data = (await connection.QueryWithRetryAsync <ToolDownloadCountData>(
                                _storedProcedureName, commandType: CommandType.StoredProcedure, transaction: transaction, commandTimeout: _defaultCommandTimeout)).ToList();
                }

            _logger.LogInformation("Gathered {DownloadedRowsCount} rows of data.", data.Count);

            if (data.Any())
            {
                // Group based on Package Id
                var grouped       = data.GroupBy(p => p.ToolId);
                var registrations = new JArray();
                foreach (var group in grouped)
                {
                    var details = new JArray();
                    details.Add(group.Key);
                    foreach (var gv in group)
                    {
                        var version = new JArray(gv.ToolVersion, gv.TotalDownloadCount);
                        details.Add(version);
                    }
                    registrations.Add(details);
                }

                var reportText = registrations.ToString(Formatting.None);

                foreach (var storageContainerTarget in Targets)
                {
                    try
                    {
                        var targetBlobContainer = await GetBlobContainer(storageContainerTarget);

                        var blob = targetBlobContainer.GetBlockBlobReference(ReportName);
                        _logger.LogInformation("Writing report to {ReportUri}", blob.Uri.AbsoluteUri);
                        blob.Properties.ContentType = "application/json";
                        await blob.UploadTextAsync(reportText);

                        _logger.LogInformation("Wrote report to {ReportUri}", blob.Uri.AbsoluteUri);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError("Error writing report to storage account {StorageAccount}, container {ReportContainer}. {Exception}",
                                         storageContainerTarget.StorageAccount.Credentials.AccountName,
                                         storageContainerTarget.ContainerName,
                                         ex);
                    }
                }
            }
        }
        public async Task Run()
        {
            // gather package numbers from gallery database
            GalleryTotalsData totalsData;

            _logger.LogInformation("Gathering Gallery Totals from {GalleryDataSource}/{GalleryInitialCatalog}...", GalleryDatabase.DataSource, GalleryDatabase.InitialCatalog);
            using (var connection = await GalleryDatabase.ConnectTo())
                using (var transaction = connection.BeginTransaction(IsolationLevel.Snapshot))
                {
                    totalsData = (await connection.QueryWithRetryAsync <GalleryTotalsData>(
                                      GalleryQuery, commandType: CommandType.Text, transaction: transaction)).First();
                }
            _logger.LogInformation("Total packages: {TotalPackagesCount}", totalsData.TotalPackages);
            _logger.LogInformation("Unique packages: {UniquePackagesCount}", totalsData.UniquePackages);

            // gather download count data from statistics warehouse
            _logger.LogInformation("Gathering Gallery Totals from {StatisticsDataSource}/{StatisticsInitialCatalog}...", StatisticsDatabase.DataSource, StatisticsDatabase.InitialCatalog);
            using (var connection = await StatisticsDatabase.ConnectTo())
                using (var transaction = connection.BeginTransaction(IsolationLevel.Snapshot))
                {
                    totalsData.Downloads = (await connection.ExecuteScalarWithRetryAsync <long>(
                                                WarehouseStoredProcedureName,
                                                commandType: CommandType.StoredProcedure,
                                                commandTimeout: TimeSpan.FromMinutes(5),
                                                transaction: transaction));
                }
            _logger.LogInformation("Total downloads: {TotalDownloadsCount}", totalsData.Downloads);

            // write to blob
            totalsData.LastUpdateDateUtc = DateTime.UtcNow;

            var reportText = JsonConvert.SerializeObject(totalsData);

            foreach (var storageContainerTarget in Targets)
            {
                try
                {
                    var targetBlobContainer = await GetBlobContainer(storageContainerTarget);

                    var blob = targetBlobContainer.GetBlockBlobReference(ReportName);
                    _logger.LogInformation("Writing report to {ReportUri}", blob.Uri.AbsoluteUri);
                    blob.Properties.ContentType = "application/json";
                    await blob.UploadTextAsync(reportText);

                    _logger.LogInformation("Wrote report to {ReportUri}", blob.Uri.AbsoluteUri);
                }
                catch (Exception ex)
                {
                    _logger.LogError("Error writing report to storage account {StorageAccount}, container {ReportContainer}. {Exception}",
                                     storageContainerTarget.StorageAccount.Credentials.AccountName,
                                     storageContainerTarget.ContainerName,
                                     ex);
                }
            }
        }
        public async Task Run()
        {
            // Gather download count data from statistics warehouse
            IReadOnlyCollection <DownloadCountData> downloadData;

            Trace.TraceInformation("Gathering Download Counts from {0}/{1}...", StatisticsDatabase.DataSource, StatisticsDatabase.InitialCatalog);
            using (var connection = await StatisticsDatabase.ConnectTo())
                using (var transaction = connection.BeginTransaction(IsolationLevel.Snapshot))
                {
                    downloadData = (await connection.QueryWithRetryAsync <DownloadCountData>(
                                        _storedProcedureName, commandType: CommandType.StoredProcedure, transaction: transaction, commandTimeout: _defaultCommandTimeout)).ToList();
                }

            Trace.TraceInformation("Gathered {0} rows of data.", downloadData.Count);

            if (downloadData.Any())
            {
                SemanticVersion semanticVersion = null;

                // Group based on Package Id
                var grouped       = downloadData.GroupBy(p => p.PackageId);
                var registrations = new JArray();
                foreach (var group in grouped)
                {
                    var details = new JArray();
                    details.Add(group.Key);
                    foreach (var gv in group)
                    {
                        // downloads.v1.json should only contain normalized versions - ignore others
                        if (!string.IsNullOrEmpty(gv.PackageVersion) &&
                            SemanticVersion.TryParse(gv.PackageVersion, out semanticVersion) &&
                            gv.PackageVersion == semanticVersion.ToNormalizedString())
                        {
                            var version = new JArray(gv.PackageVersion, gv.TotalDownloadCount);
                            details.Add(version);
                        }
                    }
                    registrations.Add(details);
                }

                var reportText = registrations.ToString(Formatting.None);

                foreach (var storageContainerTarget in Targets)
                {
                    try
                    {
                        var targetBlobContainer = await GetBlobContainer(storageContainerTarget);

                        var blob = targetBlobContainer.GetBlockBlobReference(ReportName);
                        Trace.TraceInformation("Writing report to {0}", blob.Uri.AbsoluteUri);
                        blob.Properties.ContentType = "application/json";
                        await blob.UploadTextAsync(reportText);

                        Trace.TraceInformation("Wrote report to {0}", blob.Uri.AbsoluteUri);
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("Error writing report to storage account {0}, container {1}. {2} {3}",
                                         storageContainerTarget.StorageAccount.Credentials.AccountName,
                                         storageContainerTarget.ContainerName, ex.Message, ex.StackTrace);
                    }
                }
            }
        }