public Task <DailyTrendDataViewModel> GetTrendDataForTermAsync(string searchTerm) { if (string.IsNullOrWhiteSpace(searchTerm)) { throw new ArgumentException("Search term cannot be null or empty", nameof(searchTerm)); } var countsByDay = postCountsCache.Get(); if (!resultsCache.TryGet(searchTerm, out var cached)) { var searchResults = indexManager.Search(searchTerm); var counts = new List <ushort>(); var maxCount = 0; for (var i = 0; i < countsByDay.Days.Count; i++) { var date = countsByDay.Days[i]; var onDate = searchResults.Count(x => x.Date.Date == date.Date); counts.Add((ushort)onDate); if (onDate > maxCount) { maxCount = onDate; } } cached = new CachedResult { Counts = counts, MaxCount = maxCount }; if (searchResults.Count > 1000) { resultsCache.Cache(searchTerm, cached); } } var result = new DailyTrendDataViewModel { Counts = cached.Counts, Start = countsByDay.Min, End = countsByDay.Max, CountMax = cached.MaxCount, DailyTotals = countsByDay.PostsPerDay }; return(Task.FromResult(result)); }
public async Task <DailyTrendDataViewModel> GetTrendDataForTermAsync(string searchTerm) { if (string.IsNullOrWhiteSpace(searchTerm)) { throw new ArgumentException("Search term cannot be null or empty", nameof(searchTerm)); } var countsByDay = postCountsCache.Get(); if (!resultsCache.TryGet(searchTerm, out var cached)) { var searchResults = await Search(searchTerm); var counts = new List <ushort>(); var scores = new List <int>(); var maxCount = 0; for (var i = 0; i < countsByDay.Days.Count; i++) { var date = countsByDay.Days[i]; scores.Add(0); counts.Add(0); var onDate = searchResults.Where(x => x.Date.Date == date.Date); var count = 0; foreach (var locatedEntry in onDate) { scores[i] += locatedEntry.Score; count++; } counts[i] = (ushort)count; if (count > maxCount) { maxCount = count; } } cached = new CachedResult { Counts = counts, MaxCount = maxCount, Scores = scores }; if (searchResults.Count > 1000) { resultsCache.Cache(searchTerm, cached); } } var result = new DailyTrendDataViewModel { Counts = cached.Counts, Start = countsByDay.Min, End = countsByDay.Max, CountMax = cached.MaxCount, DailyTotals = countsByDay.PostsPerDay, Scores = cached.Scores }; return(result); }