public static void AddTweetPerMinute(TweetHashtag tweetHashtag, HashtagCount hashtagCount, IEnumerable <Tweet> tweets, DateTime dateTime) { // TODO- tweet is sometimes returning as null, not sure how this is happening // using if to catch null instances Tweet tweet = tweets.SingleOrDefault(t => t.ID == tweetHashtag.TweetID); if (tweet != null) { // adds 100 to the correct 5 minute interval 0-11 hashtagCount.TweetsPer[Convert.ToInt32(Math.Floor((dateTime - UnixTimeStampToDateTime(tweet.UnixTimeStamp)).Minutes / 5.0))] += 100; } }
public IActionResult Index(int id) { List <Hashtag> hashtagsAll = context.Hashtags.ToList(); Hashtag hashtag = hashtagsAll.Single(h => h.ID == id); List <TweetHashtag> tweetHashtags = context.TweetHashtags.Where(th => th.HashtagID == id).ToList(); List <TweetHashtag> tweethHashtagsPast24Hours = tweetHashtags.Where(th => UnixTimeStampToDateTime(th.UnixTimeStamp) > dateTimeNow.AddDays(-1)).ToList(); List <Tweet> tweetsPast24Hours = context.Tweets.Where(t => UnixTimeStampToDateTime(t.UnixTimeStamp) > dateTimeNow.AddDays(-1)).ToList(); List <Language> languages = context.Languages.ToList(); List <Tweet> tweets = new List <Tweet>(); // Count the number of times the Hashtag has been used in the past 24 hours foreach (TweetHashtag th in tweethHashtagsPast24Hours) { Tweet tweet = tweetsPast24Hours.Single(t => t.ID == th.TweetID); tweets.Add(tweet); } // Muliply total by 100 to account for 1% stream int totalTweets = tweets.Count * 100; // Keep track of which languages have already been used List <int> languagesIdsUsed = new List <int>(); // languageCounts are created for each different language // then used to keep track of how many times the language is used List <LanguageCount> languageCounts = new List <LanguageCount>(); // Keep track of hashtags that have been used in combination with primary hashtag List <String> hashtagStrings = new List <string>(); // hashtagStringCounts are created for each different hashtag // then used to keep track of how many times the hashtag is used in conjunction with the primary hashtag List <HashtagStringCount> hashtagStringCounts = new List <HashtagStringCount>(); // Create array to hold number of tweets in each hour int[] tweetsPerHour = new int[24]; foreach (Tweet tweet in tweets) { // determine the hour the tweet was created and add 100 to the tweetsPerHour array int hour = UnixTimeStampToDateTime(tweet.UnixTimeStamp).Hour; tweetsPerHour[hour] += 100; // Retrieve the language of the tweet and keep track of the times that language has been used if (languagesIdsUsed != null && languagesIdsUsed.Contains(tweet.LanguageID)) { LanguageCount languageCount = languageCounts.Single(lc => lc.Language.ID == tweet.LanguageID); languageCount.TimesUsed += 100; } else { // If it is the first time the language has been used create a new languageCount LanguageCount newLanguageCount = new LanguageCount { Language = languages.Single(l => l.ID == tweet.LanguageID), TimesUsed = 100 }; languagesIdsUsed.Add(tweet.LanguageID); languageCounts.Add(newLanguageCount); } // Hashtags are saved as a single string // each hashtag is seperated by a "/" // Split single hashtag string into substrings String value = tweet.Hashtags; Char delimiter = '/'; String[] substrings = value.Split(delimiter); // Look at each hashtag in the tweet // if the hashtag is new create a hashtagStringCount object for it // keep track of the number of times each hashtag has been used // Using the HashtagString here to limit having to go back to the database to retrieve each Hashtag object // Will retrieve hashtag object once the top ten have been found foreach (String hashtagString in substrings) { if (hashtagStrings != null && hashtagStrings.Contains(hashtagString)) { HashtagStringCount hashtagStringCount = hashtagStringCounts.Single(hsc => hsc.HashtagString == hashtagString); hashtagStringCount.TimesUsed += 100; } else { HashtagStringCount newHashtagStringCount = new HashtagStringCount { HashtagString = hashtagString, TimesUsed = 100 }; hashtagStrings.Add(hashtagString); hashtagStringCounts.Add(newHashtagStringCount); } } } // Sort HashtagStringCounts by TimesUsed hashtagStringCounts.Sort(new HashtagStringCountComparer()); if (hashtagStringCounts.Count > 11) { // Drop all but Top 10 Hashtags hashtagStringCounts.RemoveRange(11, hashtagStringCounts.Count - 11); } // Create a HashtagCount object for each of the Top Ten HashtagStringCount objects // HashtagCount contains instance of hashtag object rather than just the string List <HashtagCount> hashtagCounts = new List <HashtagCount>(); foreach (var hashtagStringCount in hashtagStringCounts) { Hashtag newHashtag = context.Hashtags.Single(ht => ht.Name == hashtagStringCount.HashtagString); HashtagCount newHashtagCount = new HashtagCount { Hashtag = newHashtag, TimesUsed = hashtagStringCount.TimesUsed }; hashtagCounts.Add(newHashtagCount); } // Sort HashtagCounts by TimesUsed hashtagCounts.Sort(new HashtagCountComparer()); languageCounts.Sort(new LanguageCountComparer()); int totalLanguages = languagesIdsUsed.Count; int totalHashtags = hashtagStrings.Count - 1; string hashtagName = hashtag.Name; HashtagViewModel hashtagViewModel = new HashtagViewModel(languageCounts, hashtagCounts, tweetsPerHour, totalTweets, totalLanguages, totalHashtags, hashtagName, hashtagsAll); return(View(hashtagViewModel)); }
public IActionResult Index(string name) { List <Hashtag> hashtagsAll = context.Hashtags.ToList(); Hashtag userHashtag = hashtagsAll.SingleOrDefault(h => h.Name == name); // If name string is not found it redirect to Home controller and reload the home page with an error message if (userHashtag == null) { return(RedirectToAction("HashtagNotFound", "Home")); } Hashtag hashtag = hashtagsAll.Single(h => h.Name == name); List <TweetHashtag> tweetHashtags = context.TweetHashtags.Where(th => th.HashtagID == hashtag.ID).ToList(); List <TweetHashtag> tweethHashtagsPast24Hours = tweetHashtags.Where(th => UnixTimeStampToDateTime(th.UnixTimeStamp) > dateTimeNow.AddDays(-1)).ToList(); List <Tweet> tweetsPast24Hours = context.Tweets.Where(t => UnixTimeStampToDateTime(t.UnixTimeStamp) > dateTimeNow.AddDays(-1)).ToList(); List <Language> languages = context.Languages.ToList(); List <Tweet> tweets = new List <Tweet>(); foreach (TweetHashtag th in tweethHashtagsPast24Hours) { Tweet tweet = tweetsPast24Hours.Single(t => t.ID == th.TweetID); tweets.Add(tweet); } int totalTweets = tweets.Count * 100; List <int> languagesIdsUsed = new List <int>(); List <LanguageCount> languageCounts = new List <LanguageCount>(); List <String> hashtagStrings = new List <string>(); List <HashtagStringCount> hashtagStringCounts = new List <HashtagStringCount>(); // Create array to hold number of tweets in each hour int[] tweetsPerHour = new int[24]; foreach (Tweet tweet in tweets) { // determine the hour the tweet was created and add 100 to the tweetsPerHour array int hour = UnixTimeStampToDateTime(tweet.UnixTimeStamp).Hour; tweetsPerHour[hour] += 100; // Retrieve the language of the tweet and keep track of the times that language has been used if (languagesIdsUsed != null && languagesIdsUsed.Contains(tweet.LanguageID)) { LanguageCount languageCount = languageCounts.Single(lc => lc.Language.ID == tweet.LanguageID); languageCount.TimesUsed += 100; } else { LanguageCount newLanguageCount = new LanguageCount { Language = languages.Single(l => l.ID == tweet.LanguageID), TimesUsed = 100 }; languagesIdsUsed.Add(tweet.LanguageID); languageCounts.Add(newLanguageCount); } // Hashtags are saved as a single string // each hashtag is seperated by a "/" // Split single hashtag string into substrings String value = tweet.Hashtags; Char delimiter = '/'; String[] substrings = value.Split(delimiter); // Look at each hashtag in the tweet // if the hashtag is new create a hashtagStringCount object for it // keep track of the number of times each hashtag has been used // Using the HashtagString here to limit having to go back to the database to retrieve each Hashtag object // Will retrieve hashtag object once the top ten have been found foreach (String hashtagString in substrings) { if (hashtagStrings != null && hashtagStrings.Contains(hashtagString)) { HashtagStringCount hashtagStringCount = hashtagStringCounts.Single(hsc => hsc.HashtagString == hashtagString); hashtagStringCount.TimesUsed += 100; } else { HashtagStringCount newHashtagStringCount = new HashtagStringCount { HashtagString = hashtagString, TimesUsed = 100 }; hashtagStrings.Add(hashtagString); hashtagStringCounts.Add(newHashtagStringCount); } } } // Sort HashtagStringCounts by TimesUsed hashtagStringCounts.Sort(new HashtagStringCountComparer()); if (hashtagStringCounts.Count > 11) { // Drop all but Top 10 Hashtags hashtagStringCounts.RemoveRange(11, hashtagStringCounts.Count - 11); } // Create a HashtagCount object for each of the Top Ten HashtagStringCount objects // HashtagCount contains instance of hashtag object rather than just the string List <HashtagCount> hashtagCounts = new List <HashtagCount>(); foreach (var hashtagStringCount in hashtagStringCounts) { Hashtag newHashtag = context.Hashtags.Single(ht => ht.Name == hashtagStringCount.HashtagString); HashtagCount newHashtagCount = new HashtagCount { Hashtag = newHashtag, TimesUsed = hashtagStringCount.TimesUsed }; hashtagCounts.Add(newHashtagCount); } // Sort HashtagCounts by TimesUsed hashtagCounts.Sort(new HashtagCountComparer()); languageCounts.Sort(new LanguageCountComparer()); int totalLanguages = languagesIdsUsed.Count; int totalHashtags = hashtagStrings.Count - 1; string hashtagName = hashtag.Name; HashtagViewModel hashtagViewModel = new HashtagViewModel(languageCounts, hashtagCounts, tweetsPerHour, totalTweets, totalLanguages, totalHashtags, hashtagName, hashtagsAll); return(View(hashtagViewModel)); }
// Method to retrun Top Ten hashtags of the past hour public static List <HashtagCount> GetTopTenHour(IEnumerable <Hashtag> hashtagsAll, DateTime dateTimeStart, TweetDbContext context) { // Create variable to hold unix time minus one hour // Time stamps in DB are in unix time Int32 unixTimestampStart = (Int32)(dateTimeStart.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; DateTime dateTimeMinusHour = dateTimeStart.AddHours(-1); Int32 unixTimestampMinusHour = (Int32)(dateTimeMinusHour.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; // Contact DB and retrieve data needed var tweetHashtags = context.TweetHashtags.FromSql("SELECT * FROM TweetHashtags WHERE UnixTimeStamp BETWEEN {0} AND {1}", unixTimestampMinusHour, unixTimestampStart).ToList(); var tweets = context.Tweets.FromSql("SELECT * FROM Tweets WHERE UnixTimeStamp BETWEEN {0} AND {1}", unixTimestampMinusHour, unixTimestampStart).ToList(); IEnumerable <int> tweetHashtagIds = (tweetHashtags.Select(tht => tht.HashtagID)).ToList(); IEnumerable <Hashtag> hashtags = hashtagsAll.Where(ht => tweetHashtagIds.Contains(ht.ID)).ToList();; // HashtagIdCount save the ID of the hashtag, keep track of the number of times it was used, and save a list of the TweetHashtags List <HashtagIdCount> hashtagIdCounts = new List <HashtagIdCount>(); // listof hashtagIds will be used to check if a hashtagIdCount has already been made for that hashtag List <int> hashtagIdList = new List <int>(); // For each unique hashtag create HashtagIdCount object and keep track of how many times it was used foreach (TweetHashtag tweetHashtag in tweetHashtags) { if (hashtagIdList != null && hashtagIdList.Contains(tweetHashtag.HashtagID)) { HashtagIdCount hashtagIdCount = hashtagIdCounts.Single(hid => hid.HashtagId == tweetHashtag.HashtagID); hashtagIdCount.TimesUsed += 1; hashtagIdCount.TweetHashtags.Add(tweetHashtag); } else { HashtagIdCount newHashtagIdCount = new HashtagIdCount { HashtagId = tweetHashtag.HashtagID, TimesUsed = 1, TweetHashtags = new List <TweetHashtag>() }; newHashtagIdCount.TweetHashtags.Add(tweetHashtag); hashtagIdCounts.Add(newHashtagIdCount); hashtagIdList.Add(newHashtagIdCount.HashtagId); } } // Sort HashtagIdCounts by TimesUsed hashtagIdCounts.Sort(new HashtagIdCountComparer()); if (hashtagIdCounts.Count > 10) { // Drop all but Top 10 Hashtags hashtagIdCounts.RemoveRange(10, hashtagIdCounts.Count - 10); } // HastagCount objects contain a Hashtag object, TimesUsed int, and an int[] TweetsPer // HashtagCounts will be used to hold information about how many times the hashtag was used in 5 minute intervals List <HashtagCount> hashtagCounts = new List <HashtagCount>(); // List to keep track of which hashtags already have a HashtagCount object // Will not be passed to ViewModel List <string> hashtagStringList = new List <string>(); foreach (HashtagIdCount hashtagIdCount in hashtagIdCounts) { foreach (TweetHashtag tweetHashtag in hashtagIdCount.TweetHashtags) { Hashtag hashTag = hashtags.Single(h => h.ID == tweetHashtag.HashtagID); if (hashtagStringList != null && hashtagStringList.Contains(hashTag.Name.ToString())) { HashtagCount hashtagCount = hashtagCounts.Single(hc => hc.Hashtag == hashTag); // Twitter API stream only gives 1% of all tweets // So each hashtag collected will be counted 100 times hashtagCount.TimesUsed += 100; AddTweetPerMinute(tweetHashtag, hashtagCount, tweets, dateTimeStart); } else { byte[] bytes = Encoding.Default.GetBytes(hashTag.Name); string hashtagName = (Encoding.UTF8.GetString(bytes)); HashtagCount newHashtagCount = new HashtagCount { Hashtag = hashTag, // Twitter API stream only gives 1% of all tweets // So each hashtag collected will be counted 100 times TimesUsed = 100, HashtagName = hashtagName, // int[] that stores data to be used in line graph // period is intervals for the X axis TweetsPer = new int[12] }; // TODO- tweet is sometimes returning as null, not sure how this is happening // using if to catch null instances Tweet tweet = tweets.SingleOrDefault(t => t.ID == tweetHashtag.TweetID); if (tweet != null) { // adds 100 to the correct 5 minute interval 0-11 newHashtagCount.TweetsPer[Convert.ToInt32(Math.Floor((dateTimeStart - UnixTimeStampToDateTime(tweet.UnixTimeStamp)).Minutes / 5.0))] += 100; } hashtagCounts.Add(newHashtagCount); hashtagStringList.Add(hashTag.Name); } } } // Sort HashtagCounts by TimesUsed hashtagCounts.Sort(new HashtagCountComparer()); if (hashtagCounts.Count > 10) { // Drop all but Top 10 Hashtags hashtagCounts.RemoveRange(10, hashtagCounts.Count - 10); } return(hashtagCounts); }