update reports

This commit is contained in:
Luke Pulverenti 2016-07-15 15:54:34 -04:00
parent 78e6c638bc
commit e329811d2d
2 changed files with 93 additions and 82 deletions

View File

@ -58,10 +58,10 @@ namespace MediaBrowser.Api.Reports
/// <summary> Gets the given request. </summary> /// <summary> Gets the given request. </summary>
/// <param name="request"> The request. </param> /// <param name="request"> The request. </param>
/// <returns> A Task&lt;object&gt; </returns> /// <returns> A Task&lt;object&gt; </returns>
public async Task<object> Get(GetActivityLogs request) public object Get(GetActivityLogs request)
{ {
request.DisplayType = "Screen"; request.DisplayType = "Screen";
ReportResult result = await GetReportActivities(request).ConfigureAwait(false); ReportResult result = GetReportActivities(request);
return ToOptimizedResult(result); return ToOptimizedResult(result);
} }
@ -104,7 +104,8 @@ namespace MediaBrowser.Api.Reports
return null; return null;
request.DisplayType = "Screen"; request.DisplayType = "Screen";
var reportResult = await GetReportResult(request); var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
var reportResult = await GetReportResult(request, user);
return ToOptimizedResult(reportResult); return ToOptimizedResult(reportResult);
} }
@ -117,7 +118,8 @@ namespace MediaBrowser.Api.Reports
if (string.IsNullOrEmpty(request.IncludeItemTypes)) if (string.IsNullOrEmpty(request.IncludeItemTypes))
return null; return null;
request.DisplayType = "Screen"; request.DisplayType = "Screen";
var reportResult = await GetReportStatistic(request); var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
var reportResult = await GetReportStatistic(request, user);
return ToOptimizedResult(reportResult); return ToOptimizedResult(reportResult);
} }
@ -150,6 +152,7 @@ namespace MediaBrowser.Api.Reports
headers["Content-Disposition"] = string.Format("attachment; filename=\"{0}\"", filename); headers["Content-Disposition"] = string.Format("attachment; filename=\"{0}\"", filename);
headers["Content-Encoding"] = "UTF-8"; headers["Content-Encoding"] = "UTF-8";
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
ReportResult result = null; ReportResult result = null;
switch (reportViewType) switch (reportViewType)
{ {
@ -157,12 +160,12 @@ namespace MediaBrowser.Api.Reports
case ReportViewType.ReportData: case ReportViewType.ReportData:
ReportIncludeItemTypes reportRowType = ReportHelper.GetRowType(request.IncludeItemTypes); ReportIncludeItemTypes reportRowType = ReportHelper.GetRowType(request.IncludeItemTypes);
ReportBuilder dataBuilder = new ReportBuilder(_libraryManager); ReportBuilder dataBuilder = new ReportBuilder(_libraryManager);
QueryResult<BaseItem> queryResult = await GetQueryResult(request).ConfigureAwait(false); QueryResult<BaseItem> queryResult = await GetQueryResult(request, user).ConfigureAwait(false);
result = dataBuilder.GetResult(queryResult.Items, request); result = dataBuilder.GetResult(queryResult.Items, request);
result.TotalRecordCount = queryResult.TotalRecordCount; result.TotalRecordCount = queryResult.TotalRecordCount;
break; break;
case ReportViewType.ReportActivities: case ReportViewType.ReportActivities:
result = await GetReportActivities(request).ConfigureAwait(false); result = GetReportActivities(request);
break; break;
} }
@ -177,23 +180,15 @@ namespace MediaBrowser.Api.Reports
break; break;
} }
object ro = ResultFactory.GetResult(returnResult, contentType, headers); return ResultFactory.GetResult(returnResult, contentType, headers);
return ro;
} }
#endregion #endregion
#region [Private Methods]
/// <summary> Gets items query. </summary>
/// <param name="request"> The request. </param>
/// <param name="user"> The user. </param>
/// <returns> The items query. </returns>
private InternalItemsQuery GetItemsQuery(BaseReportRequest request, User user) private InternalItemsQuery GetItemsQuery(BaseReportRequest request, User user)
{ {
var query = new InternalItemsQuery var query = new InternalItemsQuery(user)
{ {
User = user,
IsPlayed = request.IsPlayed, IsPlayed = request.IsPlayed,
MediaTypes = request.GetMediaTypes(), MediaTypes = request.GetMediaTypes(),
IncludeItemTypes = request.GetIncludeItemTypes(), IncludeItemTypes = request.GetIncludeItemTypes(),
@ -231,6 +226,7 @@ namespace MediaBrowser.Api.Reports
Tags = request.GetTags(), Tags = request.GetTags(),
OfficialRatings = request.GetOfficialRatings(), OfficialRatings = request.GetOfficialRatings(),
Genres = request.GetGenres(), Genres = request.GetGenres(),
GenreIds = request.GetGenreIds(),
Studios = request.GetStudios(), Studios = request.GetStudios(),
StudioIds = request.GetStudioIds(), StudioIds = request.GetStudioIds(),
Person = request.Person, Person = request.Person,
@ -245,9 +241,11 @@ namespace MediaBrowser.Api.Reports
MaxPlayers = request.MaxPlayers, MaxPlayers = request.MaxPlayers,
MinCommunityRating = request.MinCommunityRating, MinCommunityRating = request.MinCommunityRating,
MinCriticRating = request.MinCriticRating, MinCriticRating = request.MinCriticRating,
ParentId = string.IsNullOrWhiteSpace(request.ParentId) ? (Guid?)null : new Guid(request.ParentId),
ParentIndexNumber = request.ParentIndexNumber, ParentIndexNumber = request.ParentIndexNumber,
AiredDuringSeason = request.AiredDuringSeason, AiredDuringSeason = request.AiredDuringSeason,
AlbumArtistStartsWithOrGreater = request.AlbumArtistStartsWithOrGreater AlbumArtistStartsWithOrGreater = request.AlbumArtistStartsWithOrGreater,
EnableTotalRecordCount = request.EnableTotalRecordCount
}; };
if (!string.IsNullOrWhiteSpace(request.Ids)) if (!string.IsNullOrWhiteSpace(request.Ids))
@ -357,70 +355,86 @@ namespace MediaBrowser.Api.Reports
query.AlbumNames = request.Albums.Split('|'); query.AlbumNames = request.Albums.Split('|');
} }
if (request.HasQueryLimit == false)
{
query.StartIndex = null;
query.Limit = null;
}
return query; return query;
} }
/// <summary> Gets query result. </summary> private async Task<QueryResult<BaseItem>> GetQueryResult(BaseReportRequest request, User user)
/// <param name="request"> The request. </param>
/// <returns> The query result. </returns>
private async Task<QueryResult<BaseItem>> GetQueryResult(BaseReportRequest request)
{ {
// Placeholder in case needed later // all report queries currently need this because it's not being specified
request.Recursive = true; request.Recursive = true;
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
request.Fields = "MediaSources,DateCreated,Settings,Studios,SyncInfo,ItemCounts";
var parentItem = string.IsNullOrEmpty(request.ParentId) ?
(user == null ? _libraryManager.RootFolder : user.RootFolder) :
_libraryManager.GetItemById(request.ParentId);
var item = string.IsNullOrEmpty(request.ParentId) ? var item = string.IsNullOrEmpty(request.ParentId) ?
user == null ? _libraryManager.RootFolder : user.RootFolder : user == null ? _libraryManager.RootFolder : user.RootFolder :
parentItem; _libraryManager.GetItemById(request.ParentId);
IEnumerable<BaseItem> items; if (string.Equals(request.IncludeItemTypes, "Playlist", StringComparison.OrdinalIgnoreCase))
{
//item = user == null ? _libraryManager.RootFolder : user.RootFolder;
}
else if (string.Equals(request.IncludeItemTypes, "BoxSet", StringComparison.OrdinalIgnoreCase))
{
item = user == null ? _libraryManager.RootFolder : user.RootFolder;
}
// Default list type = children
var folder = item as Folder;
if (folder == null)
{
folder = user == null ? _libraryManager.RootFolder : _libraryManager.GetUserRootFolder();
}
if (!string.IsNullOrEmpty(request.Ids))
{
request.Recursive = true;
var query = GetItemsQuery(request, user);
var result = await folder.GetItems(query).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(request.SortBy))
{
var ids = query.ItemIds.ToList();
// Try to preserve order
result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id.ToString("N"))).ToArray();
}
return result;
}
if (request.Recursive) if (request.Recursive)
{ {
var result = await ((Folder)item).GetItems(GetItemsQuery(request, user)).ConfigureAwait(false); return await folder.GetItems(GetItemsQuery(request, user)).ConfigureAwait(false);
return result;
} }
else
{
if (user == null) if (user == null)
{ {
var result = await ((Folder)item).GetItems(GetItemsQuery(request, null)).ConfigureAwait(false); return await folder.GetItems(GetItemsQuery(request, null)).ConfigureAwait(false);
return result;
} }
var userRoot = item as UserRootFolder; var userRoot = item as UserRootFolder;
if (userRoot == null) if (userRoot == null)
{ {
var result = await ((Folder)item).GetItems(GetItemsQuery(request, user)).ConfigureAwait(false); return await folder.GetItems(GetItemsQuery(request, user)).ConfigureAwait(false);
return result;
} }
items = ((Folder)item).GetChildren(user, true); IEnumerable<BaseItem> items = folder.GetChildren(user, true);
var itemsArray = items.ToArray();
return new QueryResult<BaseItem>
{
Items = itemsArray,
TotalRecordCount = itemsArray.Length
};
} }
return new QueryResult<BaseItem> { Items = items.ToArray() }; #region [Private Methods]
}
/// <summary> Gets report activities. </summary> /// <summary> Gets report activities. </summary>
/// <param name="request"> The request. </param> /// <param name="request"> The request. </param>
/// <returns> The report activities. </returns> /// <returns> The report activities. </returns>
private Task<ReportResult> GetReportActivities(IReportsDownload request) private ReportResult GetReportActivities(IReportsDownload request)
{
return Task<ReportResult>.Run(() =>
{ {
DateTime? minDate = string.IsNullOrWhiteSpace(request.MinDate) ? DateTime? minDate = string.IsNullOrWhiteSpace(request.MinDate) ?
(DateTime?)null : (DateTime?)null :
@ -437,18 +451,15 @@ namespace MediaBrowser.Api.Reports
var result = builder.GetResult(queryResult, request); var result = builder.GetResult(queryResult, request);
result.TotalRecordCount = queryResult.TotalRecordCount; result.TotalRecordCount = queryResult.TotalRecordCount;
return result; return result;
});
} }
/// <summary> Gets report result. </summary> /// <summary> Gets report result. </summary>
/// <param name="request"> The request. </param> /// <param name="request"> The request. </param>
/// <returns> The report result. </returns> /// <returns> The report result. </returns>
private async Task<ReportResult> GetReportResult(GetItemReport request) private async Task<ReportResult> GetReportResult(GetItemReport request, User user)
{ {
ReportBuilder reportBuilder = new ReportBuilder(_libraryManager); ReportBuilder reportBuilder = new ReportBuilder(_libraryManager);
QueryResult<BaseItem> queryResult = await GetQueryResult(request).ConfigureAwait(false); QueryResult<BaseItem> queryResult = await GetQueryResult(request, user).ConfigureAwait(false);
ReportResult reportResult = reportBuilder.GetResult(queryResult.Items, request); ReportResult reportResult = reportBuilder.GetResult(queryResult.Items, request);
reportResult.TotalRecordCount = queryResult.TotalRecordCount; reportResult.TotalRecordCount = queryResult.TotalRecordCount;
@ -458,10 +469,10 @@ namespace MediaBrowser.Api.Reports
/// <summary> Gets report statistic. </summary> /// <summary> Gets report statistic. </summary>
/// <param name="request"> The request. </param> /// <param name="request"> The request. </param>
/// <returns> The report statistic. </returns> /// <returns> The report statistic. </returns>
private async Task<ReportStatResult> GetReportStatistic(GetReportStatistics request) private async Task<ReportStatResult> GetReportStatistic(GetReportStatistics request, User user)
{ {
ReportIncludeItemTypes reportRowType = ReportHelper.GetRowType(request.IncludeItemTypes); ReportIncludeItemTypes reportRowType = ReportHelper.GetRowType(request.IncludeItemTypes);
QueryResult<BaseItem> queryResult = await GetQueryResult(request).ConfigureAwait(false); QueryResult<BaseItem> queryResult = await GetQueryResult(request, user).ConfigureAwait(false);
ReportStatBuilder reportBuilder = new ReportStatBuilder(_libraryManager); ReportStatBuilder reportBuilder = new ReportStatBuilder(_libraryManager);
ReportStatResult reportResult = reportBuilder.GetResult(queryResult.Items, ReportHelper.GetRowType(request.IncludeItemTypes), request.TopItems ?? 5); ReportStatResult reportResult = reportBuilder.GetResult(queryResult.Items, ReportHelper.GetRowType(request.IncludeItemTypes), request.TopItems ?? 5);

View File

@ -101,7 +101,7 @@ namespace MediaBrowser.Api.UserLibrary
{ {
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null; var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
var result = await GetItemsToSerialize(request, user).ConfigureAwait(false); var result = await GetQueryResult(request, user).ConfigureAwait(false);
if (result == null) if (result == null)
{ {
@ -135,7 +135,7 @@ namespace MediaBrowser.Api.UserLibrary
/// <param name="request">The request.</param> /// <param name="request">The request.</param>
/// <param name="user">The user.</param> /// <param name="user">The user.</param>
/// <returns>IEnumerable{BaseItem}.</returns> /// <returns>IEnumerable{BaseItem}.</returns>
private async Task<QueryResult<BaseItem>> GetItemsToSerialize(GetItems request, User user) private async Task<QueryResult<BaseItem>> GetQueryResult(GetItems request, User user)
{ {
var item = string.IsNullOrEmpty(request.ParentId) ? var item = string.IsNullOrEmpty(request.ParentId) ?
user == null ? _libraryManager.RootFolder : user.RootFolder : user == null ? _libraryManager.RootFolder : user.RootFolder :