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