public async Task OnReceived(ITweetDTO tweet) { try { double?sentimentValue = await sentiment.Measure(tweet.Text).ConfigureAwait(false); ITweet tweetItem = Tweet.GenerateTweetFromDTO(tweet); var saveTask = Task.Run(() => persistency?.Save(tweetItem, sentimentValue)); var rating = new RatingRecord(tweet.Id.ToString(), DateTime.UtcNow, sentimentValue); foreach (IKeywordTracker tracker in Trackers) { tracker.AddRating(tweet.Text, rating); } if (users.Contains(tweet.CreatedBy.Name)) { manager.Resolve(tweet.CreatedBy.Name, "User").AddRating(rating); } await saveTask.ConfigureAwait(false); } catch (Exception ex) { logger.LogError(ex, "Failed processing"); } }
private void LoadSingleFile(string file) { logger.LogInformation("Loading {0}", file); using (var streamRead = new StreamReader(file)) using (var csvData = new CsvReader(streamRead, CultureInfo.CurrentCulture)) { csvData.Read(); csvData.ReadHeader(); while (csvData.Read()) { var date = csvData.GetField <DateTime>("Date"); var tag = csvData.GetField <string>("Tag"); var type = csvData.GetField <string>("Type"); var id = csvData.GetField <string>("Id"); var rating = csvData.GetField <double?>("Rating"); var tracker = manager.Resolve(tag, type); tracker.AddRating(new RatingRecord { Date = date, Id = id, Rating = rating }); } } }
public TestExpectation(ITrackingManager manager) { manager.Resolve("AMD", Constant.DefaultType).AddRating(new RatingRecord { Id = "1", Date = DateTime.Now, Rating = 1 }); }
public IKeywordTracker[] GetTrackers() { List <IKeywordTracker> tracker = new List <IKeywordTracker>(); if (Config.Keywords?.Length > 0) { logger.LogDebug("Adding keywords"); tracker.AddRange(Config.Keywords.Select(item => new KeywordTracker(item, true, manager.Resolve(item, "Keyword")))); logger.LogDebug("Total keywords: {0}", tracker.Count); } if (Config.Users?.Length > 0) { logger.LogDebug("Adding users"); tracker.AddRange(Config.Users.Where(item => item.StartsWith("@")).Select(item => new KeywordTracker(item, false, manager.Resolve(item, "User")))); logger.LogDebug("Total keywords: {0}", tracker.Count); } return(tracker.ToArray()); }
public async Task <bool> Save(Article article) { try { logger.LogDebug("Saving: {0} {1}", article.Definition.Feed.Category, article.Definition.Id); var saveTask = Task.Run(() => persistency.Save(article)); var trackerArticle = trackingManager.Resolve(article.Definition.Feed.Category, "Article"); var trackerComments = trackingManager.Resolve(article.Definition.Feed.Category, "Comment"); var texts = new Dictionary <string, (string Text, Action <double?> Rating)>(); if (!trackerArticle.IsTracked(article.Definition.Id)) { logger.LogDebug("Tracking: {0}", article.Definition.Id); var date = article.Definition.Date ?? DateTime.UtcNow; texts[article.Definition.Id] = (article.Content.Text, rating => trackerArticle.AddRating(new RatingRecord(article.Definition.Id, date, rating))); } logger.LogDebug("Total comments: {0}", article.Comments.Length); foreach (var comment in article.Comments) { if (!trackerComments.IsTracked(comment.Id)) { logger.LogDebug("Tracking: {0}", comment.Id); texts[comment.Id] = (comment.Text, rating => trackerComments.AddRating(new RatingRecord(comment.Id, comment.Date, rating))); } } if (texts.Count > 0) { logger.LogDebug("Checking sentiment: {0}", texts.Count); var request = texts.Select(item => (item.Key, item.Value.Text)).ToArray(); var sentimentValue = await sentiment.Measure(request, CancellationToken.None).ToArray(); foreach (var tuple in sentimentValue) { var result = texts[tuple.Item1]; result.Rating(tuple.Item2); } } logger.LogDebug( "Articles [{0}] Total: {1} with sentiment: {2} Average: {3}", article.Definition.Feed.Category, trackerArticle.Count(false), trackerArticle.Count(), trackerArticle.CalculateAverageRating()); logger.LogDebug( "Comments [{0}] Total: {1} with sentiment: {2} Average: {3}", article.Definition.Feed.Category, trackerArticle.Count(false), trackerArticle.Count(), trackerArticle.CalculateAverageRating()); await saveTask.ConfigureAwait(false); return(true); } catch (Exception ex) { logger.LogError(ex, "Failed processing"); } return(false); }
private RatingRecord[] GetSingleHistory(string keyword, string type, int hours) { var tracker = tracking.Resolve(keyword, type); return(tracker.GetRatings(hours).OrderByDescending(item => item.Date).ToArray()); }