Ejemplo n.º 1
0
        public IEnumerable <SimpleDataSet> GetAllData(string indexType)
        {
            var umbContxt = EnsureUmbracoContext();

            var projects = umbContxt.ContentCache.GetByXPath("//Community/Projects//Project [projectLive='1']").ToArray();

            var nugetService = new NugetPackageDownloadService();

            var nugetDownloads = nugetService.GetNugetPackageDownloads();

            var allProjectIds          = projects.Select(x => x.Id).ToArray();
            var allProjectKarma        = Utils.GetProjectTotalVotes();
            var allProjectWikiFiles    = WikiFile.CurrentFiles(allProjectIds);
            var allProjectDownloads    = Utils.GetProjectTotalPackageDownload();
            var allCompatVersions      = Utils.GetProjectCompatibleVersions();
            var mostRecentDownloadDate = WikiFile.GetMostRecentDownloadDate();

            // if most recent download date is MinValue then there is no download data to query
            var downloadStats = mostRecentDownloadDate == DateTime.MinValue
                ? new Dictionary <int, MonthlyProjectDownloads>()
                : WikiFile.GetMonthlyDownloadStatsByProject(mostRecentDownloadDate.Subtract(TimeSpan.FromDays(365)));

            foreach (var project in projects)
            {
                LogHelper.Debug(this.GetType(), "Indexing " + project.Name);

                var simpleDataSet = new SimpleDataSet {
                    NodeDefinition = new IndexedNode(), RowData = new Dictionary <string, string>()
                };

                var projectDownloads = allProjectDownloads.ContainsKey(project.Id) ? allProjectDownloads[project.Id] : 0;
                var projectKarma     = allProjectKarma.ContainsKey(project.Id) ? allProjectKarma[project.Id] : 0;
                var projectFiles     = allProjectWikiFiles.ContainsKey(project.Id) ? allProjectWikiFiles[project.Id].ToArray() : new WikiFile[] { };
                var projectVersions  = allCompatVersions.ContainsKey(project.Id) ? allCompatVersions[project.Id] : Enumerable.Empty <string>();

                var nugetPackageId = nugetService.GetNuGetPackageId(project);

                int?dailyNugetDownLoads = null;

                if (!string.IsNullOrWhiteSpace(nugetPackageId))
                {
                    var packageInfo = nugetDownloads.FirstOrDefault(x => x.PackageId == nugetPackageId);

                    if (packageInfo != null)
                    {
                        projectDownloads   += packageInfo.TotalDownLoads;
                        dailyNugetDownLoads = packageInfo.AverageDownloadPerDay;
                    }
                }

                yield return(MapProjectToSimpleDataIndexItem(
                                 downloadStats,
                                 mostRecentDownloadDate,
                                 project, simpleDataSet, indexType, projectKarma, projectFiles, projectDownloads, projectVersions, dailyNugetDownLoads));
            }
        }
        public ActionResult GetPackageInfos()
        {
            var packages = GetAllPackages();

            var packageInfos = new List <PackageInfo>();

            var nugetService          = new NugetPackageDownloadService();
            var nugetPackageDownloads = nugetService.GetNugetPackageDownloads();

            var licenses = new HashSet <string>();

            foreach (var package in packages)
            {
                var compatibleVersions = package.GetPropertyValue <string>("compatibleVersions");
                var categoryName       = string.Empty;
                var categoryNodeId     = package.GetPropertyValue <int>("category");
                if (categoryNodeId != 0)
                {
                    categoryName = Umbraco.TypedContent(categoryNodeId).Name;
                }

                var ownerName     = string.Empty;
                var ownerMemberId = package.GetPropertyValue <int>("owner");
                if (ownerMemberId != 0)
                {
                    ownerName = Umbraco.TypedMember(ownerMemberId).Name;
                }

                var ourDownloads     = Utils.GetProjectTotalDownloadCount(package.Id);
                var nugetPackageId   = nugetService.GetNuGetPackageId(package);
                var nugetDownloads   = 0;
                var nugetPackageInfo = nugetPackageDownloads.FirstOrDefault(x => x.PackageId == nugetPackageId);
                if (nugetPackageInfo != null)
                {
                    nugetDownloads = nugetPackageInfo.TotalDownLoads;
                }
                var lastUpdate = package.UpdateDate;
                // If files have been updated later than this then use that date
                var wikiFiles  = WikiFile.CurrentFiles(package.Id);
                var latestFile = wikiFiles.Where(x => x.FileType == "package").OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (latestFile != null && latestFile.CreateDate > lastUpdate)
                {
                    lastUpdate = latestFile.CreateDate;
                }

                var licenseName = package.GetPropertyValue <string>("licenseName");
                licenses.Add(licenseName);

                var openSource       = false;
                var openSourceChecks = new List <string>
                {
                    "MIT",
                    "CC",
                    "BSD",
                    "Apache",
                    "GPL",
                    "GNU",
                    "Free",
                    "Unlicense",
                    "MS-PL",
                    "OSL",
                    "MPL",
                    "Open Government",
                    "WTFPL",
                    // Questionable but.. ok
                    "TBD",
                    "Undefined",
                    "No license"
                };

                // These two match the above, but are not open source
                if (licenseName != "Resizer Freedom license" && licenseName != "MIT + Commercial")
                {
                    foreach (var item in openSourceChecks)
                    {
                        if (!licenseName.InvariantContains(item))
                        {
                            continue;
                        }

                        openSource = true;
                        // Immediately break out of foreach, no more checks needed
                        break;
                    }
                }

                var packageInfo = new PackageInfo
                {
                    Name     = package.Name,
                    Category = categoryName,
                    // We don't know.. look at the license maybe?
                    OpenSource         = openSource,
                    License            = licenseName,
                    LicenseUrl         = package.GetPropertyValue <string>("licenseUrl"),
                    Owner              = ownerName,
                    CloudCompatible    = package.GetPropertyValue <bool>("worksOnUaaS"),
                    DownloadsTotal     = ourDownloads + nugetDownloads,
                    LastUpdate         = lastUpdate.ToString("yyyy-MM-dd"),
                    Version7Compatible = compatibleVersions.InvariantContains("v7"),
                    Version8Compatible = compatibleVersions.InvariantContains("v8"),
                    Url = package.UrlWithDomain()
                };

                packageInfos.Add(packageInfo);
            }

            var json = JsonConvert.SerializeObject(packageInfos);
            var dt   = (DataTable)JsonConvert.DeserializeObject(json, typeof(DataTable));
            var csv  = DataTableToCsv(dt);

            var fileBytes = Encoding.UTF8.GetBytes(string.Join(Environment.NewLine, csv));
            var file      = File(fileBytes, "text/csv", "packaginfos.csv");

            return(file);
        }