Skip to content

Commit

Permalink
format data fetched from for artistwork piechart and cache
Browse files Browse the repository at this point in the history
  • Loading branch information
czf committed Apr 18, 2021
1 parent 50c72ee commit 8834f6a
Showing 1 changed file with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Newtonsoft.Json;
using RadiocomDataViewApp.Interfaces;
using RadiocomDataViewApp.Objects;
using RadiocomDataViewApp.Objects.Dto;

namespace RadiocomDataViewApp.Clients.Live
{
Expand All @@ -26,6 +27,8 @@ public class LiveRadiocomDataAggregateDataClient : IRadiocomDataAggregateDataCli

private const string LOCALSTORAGEKEY_ARTISTMOSTPLAYEDARTISTWORKS = "artistmostplayedartistworks-";
private const string LOCALSTORAGEKEY_ARTISTARTISTWORKSPLAYED = "artistartistworksplayed-";
private const string LOCALSTORAGEKEY_ARTISTARTISTWORKSPLAYEDANDOTHERS = "artistartistworksplayedandothers-";


private readonly HttpClient _httpClient;
private readonly ILocalStorageService _localStorageService;
Expand Down Expand Up @@ -230,9 +233,51 @@ public async Task<List<ItemCount>> GetMostPlayedSongsAsync(AggregateTimeRange ti
return result;
}

public Task<List<ItemCount>> GetSongPlayedAndOtherPlayed(AggregateTimeRange timeRange, int artistWorkId)
public async Task<List<ItemCount>> GetSongPlayedAndOtherPlayed(AggregateTimeRange timeRange, int artistWorkId)
{
return Task.FromResult(new List<ItemCount>());
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>> GetSongPlayedOverTime(AggregateTimeRange timeRange, int artistWorkId)
Expand Down

0 comments on commit 8834f6a

Please sign in to comment.