Example #1
0
        public IDictionary <string, double> CalculatePopularPosts(PopularPostsDTO popularPostsDTO, int maxPosts)
        {
            IDictionary <string, double> postsProbability = new Dictionary <string, double>();

            if (maxPosts <= 0)
            {
                return(postsProbability);
            }

            PostStatisticsDTO[] postStatisticsDTOs = popularPostsDTO.PostsStatistics;

            int    allViews      = postStatisticsDTOs.Sum(postStatistics => postStatistics.Views);
            int    allConversion = postStatisticsDTOs.Sum(postStatistics => postStatistics.Conversions);
            double allReadTime   = postStatisticsDTOs.Sum(postStatistics => postStatistics.ReadTime);

            foreach (PostStatisticsDTO postStatitsticDTO in postStatisticsDTOs)
            {
                double postProbability = CalculatePostProbability(
                    allViews,
                    allConversion,
                    allReadTime,
                    postStatitsticDTO
                    );

                postsProbability.Add(postStatitsticDTO.PostId, postProbability);
            }

            return(GetResultProbability(postsProbability, maxPosts));
        }
Example #2
0
        public void TestResultOnePost()
        {
            PopularPostsDTO popularPostsDTO = new PopularPostsDTO();

            popularPostsDTO.PostsStatistics = GetFullDataPostStatisticsDTOs();

            IDictionary <string, double> result = calculatePopularPostsService.CalculatePopularPosts(popularPostsDTO, 1);

            Assert.Equal(0.4816487859966121, result.First().Value);
            Assert.Equal(HIGH_PROBABILITY_POST_ID, result.First().Key);
        }
Example #3
0
        public void TestZeroReadTime()
        {
            PopularPostsDTO popularPostsDTO = new PopularPostsDTO();

            popularPostsDTO.PostsStatistics = GetCustomDataPostStatisticsDTOs(1000, 250, 0);

            IDictionary <string, double> result = calculatePopularPostsService.CalculatePopularPosts(popularPostsDTO, 2);

            Assert.Equal(0, result.First().Value);
            Assert.Equal(CUSTOM_POST_ID_1, result.First().Key);

            Assert.Equal(0, result.Last().Value);
            Assert.Equal(CUSTOM_POST_ID_2, result.Last().Key);
        }
Example #4
0
        public void TestOnePopularPostStatistics()
        {
            string postId = "single";
            Random random = new Random();

            PostStatisticsDTO postStatisticsDTO = new PostStatisticsDTO();

            postStatisticsDTO.PostId      = postId;
            postStatisticsDTO.Views       = random.Next(1, 100000);
            postStatisticsDTO.Conversions = random.Next(1, 100000);
            postStatisticsDTO.ReadTime    = (double)random.Next(1, 100000);

            PostStatisticsDTO[] postsStatistics = { postStatisticsDTO };

            PopularPostsDTO popularPostsDTO = new PopularPostsDTO();

            popularPostsDTO.PostsStatistics = postsStatistics;

            IDictionary <string, double> result = calculatePopularPostsService.CalculatePopularPosts(popularPostsDTO, 1);

            Assert.Equal(0.7333333333333334, result.First().Value);
            Assert.Equal(postId, result.First().Key);
        }
 public JsonResult Calculate([FromBody] PopularPostsDTO popularPostsDTO, int maxPosts)
 {
     return(Json(calculateSimilarPostsService.CalculatePopularPosts(popularPostsDTO, maxPosts)));
 }