ReportStatBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. namespace MediaBrowser.Api.Reports
  7. {
  8. /// <summary> A report stat builder. </summary>
  9. /// <seealso cref="T:MediaBrowser.Api.Reports.ReportBuilderBase"/>
  10. public class ReportStatBuilder : ReportBuilderBase
  11. {
  12. #region [Constructors]
  13. /// <summary>
  14. /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportStatBuilder class. </summary>
  15. /// <param name="libraryManager"> Manager for library. </param>
  16. public ReportStatBuilder(ILibraryManager libraryManager)
  17. : base(libraryManager)
  18. {
  19. }
  20. #endregion
  21. #region [Public Methods]
  22. /// <summary> Gets report stat result. </summary>
  23. /// <param name="items"> The items. </param>
  24. /// <param name="reportIncludeItemTypes"> List of types of the report include items. </param>
  25. /// <param name="topItem"> The top item. </param>
  26. /// <returns> The report stat result. </returns>
  27. public ReportStatResult GetResult(BaseItem[] items, ReportIncludeItemTypes reportIncludeItemTypes, int topItem = 5)
  28. {
  29. ReportStatResult result = new ReportStatResult();
  30. result = this.GetResultGenres(result, items, topItem);
  31. result = this.GetResultStudios(result, items, topItem);
  32. result = this.GetResultPersons(result, items, topItem);
  33. result = this.GetResultProductionYears(result, items, topItem);
  34. result = this.GetResulProductionLocations(result, items, topItem);
  35. result = this.GetResultCommunityRatings(result, items, topItem);
  36. result = this.GetResultParentalRatings(result, items, topItem);
  37. switch (reportIncludeItemTypes)
  38. {
  39. case ReportIncludeItemTypes.Season:
  40. case ReportIncludeItemTypes.Series:
  41. case ReportIncludeItemTypes.MusicAlbum:
  42. case ReportIncludeItemTypes.MusicArtist:
  43. case ReportIncludeItemTypes.Game:
  44. break;
  45. case ReportIncludeItemTypes.Movie:
  46. case ReportIncludeItemTypes.BoxSet:
  47. break;
  48. case ReportIncludeItemTypes.Book:
  49. case ReportIncludeItemTypes.Episode:
  50. case ReportIncludeItemTypes.Video:
  51. case ReportIncludeItemTypes.MusicVideo:
  52. case ReportIncludeItemTypes.Trailer:
  53. case ReportIncludeItemTypes.Audio:
  54. case ReportIncludeItemTypes.BaseItem:
  55. default:
  56. break;
  57. }
  58. result.Groups = result.Groups.OrderByDescending(n => n.Items.Count()).ToList();
  59. return result;
  60. }
  61. #endregion
  62. #region [Protected Internal Methods]
  63. /// <summary> Gets the headers. </summary>
  64. /// <typeparam name="H"> Type of the header. </typeparam>
  65. /// <param name="request"> The request. </param>
  66. /// <returns> The headers. </returns>
  67. /// <seealso cref="M:MediaBrowser.Api.Reports.ReportBuilderBase.GetHeaders{H}(H)"/>
  68. protected internal override List<ReportHeader> GetHeaders<H>(H request)
  69. {
  70. throw new NotImplementedException();
  71. }
  72. #endregion
  73. #region [Private Methods]
  74. /// <summary> Gets the groups. </summary>
  75. /// <param name="result"> The result. </param>
  76. /// <param name="header"> The header. </param>
  77. /// <param name="topItem"> The top item. </param>
  78. /// <param name="top"> The top. </param>
  79. private void GetGroups(ReportStatResult result, string header, int topItem, IEnumerable<ReportStatItem> top)
  80. {
  81. if (top != null && top.Count() > 0)
  82. {
  83. var group = new ReportStatGroup { Header = ReportStatGroup.FormatedHeader(header, topItem) };
  84. group.Items.AddRange(top);
  85. result.Groups.Add(group);
  86. }
  87. }
  88. /// <summary> Gets resul production locations. </summary>
  89. /// <param name="result"> The result. </param>
  90. /// <param name="items"> The items. </param>
  91. /// <param name="topItem"> The top item. </param>
  92. /// <returns> The resul production locations. </returns>
  93. private ReportStatResult GetResulProductionLocations(ReportStatResult result, BaseItem[] items, int topItem = 5)
  94. {
  95. this.GetGroups(result, GetLocalizedHeader(HeaderMetadata.Countries), topItem,
  96. items.OfType<IHasProductionLocations>()
  97. .Where(x => x.ProductionLocations != null)
  98. .SelectMany(x => x.ProductionLocations)
  99. .GroupBy(x => x)
  100. .OrderByDescending(x => x.Count())
  101. .Take(topItem)
  102. .Select(x => new ReportStatItem
  103. {
  104. Name = x.Key.ToString(),
  105. Value = x.Count().ToString()
  106. })
  107. );
  108. return result;
  109. }
  110. /// <summary> Gets result community ratings. </summary>
  111. /// <param name="result"> The result. </param>
  112. /// <param name="items"> The items. </param>
  113. /// <param name="topItem"> The top item. </param>
  114. /// <returns> The result community ratings. </returns>
  115. private ReportStatResult GetResultCommunityRatings(ReportStatResult result, BaseItem[] items, int topItem = 5)
  116. {
  117. this.GetGroups(result, GetLocalizedHeader(HeaderMetadata.CommunityRating), topItem,
  118. items.Where(x => x.CommunityRating != null && x.CommunityRating > 0)
  119. .GroupBy(x => x.CommunityRating)
  120. .OrderByDescending(x => x.Count())
  121. .Take(topItem)
  122. .Select(x => new ReportStatItem
  123. {
  124. Name = x.Key.ToString(),
  125. Value = x.Count().ToString()
  126. })
  127. );
  128. return result;
  129. }
  130. /// <summary> Gets result genres. </summary>
  131. /// <param name="result"> The result. </param>
  132. /// <param name="items"> The items. </param>
  133. /// <param name="topItem"> The top item. </param>
  134. /// <returns> The result genres. </returns>
  135. private ReportStatResult GetResultGenres(ReportStatResult result, BaseItem[] items, int topItem = 5)
  136. {
  137. this.GetGroups(result, GetLocalizedHeader(HeaderMetadata.Genres), topItem,
  138. items.SelectMany(x => x.Genres)
  139. .GroupBy(x => x)
  140. .OrderByDescending(x => x.Count())
  141. .Take(topItem)
  142. .Select(x => new ReportStatItem
  143. {
  144. Name = x.Key,
  145. Value = x.Count().ToString(),
  146. Id = GetGenreID(x.Key)
  147. }));
  148. return result;
  149. }
  150. /// <summary> Gets result parental ratings. </summary>
  151. /// <param name="result"> The result. </param>
  152. /// <param name="items"> The items. </param>
  153. /// <param name="topItem"> The top item. </param>
  154. /// <returns> The result parental ratings. </returns>
  155. private ReportStatResult GetResultParentalRatings(ReportStatResult result, BaseItem[] items, int topItem = 5)
  156. {
  157. this.GetGroups(result, GetLocalizedHeader(HeaderMetadata.ParentalRatings), topItem,
  158. items.Where(x => x.OfficialRating != null)
  159. .GroupBy(x => x.OfficialRating)
  160. .OrderByDescending(x => x.Count())
  161. .Take(topItem)
  162. .Select(x => new ReportStatItem
  163. {
  164. Name = x.Key.ToString(),
  165. Value = x.Count().ToString()
  166. })
  167. );
  168. return result;
  169. }
  170. /// <summary> Gets result persons. </summary>
  171. /// <param name="result"> The result. </param>
  172. /// <param name="items"> The items. </param>
  173. /// <param name="topItem"> The top item. </param>
  174. /// <returns> The result persons. </returns>
  175. private ReportStatResult GetResultPersons(ReportStatResult result, BaseItem[] items, int topItem = 5)
  176. {
  177. List<HeaderMetadata> t = new List<HeaderMetadata>
  178. {
  179. HeaderMetadata.Actor,
  180. HeaderMetadata.Composer,
  181. HeaderMetadata.Director,
  182. HeaderMetadata.GuestStar,
  183. HeaderMetadata.Producer,
  184. HeaderMetadata.Writer,
  185. HeaderMetadata.Artist,
  186. HeaderMetadata.AlbumArtist
  187. };
  188. foreach (var item in t)
  189. {
  190. var ps = items.SelectMany(x => _libraryManager.GetPeople(x))
  191. .Where(n => n.Type == item.ToString())
  192. .GroupBy(x => x.Name)
  193. .OrderByDescending(x => x.Count())
  194. .Take(topItem);
  195. if (ps != null && ps.Count() > 0)
  196. this.GetGroups(result, GetLocalizedHeader(item), topItem,
  197. ps.Select(x => new ReportStatItem
  198. {
  199. Name = x.Key,
  200. Value = x.Count().ToString(),
  201. Id = GetPersonID(x.Key)
  202. })
  203. );
  204. }
  205. return result;
  206. }
  207. /// <summary> Gets result production years. </summary>
  208. /// <param name="result"> The result. </param>
  209. /// <param name="items"> The items. </param>
  210. /// <param name="topItem"> The top item. </param>
  211. /// <returns> The result production years. </returns>
  212. private ReportStatResult GetResultProductionYears(ReportStatResult result, BaseItem[] items, int topItem = 5)
  213. {
  214. this.GetGroups(result, GetLocalizedHeader(HeaderMetadata.Year), topItem,
  215. items.Where(x => x.ProductionYear != null && x.ProductionYear > 0)
  216. .GroupBy(x => x.ProductionYear)
  217. .OrderByDescending(x => x.Count())
  218. .Take(topItem)
  219. .Select(x => new ReportStatItem
  220. {
  221. Name = x.Key.ToString(),
  222. Value = x.Count().ToString()
  223. })
  224. );
  225. return result;
  226. }
  227. /// <summary> Gets result studios. </summary>
  228. /// <param name="result"> The result. </param>
  229. /// <param name="items"> The items. </param>
  230. /// <param name="topItem"> The top item. </param>
  231. /// <returns> The result studios. </returns>
  232. private ReportStatResult GetResultStudios(ReportStatResult result, BaseItem[] items, int topItem = 5)
  233. {
  234. this.GetGroups(result, GetLocalizedHeader(HeaderMetadata.Studios), topItem,
  235. items.SelectMany(x => x.Studios)
  236. .GroupBy(x => x)
  237. .OrderByDescending(x => x.Count())
  238. .Take(topItem)
  239. .Select(x => new ReportStatItem
  240. {
  241. Name = x.Key,
  242. Value = x.Count().ToString(),
  243. Id = GetStudioID(x.Key)
  244. })
  245. );
  246. return result;
  247. }
  248. #endregion
  249. }
  250. }