ReportStatBuilder.cs 12 KB

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