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);
        }
        public async Task <IEnumerable <ArtistWorkInfo> > GetArtist_ArtistWorks(int artistId)
        {
            List <ArtistWorkInfo> result;
            string localStorageKey = LOCALSTORAGEKEY_ARTISTARTISTWORK_INFO + "all";
            TimeCachedObject <List <ArtistWorkInfo> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ArtistWorkInfo> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                result = (await GetArtistWorksAsync()).Where(x => x.ArtistInfo.Id == artistId).ToList();
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ArtistWorkInfo> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }

            return(result);
        }
Exemplo n.º 3
0
        public async Task <IEnumerable <ArtistInfo> > GetArtistsAsync()
        {
            string            localStorageKey = LOCALSTORAGEKEY_ARTIST_INFO + "all";
            List <ArtistInfo> result;
            TimeCachedObject <List <ArtistInfo> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ArtistInfo> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistInfosRequest request = new ArtistInfosRequest()
                {
                    ArtistIds = Enumerable.Empty <int>()
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistInfos", request);

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

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

                cachedObject = new TimeCachedObject <List <ArtistInfo> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
        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> > 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 <IEnumerable <ArtistWorkInfo> > GetArtistWorksAsync()
        {
            string localStorageKey = LOCALSTORAGEKEY_ARTISTWORK_INFO + "all";
            List <ArtistWorkInfo> result;
            TimeCachedObject <List <ArtistWorkInfo> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ArtistWorkInfo> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistWorkInfosRequest request = new ArtistWorkInfosRequest()
                {
                    ArtistWorkIds = Enumerable.Empty <int>()
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistWorkInfos", request);

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

                List <ArtistWorkInfoSource> temp    = JsonConvert.DeserializeObject <List <ArtistWorkInfoSource> >(responseBody);
                IEnumerable <ArtistInfo>    artists = await _radiocomArtistRepository.GetArtistsAsync();

                result = artists.Join(temp, x => x.Id, y => y.ArtistId, (x, y) => new ArtistWorkInfo()
                {
                    ArtistInfo = x, Id = y.Id, Name = y.Title
                }).ToList();
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ArtistWorkInfo> >()
                {
                    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);
        }