Exemplo n.º 1
0
 public StoryCollection FetchAll()
 {
     StoryCollection coll = new StoryCollection();
     Query qry = new Query(Story.Schema);
     coll.LoadAndCloseReader(qry.ExecuteReader());
     return coll;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the most popular domains.
        /// Measured by number of kicks per domain
        /// </summary>
        /// <param name="hostID">The host ID.</param>
        /// <param name="storyCount">The story count.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        /// <returns></returns>
        public static Dictionary<string, int> GetMostPopularDomains(int hostID, int storyCount, int year, int? month, int? day)
        {
            string cacheKey = String.Format("Zeitgeist_MostPopularDomains_{0}_{1}_{2}_{3}_{4}", hostID, storyCount, year, month, day);
            CacheManager<string, Dictionary<string, int>> domainCache = GetMostPopularDomainsCache();
            Dictionary<string, int> domainList = domainCache[cacheKey];

            if (domainList == null)
            {
                //get all stories, then loop and make our own domain list
                Query qry = new Query(Story.Schema);
                qry.OrderBy = OrderBy.Desc(Story.Columns.KickCount);
                qry.AddWhere(Story.Columns.IsSpam, false);
                qry.AddWhere(Story.Columns.HostID, hostID);
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.GreaterOrEquals, StartingDate(year, month, day));
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.LessOrEquals, EndingDate(year, month, day));
                qry.AddWhere(Story.Columns.KickCount, Comparison.GreaterOrEquals, 1);
                StoryCollection stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByQuery(qry));

                domainList = new Dictionary<string, int>();
                Regex rx = new Regex(@"^(?=[^&])(?:(?<scheme>[^:/?#]+):)?(?://(?<authority>[^/?#]*))?(?<path>[^?#]*)(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?");

                foreach (Story s in stories)
                {
                    Match uriMatch = rx.Match(s.Url);
                    if (!uriMatch.Success)
                        continue;
                    string authority = uriMatch.Groups["authority"].Value;

                    if (string.IsNullOrEmpty(authority))
                        continue;

                    if (domainList.ContainsKey(authority))
                        domainList[authority] += s.KickCount;
                    else
                        domainList.Add(authority, s.KickCount);
                }
                //sort and trim to   storyCount
                List<KeyValuePair<string, int>> sortedPairs = new List<KeyValuePair<string, int>>(domainList);
                sortedPairs.Sort(
                   delegate(KeyValuePair<string, int> obj1, KeyValuePair<string, int> obj2)
                   { return obj2.Value.CompareTo(obj1.Value); }
                );

                domainList.Clear();//clear and add top X values
                if (sortedPairs.Count < storyCount)
                    storyCount = sortedPairs.Count;

                for (int i = 0; i < storyCount; i++)
                    domainList.Add(sortedPairs[i].Key, sortedPairs[i].Value);

                domainCache.Insert(cacheKey, domainList, CacheHelper.CACHE_DURATION_IN_SECONDS);
            }

            return domainList;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the most published users.
        /// Measured by number of published stories per user
        /// </summary>
        /// <param name="hostID">The host ID.</param>
        /// <param name="userCount">The story count.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        /// <returns></returns>
        public static Dictionary <string, int> GetMostPublishedUsers(int hostID, int userCount, int year, int?month, int?day)
        {
            string cacheKey = String.Format("Zeitgeist_MostPublishedUsers_{0}_{1}_{2}_{3}_{4}", hostID, userCount, year, month, day);
            CacheManager <string, Dictionary <string, int> > userListCache = GetMostPublishedUsersCache();
            Dictionary <string, int> userList = userListCache[cacheKey];

            if (userList == null)
            {
                //get all stories, then loop and make our own domain list
                Query qry = new Query(Story.Schema);
                qry.OrderBy = OrderBy.Desc(Story.Columns.UserID);
                qry.AddWhere(Story.Columns.IsSpam, false);
                qry.AddWhere(Story.Columns.IsPublishedToHomepage, true);
                qry.AddWhere(Story.Columns.HostID, hostID);
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.GreaterOrEquals, StartingDate(year, month, day));
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.LessOrEquals, EndingDate(year, month, day));
                StoryCollection stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByQuery(qry));

                userList = new Dictionary <string, int>();

                foreach (Story s in stories)
                {
                    if (userList.ContainsKey(s.User.Username))
                    {
                        userList[s.User.Username] += 1;
                    }
                    else
                    {
                        userList.Add(s.User.Username, 1);
                    }
                }
                //sort and trim to storyCount
                List <KeyValuePair <string, int> > sortedPairs = new List <KeyValuePair <string, int> >(userList);
                sortedPairs.Sort(
                    delegate(KeyValuePair <string, int> obj1, KeyValuePair <string, int> obj2)
                    { return(obj2.Value.CompareTo(obj1.Value)); }
                    );

                userList.Clear();//clear and add top X values
                if (sortedPairs.Count < userCount)
                {
                    userCount = sortedPairs.Count;
                }

                for (int i = 0; i < userCount; i++)
                {
                    userList.Add(sortedPairs[i].Key, sortedPairs[i].Value);
                }

                userListCache.Insert(cacheKey, userList, CacheHelper.CACHE_DURATION_IN_SECONDS);
            }

            return(userList);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get all the stories for a given host
        /// </summary>
        /// <param name="hostID"></param>
        /// <returns></returns>
        public static StoryCollection GetAllStories(int hostID, int pageIndex, int pageSize)
        {
            Query q = Story.Query();
            q.WHERE(Story.Columns.HostID, hostID).AND(Story.Columns.IsSpam, false);
            q.PageIndex = pageIndex;
            q.PageSize = pageSize;

            StoryCollection stories = new StoryCollection();
            stories.LoadAndCloseReader(q.ExecuteReader());
            return stories;
        }
Exemplo n.º 5
0
        public static string GetStoryIdentifier(string title)
        {
            title = Regex.Replace(title, @"[^\w\s]", "_");
            title = title.Replace(" ", "_");
            while (Regex.Match(title, "__").Success)
            {
                title = Regex.Replace(title, "__", "_");
            }

            if (title.Substring(0, 1) == "_")
            {
                title = title.Substring(1, title.Length - 1);
            }

            if (title.Substring(title.Length - 1, 1) == "_")
            {
                title = title.Substring(0, title.Length - 1);
            }

            string identifier = title;

            //ensure that it is unique in the database
            //NOTE: we could use a datestamp to make it unique
            int iteration = 0;

            while (iteration < 10)
            {
                if (iteration > 0)
                {
                    identifier = title + "_" + iteration;
                }

                StoryCollection stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByParameter(Story.Columns.StoryIdentifier, identifier));

                if (stories.Count == 0)
                {
                    return(identifier);
                }

                iteration++;
            }

            throw new Exception("The story identifier [" + title + "] was not unique");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the most kicked stories.
        /// </summary>
        /// <param name="hostID">The host ID.</param>
        /// <param name="storyCount">The story count.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        /// <returns></returns>
        public static StoryCollection GetMostPopularStories(int hostID, int storyCount, int year, int?month, int?day)
        {
            string cacheKey = String.Format("Zeitgeist_MostPopular_{0}_{1}_{2}_{3}_{4}", hostID, storyCount, year, month, day);
            CacheManager <string, StoryCollection> storyCache = GetStoryCollectionCache();
            StoryCollection stories = storyCache[cacheKey];

            if (stories == null)
            {
                Query qry = new Query(Story.Schema);
                qry.Top     = storyCount.ToString();
                qry.OrderBy = OrderBy.Desc(Story.Columns.KickCount);
                qry.AddWhere(Story.Columns.IsSpam, false);
                qry.AddWhere(Story.Columns.HostID, hostID);
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.GreaterOrEquals, StartingDate(year, month, day));
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.LessOrEquals, EndingDate(year, month, day));
                qry.AddWhere(Story.Columns.KickCount, Comparison.GreaterOrEquals, 1);
                stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByQuery(qry));
                storyCache.Insert(cacheKey, stories, CacheHelper.CACHE_DURATION_IN_SECONDS);
            }

            return(stories);
        }
Exemplo n.º 7
0
        // [rgn] Public Methods (9)
        /// <summary>
        /// Gets the most commented on stories.
        /// </summary>
        /// <param name="hostID">The host ID.</param>
        /// <param name="storyCount">The story count.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        /// <returns></returns>
        public static StoryCollection GetMostCommentedOnStories(int hostID, int storyCount, int year, int? month, int? day)
        {
            string cacheKey = String.Format("Zeitgeist_MostCommentedOn_{0}_{1}_{2}_{3}_{4}", hostID, storyCount, year, month, day);
            CacheManager<string, StoryCollection> storyCache = GetStoryCollectionCache();
            StoryCollection stories = storyCache[cacheKey];

            if (stories == null)
            {
                Query qry = new Query(Story.Schema);
                qry.Top = storyCount.ToString();
                qry.OrderBy = OrderBy.Desc(Story.Columns.CommentCount);
                qry.AddWhere(Story.Columns.IsSpam, false);
                qry.AddWhere(Story.Columns.HostID, hostID);
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.GreaterOrEquals, StartingDate(year, month, day));
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.LessOrEquals, EndingDate(year, month, day));
                qry.AddWhere(Story.Columns.CommentCount, Comparison.GreaterOrEquals, 1);
                stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByQuery(qry));
                storyCache.Insert(cacheKey, stories, CacheHelper.CACHE_DURATION_IN_SECONDS);
            }

            return stories;
        }
Exemplo n.º 8
0
        public static string GetStoryIdentifier(string title)
        {
            title = Regex.Replace(title, @"[^\w\s]", "_");
            title = title.Replace(" ", "_");
            while (Regex.Match(title, "__").Success)
                title = Regex.Replace(title, "__", "_");

            if (title.Substring(0, 1) == "_")
                title = title.Substring(1, title.Length - 1);

            if (title.Substring(title.Length - 1, 1) == "_")
                title = title.Substring(0, title.Length - 1);

            string identifier = title;

            //ensure that it is unique in the database
            //NOTE: we could use a datestamp to make it unique
            int iteration = 0;
            while (iteration < 10) {
                if (iteration > 0)
                    identifier = title + "_" + iteration;

                StoryCollection stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByParameter(Story.Columns.StoryIdentifier, identifier));

                if (stories.Count == 0)
                    return identifier;

                iteration++;
            }

            throw new Exception("The story identifier [" + title + "] was not unique");
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets the most popular domains.
        /// Measured by number of kicks per domain
        /// </summary>
        /// <param name="hostID">The host ID.</param>
        /// <param name="storyCount">The story count.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        /// <returns></returns>
        public static Dictionary <string, int> GetMostPopularDomains(int hostID, int storyCount, int year, int?month, int?day)
        {
            string cacheKey = String.Format("Zeitgeist_MostPopularDomains_{0}_{1}_{2}_{3}_{4}", hostID, storyCount, year, month, day);
            CacheManager <string, Dictionary <string, int> > domainCache = GetMostPopularDomainsCache();
            Dictionary <string, int> domainList = domainCache[cacheKey];

            if (domainList == null)
            {
                //get all stories, then loop and make our own domain list
                Query qry = new Query(Story.Schema);
                qry.OrderBy = OrderBy.Desc(Story.Columns.KickCount);
                qry.AddWhere(Story.Columns.IsSpam, false);
                qry.AddWhere(Story.Columns.HostID, hostID);
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.GreaterOrEquals, StartingDate(year, month, day));
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.LessOrEquals, EndingDate(year, month, day));
                qry.AddWhere(Story.Columns.KickCount, Comparison.GreaterOrEquals, 1);
                StoryCollection stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByQuery(qry));

                domainList = new Dictionary <string, int>();
                Regex rx = new Regex(@"^(?=[^&])(?:(?<scheme>[^:/?#]+):)?(?://(?<authority>[^/?#]*))?(?<path>[^?#]*)(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?");

                foreach (Story s in stories)
                {
                    Match uriMatch = rx.Match(s.Url);
                    if (!uriMatch.Success)
                    {
                        continue;
                    }
                    string authority = uriMatch.Groups["authority"].Value;

                    if (string.IsNullOrEmpty(authority))
                    {
                        continue;
                    }

                    if (domainList.ContainsKey(authority))
                    {
                        domainList[authority] += s.KickCount;
                    }
                    else
                    {
                        domainList.Add(authority, s.KickCount);
                    }
                }
                //sort and trim to   storyCount
                List <KeyValuePair <string, int> > sortedPairs = new List <KeyValuePair <string, int> >(domainList);
                sortedPairs.Sort(
                    delegate(KeyValuePair <string, int> obj1, KeyValuePair <string, int> obj2)
                    { return(obj2.Value.CompareTo(obj1.Value)); }
                    );

                domainList.Clear();//clear and add top X values
                if (sortedPairs.Count < storyCount)
                {
                    storyCount = sortedPairs.Count;
                }

                for (int i = 0; i < storyCount; i++)
                {
                    domainList.Add(sortedPairs[i].Key, sortedPairs[i].Value);
                }

                domainCache.Insert(cacheKey, domainList, CacheHelper.CACHE_DURATION_IN_SECONDS);
            }

            return(domainList);
        }
Exemplo n.º 10
0
 public StoryCollection FetchByQuery(Query qry)
 {
     StoryCollection coll = new StoryCollection();
     coll.LoadAndCloseReader(qry.ExecuteReader());
     return coll;
 }
Exemplo n.º 11
0
        /// <summary>
        /// Returns a StoryCollection of stories that have been modified after the
        /// given DateTime value
        /// </summary>
        /// <remarks>this is used by lucene to create an incremental update of the search
        /// index without having to do a full index again</remarks>
        /// <param name="updatedAfter"></param>
        /// <returns></returns>
        public static StoryCollection GetUpdatedStories(int hostID, DateTime updatedAfter, int pageIndex, int pageSize)
        {
            Query q = Story.Query();
            q.WHERE(Story.Columns.HostID, hostID).AND(Story.Columns.UpdatedOn, Comparison.GreaterOrEquals, updatedAfter);
            q.PageIndex = pageIndex;
            q.PageSize = pageSize;

            StoryCollection stories = new StoryCollection();
            stories.LoadAndCloseReader(q.ExecuteReader());
            return stories;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Returns a StoryCollection for a given IList of storyIds
        /// </summary>
        /// <param name="storyId"></param>
        /// <returns></returns>
        public static StoryCollection GetStoriesByIds(IList<int> storyId)
        {
            Query q = Story.Query();

            object[] storyIdArray = new object[storyId.Count];
            for (int i = 0; i < storyId.Count; i++)
            {
                storyIdArray[i] = storyId[i];
            }

            q.IN("storyID", storyIdArray);

            StoryCollection stories = new StoryCollection();
            stories.LoadAndCloseReader(q.ExecuteReader());
            return stories;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the most published users.
        /// Measured by number of published stories per user
        /// </summary>
        /// <param name="hostID">The host ID.</param>
        /// <param name="userCount">The story count.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <param name="day">The day.</param>
        /// <returns></returns>
        public static Dictionary<string, int> GetMostPublishedUsers(int hostID, int userCount, int year, int? month, int? day)
        {
            string cacheKey = String.Format("Zeitgeist_MostPublishedUsers_{0}_{1}_{2}_{3}_{4}", hostID, userCount, year, month, day);
            CacheManager<string, Dictionary<string, int>> userListCache = GetMostPublishedUsersCache();
            Dictionary<string, int> userList = userListCache[cacheKey];

            if (userList == null)
            {
                //get all stories, then loop and make our own domain list
                Query qry = new Query(Story.Schema);
                qry.OrderBy = OrderBy.Desc(Story.Columns.UserID);
                qry.AddWhere(Story.Columns.IsSpam, false);
                qry.AddWhere(Story.Columns.IsPublishedToHomepage, true);
                qry.AddWhere(Story.Columns.HostID, hostID);
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.GreaterOrEquals, StartingDate(year, month, day));
                qry.AddWhere(Story.Columns.CreatedOn, Comparison.LessOrEquals, EndingDate(year, month, day));
                StoryCollection stories = new StoryCollection();
                stories.LoadAndCloseReader(Story.FetchByQuery(qry));

                userList = new Dictionary<string, int>();

                foreach (Story s in stories)
                {
                    if (userList.ContainsKey(s.User.Username))
                        userList[s.User.Username] += 1;
                    else
                        userList.Add(s.User.Username, 1);
                }
                //sort and trim to storyCount
                List<KeyValuePair<string, int>> sortedPairs = new List<KeyValuePair<string, int>>(userList);
                sortedPairs.Sort(
                   delegate(KeyValuePair<string, int> obj1, KeyValuePair<string, int> obj2)
                   { return obj2.Value.CompareTo(obj1.Value); }
                );

                userList.Clear();//clear and add top X values
                if (sortedPairs.Count < userCount)
                    userCount = sortedPairs.Count;

                for (int i = 0; i < userCount; i++)
                    userList.Add(sortedPairs[i].Key, sortedPairs[i].Value);

                userListCache.Insert(cacheKey, userList, CacheHelper.CACHE_DURATION_IN_SECONDS);
            }

            return userList;
        }