using System; using System.Collections.Generic; using System.Linq; using Nancy; using Sony.Filtr.AdminAPI.Utility; using Sony.Filtr.AdminAPI.ViewModels.Graph; using Sony.Filtr.Contracts.Abstractions; using Sony.Filtr.Core.Buzz; using Sony.Filtr.Utility; namespace Sony.Filtr.AdminAPI.Modules { public class ArtistGraphModule : BaseModule { public ArtistGraphModule(IApplicationInstanceManager applicationInstanceManager, BuzzArtistManager buzzArtistManager) : base(applicationInstanceManager) { Get["ArtistFollowersGraph", "/artistGraph/followers", true] = async (parameters, cx) => { var artistId = Request.GetRequiredArtistId(); var startDate = Maybe.ToDateTimeOrFallback((string)Request.Query.startDate, DateTime.Today.AddDays(-30)); var endDate = Maybe.ToDateTimeOrFallback((string)Request.Query.endDate, DateTime.Today); var artistFollowers = await buzzArtistManager.GetSpotifyArtistFollowerLogAsync(artistId, startDate.AddDays(-1), endDate); var total = artistFollowers.Skip(1).Select(s => new GraphEntry() { Date = s.Timestamp, Value = s.Followers }).OrderBy(e => e.Date).ToList(); var change = GraphViewModelHelper.BuildDailyChange(total).ToList(); List series = new List { new FollowerSeriesViewModel { FollowerType = FollowerStatType.Total.ToString(), Entries = total }, new FollowerSeriesViewModel { FollowerType = FollowerStatType.DailyChange.ToString(), Entries = change } }; var viewModel = new FollowersGraphViewModel { Series = series, Filter = new FollowersFilter() { StartDate = startDate, EndDate = endDate, ArtistId = artistId, } }; return Response.AsJson(viewModel); }; } } }