Ejemplo n.º 1
0
        public void DoesExistTest()
        {
            StoryCollection storyColl = new StoryCollection();

            storyColl.Add(testModel1);
            storyColl.Add(testModel2);

            Assert.IsTrue(storyColl.Exists(testModel2.id));
        }
Ejemplo n.º 2
0
        public void NotExistTest()
        {
            StoryCollection storyColl = new StoryCollection();

            storyColl.Add(testModel1);
            storyColl.Add(testModel2);

            Assert.IsFalse(storyColl.Exists(testModel3.id));
        }
Ejemplo n.º 3
0
        public void AddStoryTest()
        {
            StoryCollection storyColl = new StoryCollection();

            storyColl.Add(testModel1);
            storyColl.Add(testModel2);

            Assert.IsTrue(storyColl.Stories.Count == 2);
        }
Ejemplo n.º 4
0
        public async Task <StoryCollection> GetBestStoriesAsync()
        {
            StoryCollection colStory = new StoryCollection();

            Uri uri = new Uri(HNBaseUrl + "/beststories.json");

            RequestResult requestResult = await HttpHelper.SendRequestAsync(uri);

            if (requestResult.IsSuccess)
            {
                List <int> lstBestStoryIds = null;

                JArray rootObject = (JArray)JsonConvert.DeserializeObject(requestResult.Content);
                lstBestStoryIds = rootObject.ToObject <List <int> >();

                foreach (int storyId in lstBestStoryIds)
                {
                    Story story = await GetStoryAsync(storyId);

                    if (story != null)
                    {
                        colStory.Add(story);
                    }
                }
            }

            return(colStory);
        }
Ejemplo n.º 5
0
        public static void PublishStoryProcess()
        {
            // promote stories to the various hosts homepages
            foreach (string hostKey in HostCache.Hosts.Keys)
            {
                Host host = HostCache.Hosts[hostKey];
                Trace.Write("Pub: Processing " + host.HostName);

                //get unkicked stories within the maximumStoryAgeInHours
                DateTime startDate = DateTime.Now.AddHours(-host.Publish_MinimumStoryAgeInHours);
                DateTime endDate   = DateTime.Now.AddHours(-host.Publish_MaximumStoryAgeInHours);

                //now get a dataset containing all these (NOTE: perf: we could use paging here to reduce the memory footprint)
                StoryCollection stories = Story.GetStoriesByIsPublishedAndHostIDAndPublishedDate(host.HostID, false, endDate, startDate);
                Trace.Write("Pub: There are now " + stories.Count + " candidate stories.");

                //pass 1: remove any weak candidate stories
                StoryCollection candidateStories = new StoryCollection();
                foreach (Story story in stories)
                {
                    if (!IsWeakStory(story, host))
                    {
                        candidateStories.Add(story);
                    }
                }

                Trace.Write("Pub: There are now " + candidateStories.Count + " candidate stories.");

                //pass 2: calculate scores for each story
                SortedList <int, int> storyScoreList = new SortedList <int, int>();
                foreach (Story story in candidateStories)
                {
                    storyScoreList[GetStoryScore(story, host)] = story.StoryID;
                }

                //pass 3: should the top story be published?
                if (storyScoreList.Count > 0)
                {
                    for (int i = 0; i < host.Publish_MaximumSimultaneousStoryPublishCount; i++)
                    {
                        if (storyScoreList.Count > i)
                        {
                            int storyIndex = storyScoreList.Count - 1 - i; //we have to work backwards as the lowest are first
                            if (storyScoreList.Keys[storyIndex] >= host.Publish_MinimumStoryScore)
                            {
                                //publish this story
                                Trace.Write("Pub: Publishing storyID:" + storyScoreList.Values[storyIndex]);
                                PublishStory(storyScoreList.Values[storyIndex]);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public async Task <StoryCollection> GetBestStoriesAsync(int[] storyIds)
        {
            StoryCollection colStory = new StoryCollection();

            Uri uri = new Uri(HNBaseUrl + "/beststories.json");

            for (var i = 0; i < storyIds.Length; i++)
            {
                Story story = await GetStoryAsync(storyIds[i]);

                if (story != null)
                {
                    colStory.Add(story);
                }
            }

            return(colStory);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns a StoryCollection containing the stories that match for the given
        /// search term. The stories are returned in the correct order as given
        /// by the lucene results.
        /// </summary>
        /// <param name="results"></param>
        /// <returns></returns>
        internal static StoryCollection LoadStorySearchResults(IList <int> results)
        {
            if (results.Count == 0)
            {
                return(null);
            }

            StoryCollection stories = Story.GetStoriesByIds(results);

            //the stories are not returned in the hit order lucene found
            //the matches, the order is storyId asc. We need to correct
            //the order so the collection is correctly order as per the
            //hits result from lucene
            StoryCollection searchResults = new StoryCollection();

            foreach (int i in results)
            {
                Story s = (Story)stories.Find(i);
                searchResults.Add(s);
            }

            return(searchResults);
        }
Ejemplo n.º 8
0
        public static void PublishStoryProcess()
        {
            // promote stories to the various hosts homepages
            foreach (string hostKey in HostCache.Hosts.Keys) {
                Host host = HostCache.Hosts[hostKey];
                Trace.Write("Pub: Processing " + host.HostName);

                //get unkicked stories within the maximumStoryAgeInHours
                DateTime startDate = DateTime.Now.AddHours(-host.Publish_MinimumStoryAgeInHours);
                DateTime endDate = DateTime.Now.AddHours(-host.Publish_MaximumStoryAgeInHours);

                //now get a dataset containing all these (NOTE: perf: we could use paging here to reduce the memory footprint)
                StoryCollection stories = Story.GetStoriesByIsPublishedAndHostIDAndPublishedDate(host.HostID, false, endDate, startDate);
                Trace.Write("Pub: There are now " + stories.Count + " candidate stories.");

                //pass 1: remove any weak candidate stories
                StoryCollection candidateStories = new StoryCollection();
                foreach (Story story in stories) {
                    if (!IsWeakStory(story, host))
                        candidateStories.Add(story);
                }

                Trace.Write("Pub: There are now " + candidateStories.Count + " candidate stories.");

                //pass 2: calculate scores for each story
                SortedList<int, int> storyScoreList = new SortedList<int, int>();
                foreach (Story story in candidateStories) {
                    storyScoreList[GetStoryScore(story, host)] = story.StoryID;
                }

                //pass 3: should the top story be published?
                if (storyScoreList.Count > 0) {
                    for (int i = 0; i < host.Publish_MaximumSimultaneousStoryPublishCount; i++) {
                        if (storyScoreList.Count > i) {
                            int storyIndex = storyScoreList.Count - 1 - i; //we have to work backwards as the lowest are first
                            if (storyScoreList.Keys[storyIndex] >= host.Publish_MinimumStoryScore) {
                                //publish this story
                                Trace.Write("Pub: Publishing storyID:" + storyScoreList.Values[storyIndex]);
                                PublishStory(storyScoreList.Values[storyIndex]);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns a StoryCollection containing the stories that match for the given
        /// search term. The stories are returned in the correct order as given
        /// by the lucene results.
        /// </summary>
        /// <param name="results"></param>
        /// <returns></returns>
        internal static StoryCollection LoadStorySearchResults(IList<int> results)
        {
            if (results.Count == 0)
                return null;

            StoryCollection stories = Story.GetStoriesByIds(results);

            //the stories are not returned in the hit order lucene found
            //the matches, the order is storyId asc. We need to correct
            //the order so the collection is correctly order as per the
            //hits result from lucene
            StoryCollection searchResults = new StoryCollection();
            foreach (int i in results)
            {
                Story s = (Story)stories.Find(i);
                searchResults.Add(s);
            }

            return searchResults;
        }