Skip to content

Commit

Permalink
fix: fixed stats controller (#4742)
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar authored Aug 23, 2022
1 parent c222f1a commit 47ea64b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions src/Ombi.Core/Engine/UserStatsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,29 @@ public async Task<UserStatsSummary> GetSummary(SummaryRequest request)
{
// get all movie requests
var movies = _movieRequest.GetWithUser();
var filteredMovies = movies.Where(x => x.RequestedDate >= request.From && x.RequestedDate <= request.To);
var filteredMovies = await movies.Where(x => x.RequestedDate >= request.From && x.RequestedDate <= request.To).ToListAsync();
var tv = _tvRequest.GetLite();
var children = tv.SelectMany(x =>
x.ChildRequests.Where(c => c.RequestedDate >= request.From && c.RequestedDate <= request.To));
var children = await tv.SelectMany(x =>
x.ChildRequests.Where(c => c.RequestedDate >= request.From && c.RequestedDate <= request.To)).ToListAsync();

var userMovie = filteredMovies.GroupBy(x => x.RequestedUserId).OrderBy(x => x.Key).FirstOrDefault();
var userTv = children.GroupBy(x => x.RequestedUserId).OrderBy(x => x.Key).FirstOrDefault();

var userMovie = filteredMovies.GroupBy(x => x.RequestedUserId).OrderBy(x => x.Key).FirstOrDefaultAsync();
var userTv = children.GroupBy(x => x.RequestedUserId).OrderBy(x => x.Key).FirstOrDefaultAsync();

var moviesCount = filteredMovies.CountAsync();
var childrenCount = children.CountAsync();
var moviesCount = filteredMovies.Count;
var childrenCount = children.Count;
var availableMovies =
filteredMovies.Select(x => x.MarkedAsAvailable >= request.From && x.MarkedAsAvailable <= request.To).CountAsync();
var availableChildren = children.Where(c => c.MarkedAsAvailable >= request.From && c.MarkedAsAvailable <= request.To).CountAsync();
filteredMovies.Select(x => x.MarkedAsAvailable >= request.From && x.MarkedAsAvailable <= request.To).Count();
var availableChildren = children.Where(c => c.MarkedAsAvailable >= request.From && c.MarkedAsAvailable <= request.To).Count();

return new UserStatsSummary
{
TotalMovieRequests = await moviesCount,
TotalTvRequests = await childrenCount,
CompletedRequestsTv = await availableChildren,
CompletedRequestsMovies = await availableMovies,
MostRequestedUserMovie = (await userMovie).FirstOrDefault()?.RequestedUser ?? new OmbiUser(),
MostRequestedUserTv = (await userTv).FirstOrDefault()?.RequestedUser ?? new OmbiUser(),
TotalMovieRequests = moviesCount,
TotalTvRequests = childrenCount,
CompletedRequestsTv = availableChildren,
CompletedRequestsMovies = availableMovies,
MostRequestedUserMovie = userMovie.FirstOrDefault()?.RequestedUser ?? new OmbiUser(),
MostRequestedUserTv = userTv.FirstOrDefault()?.RequestedUser ?? new OmbiUser(),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ombi/Controllers/V1/StatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public StatsController(IUserStatsEngine eng)
private readonly IUserStatsEngine _statsEngine;

[HttpGet]
public async Task<UserStatsSummary> GetUserStats(SummaryRequest req)
public async Task<UserStatsSummary> GetUserStats([FromQuery]SummaryRequest req)
{
return await _statsEngine.GetSummary(req);
}
Expand Down

0 comments on commit 47ea64b

Please sign in to comment.