private async Task <List <AggregatedEvent> > GetAllArtistAggregatedEvents(AggregateTimeRange timeRange)
        {
            List <AggregatedEvent> result;
            string localStorageKey = LOCALSTORAGEKEY_ALLARTISTSAGGREGATEDEVENTS + timeRange;
            TimeCachedObject <List <AggregatedEvent> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <AggregatedEvent> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistAggregatedEventsRequest request = new ArtistAggregatedEventsRequest()
                {
                    ArtistIds = new List <int>(), TimeSeries = timeRange
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistAggregatedEvents", request);

                string responseBody = await responseMessage.Content.ReadAsStringAsync();

                result = JsonConvert.DeserializeObject <List <AggregatedEvent> >(responseBody);
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <AggregatedEvent> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        private static int GetTimeRangeMultipler(AggregateTimeRange timeRange)
        {
            int multiplier;

            switch (timeRange)
            {
            case AggregateTimeRange.None:
                throw new InvalidEnumArgumentException("Must specify time range value other than None");

            case AggregateTimeRange.SevenDays:
                multiplier = 1;
                break;

            case AggregateTimeRange.ThreeMonths:
                multiplier = 20;
                break;

            case AggregateTimeRange.OneYear:
                multiplier = 50;
                break;

            default:
                throw new NotSupportedException("Value: " + timeRange.ToString());
            }

            return(multiplier);
        }
        public Task <List <ItemCount> > GetMostPlayedArtistsAsync(AggregateTimeRange timeRange)
        {
            int multiplier = GetTimeRangeMultipler(timeRange);

            return(Task.FromResult(new List <ItemCount>()
            {
                new ItemCount()
                {
                    Count = 100 * multiplier, Name = "artist name", ItemId = 77
                },
                new ItemCount()
                {
                    Count = 100 * multiplier, Name = "artist", ItemId = 53
                },
                new ItemCount()
                {
                    Count = 75 * multiplier, Name = "artist name thats longer", ItemId = 12
                },
                new ItemCount()
                {
                    Count = 51 * multiplier, Name = "artist 1111", ItemId = 9
                },
                new ItemCount()
                {
                    Count = 51 * multiplier, Name = "artist something", ItemId = 204
                },
                new ItemCount()
                {
                    Count = 51 * multiplier, Name = "something artist", ItemId = 513
                }
            }));
        }
        private string GetOverTimeName(int index, AggregateTimeRange timeRange, DateTimeOffset now)
        {
            string result = string.Empty;

            switch (timeRange)
            {
            case AggregateTimeRange.None:
                throw new InvalidEnumArgumentException("Must specify time range value other than None");

            case AggregateTimeRange.SevenDays:
                result = now.AddDays(index * -1).DayOfWeek.ToString();
                break;

            case AggregateTimeRange.ThreeMonths:
                result = now.AddDays(-1 * (int)now.DayOfWeek).AddDays(index * -7).DateTime.ToShortDateString();
                break;

            case AggregateTimeRange.OneYear:
                result = now.AddMonths(index * -1).DateTime.ToShortDateString();
                break;

            default:
                throw new NotSupportedException("Value: " + timeRange.ToString());
            }
            return(result);
        }
        public async Task <List <ItemCount> > GetArtistSongsPlayed(AggregateTimeRange timeRange, int artistId)
        {
            int multiplier = GetTimeRangeMultipler(timeRange);

            return(await Task.FromResult(new List <ItemCount>()
            {
                new ItemCount()
                {
                    Count = 100 * multiplier, Name = "song name", ItemId = 77
                },
                new ItemCount()
                {
                    Count = 100 * multiplier, Name = "song", ItemId = 53
                },
                new ItemCount()
                {
                    Count = 75 * multiplier, Name = "song name thats longer", ItemId = 12
                },
                new ItemCount()
                {
                    Count = 51 * multiplier, Name = "song 1111", ItemId = 9
                },
                new ItemCount()
                {
                    Count = 51 * multiplier, Name = "song something", ItemId = 204
                },
                new ItemCount()
                {
                    Count = 51 * multiplier, Name = "something song", ItemId = 513
                }
            }));
        }
        public async Task <List <ItemCount> > GetSongPlayedOverTime(AggregateTimeRange timeRange, int artistWorkId)
        {
            string           localStorageKey = LOCALSTORAGEKEY_ARTISTWORKPLAYEDOVERTIME + artistWorkId + "-" + timeRange;
            List <ItemCount> result;
            TimeCachedObject <List <ItemCount> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ItemCount> > >(localStorageKey);


            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistWorkAggregatedEventsRequest request = new ArtistWorkAggregatedEventsRequest()
                {
                    ArtistWorkIds = new List <int>()
                    {
                        artistWorkId
                    }, TimeSeries = timeRange
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistWorkAggregatedEvents", request);

                string responseBody = await responseMessage.Content.ReadAsStringAsync();

                AggregatedEvent  temp  = JsonConvert.DeserializeObject <List <AggregatedEvent> >(responseBody)?.FirstOrDefault();
                List <ItemCount> items = temp.AggregatedEventSumSource.Select((x, i) => new ItemCount()
                {
                    Count = x.Value, ItemId = artistWorkId, Name = x.Timestamp.ToString()
                }).ToList();
                Dictionary <string, (ItemCount itemCount, DateTimeOffset sortOrder)> buckets = GetItemCountBuckets(timeRange, artistWorkId);
                foreach (var item in items)
                {
                    if (buckets.ContainsKey(item.Name))
                    {
                        buckets[item.Name].itemCount.Count = item.Count;
                    }
                }
                result = buckets.Values.OrderBy(x => x.sortOrder).Select(x => x.itemCount).ToList();
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ItemCount> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        public async Task <List <ItemCount> > GetSongPlayedAndOtherPlayed(AggregateTimeRange timeRange, int artistWorkId)
        {
            List <ItemCount> result = new List <ItemCount>();

            string localStorageKey = LOCALSTORAGEKEY_ARTISTARTISTWORKSPLAYEDANDOTHERS + artistWorkId + "-" + timeRange;
            TimeCachedObject <List <ItemCount> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ItemCount> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistInfo       info  = (await _radiocomArtistWorkRepository.GetArtistWorkAsync(artistWorkId)).ArtistInfo;
                List <ItemCount> works = await GetArtistSongsPlayed(timeRange, info.Id);

                long      otherCount = works.Where(x => x.ItemId != artistWorkId).Sum(x => x.Count);
                ItemCount work       = works.FirstOrDefault(x => x.ItemId == artistWorkId);
                if (work != null)
                {
                    result.Add(work);
                }
                else
                {
                    result.Add(new ItemCount()
                    {
                        Count  = 0,
                        ItemId = artistWorkId,
                        Name   = info.Name
                    });
                }
                result.Add(new ItemCount()
                {
                    Count  = otherCount,
                    Name   = "Other",
                    ItemId = -1
                });
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ItemCount> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        public async Task <List <ItemCount> > GetSongPlayedAndOtherPlayed(AggregateTimeRange timeRange, int artistWorkId)
        {
            int multiplier          = GetTimeRangeMultipler(timeRange);
            List <ItemCount> result = new List <ItemCount>()
            {
                new ItemCount()
                {
                    Count = 98 * multiplier, Name = "Other", ItemId = 0
                },
                new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(1, timeRange), ItemId = artistWorkId
                },
            };

            return(await Task.FromResult(result));
        }
 public TopPlayedArtistSongsChart()
 {
     HeaderButtonConfigs = new List <HeaderButtonState>()
     {
         new HeaderButtonState()
         {
             Text = "7 Days", ButtonColor = Color.Secondary, Active = true, ButtonClickCallback = EventCallback.Factory.Create(this, () => UpdateChartDataTimeRange(AggregateTimeRange.SevenDays))
         },
         new HeaderButtonState()
         {
             Text = "3 Months", ButtonClickCallback = EventCallback.Factory.Create(this, () => UpdateChartDataTimeRange(AggregateTimeRange.ThreeMonths))
         },
         new HeaderButtonState()
         {
             Text = "One Year", ButtonClickCallback = EventCallback.Factory.Create(this, () => UpdateChartDataTimeRange(AggregateTimeRange.OneYear))
         }
     };
     ChartDataTimeRange = AggregateTimeRange.SevenDays;
 }
        public async Task <List <ItemCount> > GetArtistSongsPlayed(AggregateTimeRange timeRange, int artistId)
        {
            List <ItemCount> result = new List <ItemCount>();

            string localStorageKey = LOCALSTORAGEKEY_ARTISTARTISTWORKSPLAYED + artistId + "-" + timeRange;
            TimeCachedObject <List <ItemCount> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ItemCount> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                Artist_TopArtistWorkAggregatedEventsRequest request = new Artist_TopArtistWorkAggregatedEventsRequest()
                {
                    ArtistId = artistId, TimeSeries = timeRange, TopN = 100
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "Artist_TopArtistWorkAggregatedEvents", request);

                string responseBody = await responseMessage.Content.ReadAsStringAsync();

                List <AggregatedEvent> temp = JsonConvert.DeserializeObject <List <AggregatedEvent> >(responseBody);
                foreach (var work in temp)
                {
                    result.Add(new ItemCount()
                    {
                        Count  = work.AggregatedEventSum,
                        ItemId = work.Id,
                        Name   = (await _radiocomArtistWorkRepository.GetArtistWorkAsync(work.Id)).Name
                    });
                }
                result = result.OrderByDescending(x => x.Count).ThenBy(x => x.Name).ToList();
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ItemCount> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        public async Task <List <ItemCount> > GetMostPlayedSongsAsync(AggregateTimeRange timeRange)
        {
            List <ItemCount> result          = new List <ItemCount>();
            string           localStorageKey = LOCALSTORAGEKEY_MOSTPLAYEDARTISTWORKS + timeRange;
            TimeCachedObject <List <ItemCount> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ItemCount> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                List <AggregatedEvent> artistWorks = await GetAllArtistWorkAggregatedEvents(timeRange);

                foreach (var work in artistWorks
                         .OrderByDescending(x => x.AggregatedEventSum)
                         .Take(6))
                {
                    result.Add(new ItemCount()
                    {
                        Count  = work.AggregatedEventSum,
                        ItemId = work.Id,
                        Name   = (await _radiocomArtistWorkRepository.GetArtistWorkAsync(work.Id)).Name
                    });

                    result = result.OrderByDescending(x => x.Count).ThenBy(x => x.Name).ToList();

                    DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                    cachedObject = new TimeCachedObject <List <ItemCount> >()
                    {
                        CachedObject   = result,
                        NextUpdateHour = nextUpdate
                    };
                    await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
                }
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        public async Task <int> GetTotalUniqueArtistsAsync(AggregateTimeRange timeRange)
        {
            int    result;
            string localStorageKey = LOCALSTORAGEKEY_ARTISTUNIQUECOUNT + timeRange;
            TimeCachedObject <int> cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <int> >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                result = (await GetAllArtistAggregatedEvents(timeRange))?.Count(x => x.AggregatedEventSum > 0) ?? 0;
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <int>()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        private Dictionary <string, (ItemCount itemCount, DateTimeOffset sortOrder)> GetItemCountBuckets(AggregateTimeRange timeRange, int id)
        {
            Dictionary <string, (ItemCount itemCount, DateTimeOffset sortOrder)> result = new Dictionary <string, (ItemCount itemCount, DateTimeOffset sortOrder)>();
            DateTimeOffset now = DateTimeOffset.UtcNow.Add(DateTimeOffset.Now.Offset);

            now = now.Subtract(new TimeSpan(now.Hour, now.Minute, now.Second));
            switch (timeRange)
            {
            case AggregateTimeRange.None:
                throw new InvalidEnumArgumentException("Must specify time range value other than None");

            case AggregateTimeRange.SevenDays:
                for (int i = 0; i < 8; i++)
                {
                    string         name = GetOverTimeName(i, timeRange, now);
                    DateTimeOffset key  = now.AddDays(i * -1);
                    result[key.ToString()] = (new ItemCount()
                    {
                        Name = name, ItemId = id
                    }, key);
                }
                break;

            case AggregateTimeRange.ThreeMonths:
                for (int i = 0; i < 12; i++)
                {
                    string name = GetOverTimeName(i, timeRange, now);

                    DateTimeOffset key = now.AddDays(-1 * (int)now.DayOfWeek).AddDays(i * -7);
                    result[key.ToString()] = (new ItemCount()
                    {
                        Name = name, ItemId = id
                    }, key);
                }
                break;

            case AggregateTimeRange.OneYear:
                now = now.Subtract(TimeSpan.FromDays(now.Day - 1));
                for (int i = 0; i < 12; i++)
                {
                    string         name = GetOverTimeName(i, timeRange, now);
                    DateTimeOffset key  = now.AddMonths(i * -1);
                    result[key.ToString()] = (new ItemCount()
                    {
                        Name = name, ItemId = id
                    }, key);
                }
                break;

            default:
                break;
            }
            return(result);
        }
        public async Task <List <ItemCount> > GetArtistPlayedOverTime(AggregateTimeRange timeRange, int artistId)
        {
            int multiplier = GetTimeRangeMultipler(timeRange);

            List <ItemCount> result = new List <ItemCount>()
            {
                new ItemCount()
                {
                    Count = 10 * multiplier, Name = GetOverTimeName(0, timeRange), ItemId = artistId
                },
                new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(1, timeRange), ItemId = artistId
                },
                new ItemCount()
                {
                    Count = 7 * multiplier, Name = GetOverTimeName(2, timeRange), ItemId = artistId
                },
                new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(3, timeRange), ItemId = artistId
                },
                new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(4, timeRange), ItemId = artistId
                },
                new ItemCount()
                {
                    Count = 1 * multiplier, Name = GetOverTimeName(5, timeRange), ItemId = artistId
                },
                new ItemCount()
                {
                    Count = 0 * multiplier, Name = GetOverTimeName(6, timeRange), ItemId = artistId
                },
            };

            if (timeRange == AggregateTimeRange.ThreeMonths || timeRange == AggregateTimeRange.OneYear)
            {
                result.Add(new ItemCount()
                {
                    Count = 10 * multiplier, Name = GetOverTimeName(7, timeRange), ItemId = artistId
                });
                result.Add(new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(8, timeRange), ItemId = artistId
                });
                result.Add(new ItemCount()
                {
                    Count = 7 * multiplier, Name = GetOverTimeName(9, timeRange), ItemId = artistId
                });
                result.Add(new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(10, timeRange), ItemId = artistId
                });
                result.Add(new ItemCount()
                {
                    Count = 2 * multiplier, Name = GetOverTimeName(11, timeRange), ItemId = artistId
                });
            }
            result.Reverse();
            return(await Task.FromResult(result));
        }
 public Task <List <ItemCount> > GetMostPlayedSongsAsync(AggregateTimeRange timeRange, int artistId)
 {
     return(Task.FromResult(new List <ItemCount>()));
 }
        public async Task <int> GetTotalUniqueArtistsAsync(AggregateTimeRange timeRange)
        {
            int multiplier = GetTimeRangeMultipler(timeRange);

            return(await Task.FromResult(multiplier *_random.Next(300, 350)));
        }
 private async Task UpdateChartDataTimeRange(AggregateTimeRange mostPlayedTimeRange)
 {
     ChartDataTimeRange = mostPlayedTimeRange;
     await Chart.RefreshChartData();
 }
 private async Task UpdateChartDataTimeRange(AggregateTimeRange aggregateTimeRange)
 {
     AggregateTimeRange = aggregateTimeRange;
     await Chart.RefreshChartData();
 }