public async Task<StatisticsPackagesReport> GetPackageDownloadsByVersion(string packageId)
        {
            try
            {
                if (string.IsNullOrEmpty(packageId))
                {
                    return null;
                }

                var reportName = string.Format(CultureInfo.CurrentCulture, RecentpopularityDetailBlobNameFormat,
                    StatisticsReportName.RecentPopularityDetail_, packageId).ToLowerInvariant();
                var reportContent = await _reportService.Load(reportName);

                if (reportContent == null)
                {
                    return null;
                }

                var content = JObject.Parse(reportContent.Content);
                var report = new StatisticsPackagesReport
                {
                    LastUpdatedUtc = reportContent.LastUpdatedUtc
                };

                report.Facts = CreateFacts(content);

                return report;
            }
            catch (StatisticsReportNotFoundException)
            {
                //do no logging and just return null. Since this exception will thrown for all packages which doesn't have downloads in last 6 weeks, we don't
                //want to flood the elmah logs.
                return null;
            }
            catch (NullReferenceException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (JsonReaderException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (StorageException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (ArgumentException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
        }
        public async Task<StatisticsPackagesReport> GetPackageVersionDownloadsByClient(string packageId, string packageVersion)
        {
            try
            {
                if (string.IsNullOrEmpty(packageId) || string.IsNullOrEmpty(packageVersion))
                {
                    return null;
                }

                var reportName = string.Format(CultureInfo.CurrentCulture, RecentpopularityDetailBlobNameFormat, 
                    StatisticsReportName.RecentPopularityDetail_, packageId).ToLowerInvariant();
                var reportContent = await _reportService.Load(reportName);
                if (reportContent == null)
                {
                    return null;
                }

                var content = JObject.Parse(reportContent.Content);
                var report = new StatisticsPackagesReport
                {
                    LastUpdatedUtc = reportContent.LastUpdatedUtc
                };

                var facts = new List<StatisticsFact>();
                foreach (var fact in CreateFacts(content))
                {
                    if (fact.Dimensions["Version"] == packageVersion)
                    {
                        facts.Add(fact);
                    }
                }

                report.Facts = facts;

                return report;
            }
            catch (NullReferenceException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (JsonReaderException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (StorageException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (ArgumentException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
        }
        private void ProcessReport(StatisticsPackagesReport report, string[] groupby, string[] dimensions, string id, CultureInfo clientCulture)
        {
            if (report == null)
            {
                return;
            }

            string[] pivot = new string[4];

            if (groupby != null)
            {
                //  process and validate the groupby query. unrecognized fields are ignored. others fields regarded for existance

                int dim = 0;

                foreach (string dimension in dimensions)
                {
                    CheckGroupBy(groupby, dimension, pivot, ref dim, report);
                }

                if (dim == 0)
                {
                    // no recognized fields so just fall into the null logic

                    groupby = null;
                }
                else
                {
                    // the pivot array is used as the Columns in the report so we resize because this was the final set of columns 

                    Array.Resize(ref pivot, dim);
                }

                Tuple<StatisticsPivot.TableEntry[][], string> result = StatisticsPivot.GroupBy(report.Facts, pivot, clientCulture);

                if (id != null)
                {
                    int col = Array.FindIndex(pivot, (s) => s.Equals("Version", StringComparison.Ordinal));
                    if (col >= 0)
                    {
                        for (int row = 0; row < result.Item1.GetLength(0); row++)
                        {
                            StatisticsPivot.TableEntry entry = result.Item1[row][col];
                            if (entry != null)
                            {
                                entry.Uri = Url.Package(id, entry.Data);
                            }
                        }
                    }
                }

                report.Table = result.Item1;
                report.Total = result.Item2;
                report.Columns = pivot.Select(GetDimensionDisplayName);
            }

            if (groupby == null)
            {
                //  degenerate case (but still logically valid)

                foreach (string dimension in dimensions)
                {
                    report.Dimensions.Add(new StatisticsDimension { Value = dimension, DisplayName = GetDimensionDisplayName(dimension), IsChecked = false });
                }

                report.Table = null;
                report.Total = report.Facts.Sum(fact => fact.Amount).ToString("n0", clientCulture);
            }
        }
 private static void CheckGroupBy(string[] groupby, string name, string[] pivot, ref int dimension, StatisticsPackagesReport report)
 {
     if (Array.Exists(groupby, (s) => s.Equals(name, StringComparison.OrdinalIgnoreCase)))
     {
         pivot[dimension++] = name;
         report.Dimensions.Add(new StatisticsDimension { Value = name, DisplayName = GetDimensionDisplayName(name), IsChecked = true });
     }
     else
     {
         report.Dimensions.Add(new StatisticsDimension { Value = name, DisplayName = GetDimensionDisplayName(name), IsChecked = false });
     }
 }
 public void SetPackageDownloadsByVersion(string packageId, StatisticsPackagesReport report)
 {
     PackageId = packageId;
     Report = report;
 }
 public void SetPackageVersionDownloadsByClient(string packageId, string packageVersion, StatisticsPackagesReport report)
 {
     PackageId = packageId;
     PackageVersion = packageVersion;
     Report = report;
 }
        public async Task<StatisticsPackagesReport> GetPackageVersionDownloadsByClient(string packageId, string packageVersion)
        {
            try
            {
                if (string.IsNullOrEmpty(packageId) || string.IsNullOrEmpty(packageVersion))
                {
                    return null;
                }

                string reportName = string.Format(CultureInfo.CurrentCulture, "{0}{1}.json", Reports.RecentPopularityDetail_, packageId);

                reportName = reportName.ToLowerInvariant();

                string json = await _reportService.Load(reportName);

                if (json == null)
                {
                    return null;
                }

                JObject content = JObject.Parse(json);

                StatisticsPackagesReport report = new StatisticsPackagesReport();

                IList<StatisticsFact> facts = new List<StatisticsFact>();

                foreach (StatisticsFact fact in CreateFacts(content))
                {
                    if (fact.Dimensions["Version"] == packageVersion)
                    {
                        facts.Add(fact);
                    }
                }

                report.Facts = facts;

                return report;
            }
            catch (NullReferenceException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (JsonReaderException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (StorageException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (ArgumentException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
        }
        public async Task<StatisticsPackagesReport> GetPackageDownloadsByVersion(string packageId)
        {
            try
            {
                if (string.IsNullOrEmpty(packageId))
                {
                    return null;
                }

                string reportName = string.Format(CultureInfo.CurrentCulture, "{0}{1}.json", Reports.RecentPopularityDetail_, packageId);

                reportName = reportName.ToLowerInvariant();

                string json = await _reportService.Load(reportName);

                if (json == null)
                {
                    return null;
                }

                JObject content = JObject.Parse(json);

                StatisticsPackagesReport report = new StatisticsPackagesReport();

                report.Facts = CreateFacts(content);

                return report;
            }
            catch (JsonReaderException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (StorageException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
            catch (ArgumentException e)
            {
                QuietLog.LogHandledException(e);
                return null;
            }
        }
        private void ProcessReport(StatisticsPackagesReport report, string[] groupby, string[] dimensions, string id)
        {
            if (report == null)
            {
                return;
            }

            var pivot = new string[4];

            if (groupby != null)
            {
                // process and validate the groupby query. unrecognized fields are ignored. others fields regarded for existence
                var dim = 0;

                foreach (var dimension in dimensions)
                {
                    CheckGroupBy(groupby, dimension, pivot, ref dim, report);
                }

                if (dim == 0)
                {
                    // no recognized fields so just fall into the null logic
                    groupby = null;
                }
                else
                {
                    // the pivot array is used as the Columns in the report so we resize because this was the final set of columns
                    Array.Resize(ref pivot, dim);
                }
            }

            if (groupby != null)
            {
                Tuple <StatisticsPivot.TableEntry[][], int> result = StatisticsPivot.GroupBy(report.Facts, pivot);

                if (id != null)
                {
                    var col = Array.FindIndex(pivot, s => s.Equals("Version", StringComparison.Ordinal));
                    if (col >= 0)
                    {
                        for (var row = 0; row < result.Item1.GetLength(0); row++)
                        {
                            var entry = result.Item1[row][col];
                            if (entry != null)
                            {
                                entry.Uri = Url.Package(id, entry.Data);
                            }
                        }
                    }
                }

                // We do this here to try to order the result by the Version if available.
                // Since Version might not be available, don't sort if it isn't.
                // If Version is available, we need the following empty version rows to be moved with it (rowspan)
                NuGetVersion prevVersion = null;
                report.Table = result.Item1
                               .Select(e =>
                {
                    if (NuGetVersion.TryParse(e[0]?.Data, out NuGetVersion versionOut))
                    {
                        prevVersion = versionOut;
                        return(new { version = versionOut, e });
                    }

                    return(new { version = prevVersion, e });
                })
                               .OrderByDescending(e => e.version)
                               .Select(e => e.e).ToList();
                report.Total   = result.Item2;
                report.Columns = pivot.Select(GetDimensionDisplayName);
            }

            if (groupby == null)
            {
                //  degenerate case (but still logically valid)

                foreach (string dimension in dimensions)
                {
                    if (!report.Dimensions.Any(d => d.Value == dimension))
                    {
                        report.Dimensions.Add(new StatisticsDimension
                        {
                            Value       = dimension,
                            DisplayName = GetDimensionDisplayName(dimension),
                            IsChecked   = false
                        });
                    }
                }

                report.Table = null;
                report.Total = report.Facts.Sum(fact => fact.Amount);
            }
        }
Beispiel #10
0
 private static void CheckGroupBy(string[] groupby, string name, string[] pivot, ref int dimension, StatisticsPackagesReport report)
 {
     if (Array.Exists(groupby, s => s.Equals(name, StringComparison.OrdinalIgnoreCase)))
     {
         pivot[dimension++] = name;
         report.Dimensions.Add(new StatisticsDimension {
             Value = name, DisplayName = GetDimensionDisplayName(name), IsChecked = true
         });
     }
     else
     {
         report.Dimensions.Add(new StatisticsDimension {
             Value = name, DisplayName = GetDimensionDisplayName(name), IsChecked = false
         });
     }
 }
Beispiel #11
0
        private void ProcessReport(StatisticsPackagesReport report, string[] groupby, string[] dimensions, string id)
        {
            if (report == null)
            {
                return;
            }

            var pivot = new string[4];

            if (groupby != null)
            {
                // process and validate the groupby query. unrecognized fields are ignored. others fields regarded for existence
                var dim = 0;

                foreach (var dimension in dimensions)
                {
                    CheckGroupBy(groupby, dimension, pivot, ref dim, report);
                }

                if (dim == 0)
                {
                    // no recognized fields so just fall into the null logic
                    groupby = null;
                }
                else
                {
                    // the pivot array is used as the Columns in the report so we resize because this was the final set of columns
                    Array.Resize(ref pivot, dim);
                }
            }

            if (groupby != null)
            {
                Tuple <StatisticsPivot.TableEntry[][], string> result = StatisticsPivot.GroupBy(report.Facts, pivot);

                if (id != null)
                {
                    var col = Array.FindIndex(pivot, s => s.Equals("Version", StringComparison.Ordinal));
                    if (col >= 0)
                    {
                        for (var row = 0; row < result.Item1.GetLength(0); row++)
                        {
                            var entry = result.Item1[row][col];
                            if (entry != null)
                            {
                                entry.Uri = Url.Package(id, entry.Data);
                            }
                        }
                    }
                }

                report.Table   = result.Item1;
                report.Total   = result.Item2;
                report.Columns = pivot.Select(GetDimensionDisplayName);
            }

            if (groupby == null)
            {
                //  degenerate case (but still logically valid)

                foreach (string dimension in dimensions)
                {
                    if (!report.Dimensions.Any(d => d.Value == dimension))
                    {
                        report.Dimensions.Add(new StatisticsDimension
                        {
                            Value       = dimension,
                            DisplayName = GetDimensionDisplayName(dimension),
                            IsChecked   = false
                        });
                    }
                }

                report.Table = null;
                report.Total = report.Facts.Sum(fact => fact.Amount).ToNuGetNumberString();
            }
        }