Example #1
0
        public async Task <IActionResult> Index(string organizationName, string repositoryName, [FromQuery] Dictionary <string, List <string> > filters = null)
        {
            var model = new BrowseRepositoryViewModel()
            {
                RepositoryName   = repositoryName,
                OrganizationName = organizationName
            };

            this.telemetryClient.TrackViewRepository(organizationName, repositoryName);
            var queryParam     = new RepositoryQueryParameter(organizationName, repositoryName);
            var repositoryInfo = (await this.repositoryService.GetRepositories(queryParam)).FirstOrDefault();

            if (repositoryInfo == null)
            {
                this.TempData["Error"] = $"Did not find a repository [{repositoryName}] on [{organizationName}].";
                return(this.RedirectToAction("PageNotFound", "Home"));
            }

            var projectsTask      = this.repositoryService.GetAllCurrentProjects(repositoryInfo);
            var stampsTask        = this.repositoryService.GetStamps(repositoryInfo);
            var downloadStatsTask = this.statisticsService.GetDownloadStatistics(repositoryInfo);

            await Task.WhenAll(projectsTask, stampsTask, downloadStatsTask).ConfigureAwait(false);

            ManifestQueryResult result = projectsTask.Result;

            model.NumberOfStamps = stampsTask.Result.Count;
            model.RepositoryMode = result.Projects?.FirstOrDefault()?.RepositoryInfo?.RepositoryMode.ToString();
            List <ProjectInfoViewModel> manifests = this.mapper.Map <List <ProjectInfoViewModel> >(result.Projects);

            if (manifests.Any())
            {
                model.RepositoryStamp = StampSorter.GetNewestStamp(manifests.Select(x => x.RepositoryStamp).ToList());
                var orderedTimes = manifests.OrderByDescending(x => x.AddedDateTime).ToList();
                model.ImportedDate     = orderedTimes.First().AddedDateTime;
                model.ImportDuration   = model.ImportedDate - orderedTimes.Last().AddedDateTime;
                model.NumberOfProjects = manifests.Count;
                model.NumberOfAutogeneratedProjects = manifests.Count(x => x.Autogenerated);
                model.AutogeneratedPercentage       = Math.Round((decimal)model.NumberOfAutogeneratedProjects / model.NumberOfProjects * 100, 1);
                model.NumberOfComponents            = manifests.Sum(x => x.Components.Count);
                model.NumberOfTags = manifests.Sum(prj => prj.Components.Sum(cmp => cmp.Tags.Count));
                foreach (ProjectInfoViewModel projectInfoViewModel in manifests)
                {
                    projectInfoViewModel.DownloadsCount =
                        downloadStatsTask.Result.ProjectDownloadData.FirstOrDefault(x =>
                                                                                    x.ProjectKey == projectInfoViewModel.ProjectUri)?.DownloadCount ?? 0;
                }
            }
            ProjectsTableModel projectsTableModel = new ProjectsTableModel(manifests, false, false);

            model.ProjectsTable         = projectsTableModel;
            model.ProjectsTable.Filters = filters;

            model.SearchIndexViewModel = this.GetRepositoriesSelectList(organizationName, repositoryName);

            MvcBreadcrumbNode breadcrumb = PrepareIndexBreadcrumb(organizationName, repositoryName);

            this.ViewData["BreadcrumbNode"] = breadcrumb;
            return(this.View(model));
        }
        /// <summary>
        /// If the repository is in snapshot mode, add the filter that selects entries from latest version only
        /// </summary>
        /// <param name="projects"></param>
        /// <param name="filter"></param>
        /// <param name="repository"></param>
        /// <param name="stamp"></param>
        /// <returns></returns>
        private static async Task <FilterDefinition <ProjectInfo> > AppendNewestStampFilterIfNeeded(IMongoCollection <ProjectInfo> projects,
                                                                                                    FilterDefinition <ProjectInfo> filter, RepositoryInfo repository, string stamp = null)
        {
            if (repository != null && repository.RepositoryMode == RepositoryMode.Snapshot)
            {
                List <string> stamps = await(await projects.DistinctAsync(x => x.RepositoryStamp, filter).ConfigureAwait(false)).ToListAsync().ConfigureAwait(false);
                string        stampToInclude;
                if (!string.IsNullOrEmpty(stamp))
                {
                    if (!stamps.Any(x => string.Equals(x, stamp, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new InvalidOperationException($"Stamp [{stamp}] is not available within the repository");
                    }

                    stampToInclude = stamp;
                }
                else
                {
                    stampToInclude = StampSorter.GetNewestStamp(stamps);
                }

                filter = filter & Builders <ProjectInfo> .Filter.Where(x => x.RepositoryStamp == stampToInclude);
            }

            return(filter);
        }
        public void TestSorting_EmptyCollection_ShouldBeNull()
        {
            var collection = new Collection <string>();

            var result = StampSorter.GetNewestStamp(collection);

            result.Should().BeNull();
        }
        public void TestSorting_OnlyDateTimes_ReturnsNewest()
        {
            var collection = new Collection <string>();

            collection.Add(new DateTime(2000, 01, 01).ToString("O", CultureInfo.InvariantCulture));
            collection.Add(new DateTime(2000, 01, 03).ToString("O", CultureInfo.InvariantCulture));
            collection.Add(new DateTime(2000, 01, 02).ToString("O", CultureInfo.InvariantCulture));

            var result = StampSorter.GetNewestStamp(collection);

            result.Should().BeEquivalentTo(new DateTime(2000, 01, 03).ToString("O", CultureInfo.InvariantCulture));
        }
        public void TestSorting_DateTimeCollectionStartsWithIncorrectValue_ReturnsNewest()
        {
            var collection = new Collection <string>();

            collection.Add("NOT A DATE");
            collection.Add(new DateTime(2000, 01, 01).ToString("O", CultureInfo.InvariantCulture));
            collection.Add(new DateTime(2000, 01, 03).ToString("O", CultureInfo.InvariantCulture));
            collection.Add(new DateTime(2000, 01, 02).ToString("O", CultureInfo.InvariantCulture));

            var result = StampSorter.GetNewestStamp(collection);

            result.Should().BeEquivalentTo(new DateTime(2000, 01, 03).ToString("O", CultureInfo.InvariantCulture));
        }