Пример #1
0
        /// <summary>
        /// Saves statistics data into XML
        /// Called by application end event
        /// Called by Session end event - once per hour 
        /// </summary>
        /// <param name="stat">
        /// The Stat
        /// </param>
        public void Save(Stat stat)
        {
            XElement root = null;

            try
            {
                root = XElement.Load(stat.Path);
            }
            catch (Exception ex)
            {
                // Logger.Log(NO_FILE_ERROR, ex);
                throw new ApplicationException(NO_FILE_ERROR, ex);
            }

            try
            {
                var qry = from elem in root.Elements("Stat") where elem.Attribute("id").Value == "Visits" select elem;

                if (qry.Count<XElement>() == 1)
                {
                    qry.First<XElement>().SetAttributeValue("Count", stat.Visits);
                }

                var qry2 = from elem in root.Elements("Stat") where elem.Attribute("id").Value == "Hits" select elem;

                if (qry2.Count<XElement>() == 1)
                {
                    qry2.First<XElement>().SetAttributeValue("Count", stat.Hits);
                }

                DateTime now = LocalTime.GetCurrentTime(SettingsData.TimeZoneInfo);

                string todayKey = Stat.GetFeedKey(now);
                string yesKey = Stat.GetFeedKey(now.AddDays(-1));
                string twoKey = Stat.GetFeedKey(now.AddDays(-2));

                foreach (KeyValuePair<string, int> kvp2 in stat.Feeds)
                {
                    if (kvp2.Key == todayKey || kvp2.Key == yesKey || kvp2.Key == twoKey)
                    {
                        XElement statElem = new XElement(
                            "Stat", new XAttribute("id", kvp2.Key), new XAttribute("Count", kvp2.Value));

                        root.Add(statElem);
                    }
                }

                BlogData blogData = new BlogData();
                foreach (KeyValuePair<string, PostInfo> kvp in stat.PageVisits)
                {
                    blogData.ChangeViews(kvp.Key, kvp.Value.Views);
                }
            }
            catch (Exception ex)
            {
                // Logger.Log(XML_FORMAT_ERROR, ex);
                throw new ApplicationException(XML_FORMAT_ERROR, ex);
            }

            root.Save(stat.Path);
        }
Пример #2
0
        /// <summary>
        /// Loads statistics data into a Stat object
        /// Called by Application Start event
        /// </summary>
        /// <returns>The Stat object</returns>
        public Stat Load()
        {
            Stat stat = new Stat();
            List<Tuple<PostInfo, int>> popular = new List<Tuple<PostInfo, int>>();
            XElement root = null;

            try
            {
                root = XElement.Load(this._path);
                stat.Path = this._path;
            }
            catch (Exception ex)
            {
                Logger.Log(NO_FILE_ERROR, ex);
                throw new ApplicationException(NO_FILE_ERROR, ex);
            }

            try
            {
                List<XElement> feedElems = new List<XElement>();
                List<XElement> delPostElems = new List<XElement>();

                // BlogData blogData = new BlogData();
                // List<PostInfo> posts = blogData.GetBlogItems();
                foreach (XElement statElem in root.Elements("Stat"))
                {
                    string id = statElem.Attribute("id").Value;
                    int count = int.Parse(statElem.Attribute("Count").Value);

                    // get feed nodes first
                    if (Stat.IsFeedKey(id))
                    {
                        feedElems.Add(statElem);
                        stat.Feeds[id] = count;
                        continue;
                    }

                    switch (id)
                    {
                        case "Visits":
                            stat.Visits = count;
                            break;
                        case "Hits":
                            stat.Hits = count;
                            break;
                    }
                }

                foreach (XElement feedElem in feedElems)
                {
                    feedElem.Remove();
                }

                BlogData blogData = new BlogData();
                stat.PageVisits = blogData.GetBlogItems().OrderByDescending(p => p.Views).ToDictionary(p => p.FileID);
            }
            catch (Exception ex)
            {
                Logger.Log(XML_FORMAT_ERROR, ex);
                throw new ApplicationException(XML_FORMAT_ERROR, ex);
            }

            root.Save(this._path);

            return stat;
        }
Пример #3
0
        /// <summary>
        /// Refreshes the comment XML from comments from the post
        /// Not called by application
        /// </summary>
        public void Refresh()
        {
            List<Comment> comments = new List<Comment>();
            XElement root = null;

            try
            {
                root = XElement.Load(this._path);
            }
            catch (Exception ex)
            {
                Logger.Log(NO_FILE_ERROR, ex);
                throw new ApplicationException(NO_FILE_ERROR, ex);
            }

            root.RemoveAll();
            root.SetAttributeValue("TotalComments", 0);
            root.Save(this._path);

            BlogData blogData = new BlogData();
            List<PostInfo> posts = blogData.GetBlogItems();

            foreach (PostInfo post in posts)
            {
                PostData postData = new PostData();
                Post post2 = postData.Load(post.FileID);
                comments.AddRange(post2.Comments);
            }

            var qry = from cmnt in comments orderby cmnt.Time ascending select cmnt;

            List<Comment> comments2 = qry.ToList<Comment>();
            Dictionary<string, List<Comment>> dic = new Dictionary<string, List<Comment>>();

            foreach (Comment comment in comments2)
            {
                comment.ID = this.Insert(comment);
                if (!dic.ContainsKey(comment.FileID))
                {
                    dic[comment.FileID] = new List<Comment>();
                }

                dic[comment.FileID].Add(comment);
            }

            foreach (string fileID in dic.Keys)
            {
                PostData postData = new PostData();
                Post post = postData.Load(fileID);
                post.Comments = dic[fileID];
                postData.Save(post);
            }
        }