コード例 #1
0
        public async Task AddSameTags_VariousRepos_FlattenedProperly()
        {
            //arrange
            var db = new StatisticsDatabase(Settings);
            StatisticsService service = new StatisticsService(db, new Mapper(MappingConfigurationFactory.Create()));

            var parameter1 = new RepositoryQueryParameter(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            await service.Update(parameter1, new [] { "FirstTag", "SecondTag", "Third" }).ConfigureAwait(false);

            await service.Update(parameter1, new[] { "FIRSTTAG", "", "Fourth" }).ConfigureAwait(false);

            await service.Update(parameter1, new[] { "FIRSTTAG", "SecondTag", "Fourth" }).ConfigureAwait(false);


            parameter1 = new RepositoryQueryParameter(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            await service.Update(parameter1, new[] { "FirstTag", "SecondTag", "Fourth" }).ConfigureAwait(false);

            await service.Update(parameter1, new[] { "Fifth", "Sixth" }).ConfigureAwait(false);

            //act
            List <SearchKeywordData> result = (await service.GetFlattened().ConfigureAwait(false)).ToList();


            //assert
            result.Count.Should().Be(6);
            result.Single(x => x.Keyword == "FirstTag").SearchCount.Should().Be(4);
            result.Single(x => x.Keyword == "SecondTag").SearchCount.Should().Be(3);
            result.Single(x => x.Keyword == "Third").SearchCount.Should().Be(1);
            result.Single(x => x.Keyword == "Fourth").SearchCount.Should().Be(3);
            result.Single(x => x.Keyword == "Fifth").SearchCount.Should().Be(1);
            result.Single(x => x.Keyword == "Sixth").SearchCount.Should().Be(1);
        }
コード例 #2
0
        public async Task AddSameTags_ValuesIncrementedProperly()
        {
            var db = new StatisticsDatabase(Settings);
            StatisticsService service         = new StatisticsService(db, new Mapper(MappingConfigurationFactory.Create()));
            string            organizationOne = Guid.NewGuid().ToString();
            string            repoOne         = Guid.NewGuid().ToString();

            var parameter = new RepositoryQueryParameter(organizationOne, repoOne);

            string[]         words  = new[] { "FirstTag", "SecondTag", "Third" };
            SearchStatistics stats1 = await service.Update(parameter, words).ConfigureAwait(false);

            SearchStatistics stats2 = await service.Update(parameter, words).ConfigureAwait(false);

            stats1.RepositoryName.Should().Be(stats2.RepositoryName);
            stats1.SearchKeywordData.Count.Should().Be(3);
            foreach (SearchKeywordData searchKeywordData in stats1.SearchKeywordData)
            {
                Assert.AreEqual(1, searchKeywordData.SearchCount);
            }
            stats2.SearchKeywordData.Count.Should().Be(3);
            foreach (SearchKeywordData searchKeywordData in stats2.SearchKeywordData)
            {
                Assert.AreEqual(2, searchKeywordData.SearchCount);
            }
        }
コード例 #3
0
        private async Task <SearchStatistics> FindOneOrCreateNewAsync(RepositoryQueryParameter repositoryParameter, FilterDefinition <SearchStatistics> repoNameFilter)
        {
            FindOneAndUpdateOptions <SearchStatistics, SearchStatistics> options =
                new FindOneAndUpdateOptions <SearchStatistics, SearchStatistics>()
            {
                IsUpsert       = true,
                ReturnDocument = ReturnDocument.After
            };

            UpdateDefinition <SearchStatistics> updateDef = new UpdateDefinitionBuilder <SearchStatistics>()
                                                            .SetOnInsert(x => x.OrganizationName, repositoryParameter.OrganizationName)
                                                            .SetOnInsert(x => x.RepositoryName, repositoryParameter.RepositoryName)
            ;

            try
            {
                return(await this.searchStatisticsCollection.FindOneAndUpdateAsync(repoNameFilter, updateDef, options)
                       .ConfigureAwait(false));
            }
            catch (MongoException)
            {
                //upsert might require a retry
                //https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/#upsert-and-unique-index
                //https://stackoverflow.com/questions/42752646/async-update-or-insert-mongodb-documents-using-net-driver
                return(await this.searchStatisticsCollection.FindOneAndUpdateAsync(repoNameFilter, updateDef, options)
                       .ConfigureAwait(false));
            }
        }
コード例 #4
0
        /// <summary>
        /// Updates the search statistics
        /// </summary>
        /// <param name="repositoryParameter"></param>
        /// <returns></returns>
        public async Task <SearchStatistics> Get(RepositoryQueryParameter repositoryParameter)
        {
            void CheckParams()
            {
                if (repositoryParameter == null)
                {
                    throw new ArgumentNullException(nameof(repositoryParameter));
                }
            }

            CheckParams();

            FilterDefinition <SearchStatistics> repoNameFilter =
                RepoCatFilterBuilder.BuildStatisticsRepositoryFilter(repositoryParameter.OrganizationName,
                                                                     repositoryParameter.RepositoryName);

            return(await this.FindOneOrCreateNewAsync(repositoryParameter, repoNameFilter));
        }
コード例 #5
0
        /// <summary>
        /// Updates the search statistics
        /// </summary>
        /// <param name="repositoryParameter"></param>
        /// <param name="keywords"></param>
        /// <returns></returns>
        public async Task <SearchStatistics> Update(RepositoryQueryParameter repositoryParameter, IEnumerable <string> keywords)
        {
            void CheckParams()
            {
                if (repositoryParameter == null)
                {
                    throw new ArgumentNullException(nameof(repositoryParameter));
                }
            }

            CheckParams();

            FilterDefinition <SearchStatistics> repoNameFilter =
                RepoCatFilterBuilder.BuildStatisticsRepositoryFilter(repositoryParameter.OrganizationName,
                                                                     repositoryParameter.RepositoryName);

            var statistics = await this.FindOneOrCreateNewAsync(repositoryParameter, repoNameFilter);

            foreach (string keyword in keywords)
            {
                if (string.IsNullOrWhiteSpace(keyword))
                {
                    continue;
                }
                var existing = statistics.SearchKeywordData.FirstOrDefault(x =>
                                                                           string.Equals(x.Keyword, keyword, StringComparison.OrdinalIgnoreCase));
                if (existing != null)
                {
                    existing.SearchCount++;
                }
                else
                {
                    statistics.SearchKeywordData.Add(new SearchKeywordData()
                    {
                        Keyword     = keyword,
                        SearchCount = 1
                    });
                }
            }

            await this.searchStatisticsCollection.ReplaceOneAsync(repoNameFilter, statistics);

            return(statistics);
        }