/// <summary> /// Populates help topics. /// </summary> public static void PopulateHelpTopics() { HelpTopic topic = null; int totalAddedTopics = 0; string topicMarkdown = null; List <HelpTopic> topics = null; int mediaItemsMarkerIndex = -1; ResourceSet allResourceItems = null; string titlePattern = @"^#\s+([^\n]+)$"; string mediaItemsMarkdown = string.Empty; string mediaItemsMarker = "Related images and videos:"; Dictionary <string, HelpTopicScore> scores = new Dictionary <string, HelpTopicScore>(); if (!_isPopulating) { _isPopulating = true; try { using (var repository = new Storage.Repositories.RavenRepository <HelpTopic>()) { // Deleting all topics. while ((topics = repository.Query().Take(10).ToList()).Count > 0) { try { _log.Info(string.Format("Deleting next {0} topics...", topics.Count)); topics.ForEach(t => { // Remembering the score. if (!scores.ContainsKey(t.ReferenceKey)) { scores.Add(t.ReferenceKey, t.Score); } repository.Delete(t); }); System.Threading.Thread.Sleep(50); } catch { } } // Reading all items from "Topics.resx" file. allResourceItems = Topics.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); foreach (DictionaryEntry entry in allResourceItems) { topicMarkdown = entry.Value.ToString().Trim(); if (!string.IsNullOrWhiteSpace(topicMarkdown) && topicMarkdown.Length > 0) { topic = new HelpTopic(); topic.ReferenceKey = entry.Key.ToString().Replace('_', '.').ToLowerInvariant(); topic.Title = Regex.Match(topicMarkdown, titlePattern, RegexOptions.Multiline).Groups[1].Value.Trim(); // Removing the title. topicMarkdown = Regex.Replace(topicMarkdown, titlePattern, string.Empty, RegexOptions.Multiline).Trim(); mediaItemsMarkerIndex = topicMarkdown.IndexOf(mediaItemsMarker, StringComparison.OrdinalIgnoreCase); if (mediaItemsMarkerIndex >= 0) { // Extracting and stripping media items list. mediaItemsMarkdown = topicMarkdown.Substring(mediaItemsMarkerIndex + mediaItemsMarker.Length).Trim(); topicMarkdown = topicMarkdown.Substring(0, mediaItemsMarkerIndex).Trim(); // Getting list items and separating them into its own collection (displayed as a gallery). topic.MediaItems = mediaItemsMarkdown.Split(new string[] { "- " }, StringSplitOptions.None) .Select(item => item.Trim()).Where(item => !string.IsNullOrWhiteSpace(item) && item.Length > 0) .Select(item => item.IndexOf('/') < 0 ? string.Format("/Assets/img/help/{0}/{1}", entry.Key, item) : item).ToList(); } topic.Body = topicMarkdown; if (scores.ContainsKey(topic.ReferenceKey)) { topic.Score = scores[topic.ReferenceKey]; } _log.Info(string.Format("Adding new topic: {0}...", topic.ReferenceKey)); // Inserting new topic. repository.Update(topic); totalAddedTopics++; } } } _log.Info(string.Format("Done adding topics (total: {0}).", totalAddedTopics)); } catch (Exception ex) { _log.Error("Failed to populate help topics.", ex); } _isPopulating = false; } }
/// <summary> /// Collects queued impressions. /// </summary> public static void CollectQueuedImpressions() { Presentation p = null; IMessageQueue queue = null; GenericMessageBody body = null; IEnumerable <Message> messages = null; IDictionary <int, int> addedImpressions = null; List <Tuple <GenericMessageBody, Message> > dispatchedMessages = null; if (!_isCollecting) { _isCollecting = true; queue = MessageQueueManager.Current.GetQueue(MessageQueueType.Impressions); messages = queue.GetMessages().ToList(); dispatchedMessages = new List <Tuple <GenericMessageBody, Message> >(); if (messages != null && messages.Any()) { try { using (var repository = new Storage.Repositories.NHibernateRepository <Impression>()) { foreach (var m in messages) { try { body = GenericMessageBody.FromString(m.Body); if (body != null) { repository.Update(new Impression() { PresentationId = body.GetParameter <int>("PresentationId"), PresentationUserId = body.GetParameter <int>("PresentationUserId"), Timestamp = m.Created }); dispatchedMessages.Add(new Tuple <GenericMessageBody, Message>(body, m)); } } catch (Exception ex) { _log.Error(string.Format("Failed to collect message with Id '{0}'", m.Id), ex); } } } queue.RemoveMessages(dispatchedMessages.Select(m => m.Item2)); addedImpressions = dispatchedMessages.GroupBy(t => t.Item1.GetParameter <int>("PresentationId")).ToDictionary(g => g.Key, g => g.Count()); using (var repository = new Storage.Repositories.RavenRepository <Presentation>()) { foreach (var imp in addedImpressions) { try { p = repository.Select(imp.Key); if (p != null && p.TotalImpressions < 10000) { p.TotalImpressions += imp.Value; repository.Update(p); } } catch (Exception ex) { _log.Error(string.Format("Failed to update total impressions for infographic with Id '{0}'", imp.Key), ex); } } } if (addedImpressions != null && addedImpressions.Any()) { CollectSocialImpacts(addedImpressions.Keys); } } catch (Exception ex) { _log.Error(string.Format("Failed to collect {0} impression(s).", messages.Count()), ex); } } _isCollecting = false; } }