ReportStatBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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, ReportHelper.GetServerLocalizedString("HeaderCountries"), 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, ReportHelper.GetServerLocalizedString("LabelCommunityRating"), 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, ReportHelper.GetServerLocalizedString("HeaderGenres"), 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, ReportHelper.GetServerLocalizedString("HeaderParentalRatings"), 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<string> t = new List<string> { PersonType.Actor, PersonType.Composer, PersonType.Director, PersonType.GuestStar, PersonType.Producer, PersonType.Writer, "Artist", "AlbumArtist" };
  181. foreach (var item in t)
  182. {
  183. var ps = items.Where(x => x.People != null && x.SupportsPeople).SelectMany(x => x.People)
  184. .Where(n => n.Type == item)
  185. .GroupBy(x => x.Name)
  186. .OrderByDescending(x => x.Count())
  187. .Take(topItem);
  188. if (ps != null && ps.Count() > 0)
  189. this.GetGroups(result, ReportHelper.GetServerLocalizedString("Option" + item), topItem,
  190. ps.Select(x => new ReportStatItem
  191. {
  192. Name = x.Key,
  193. Value = x.Count().ToString(),
  194. Id = GetPersonID(x.Key)
  195. })
  196. );
  197. }
  198. return result;
  199. }
  200. /// <summary> Gets result production years. </summary>
  201. /// <param name="result"> The result. </param>
  202. /// <param name="items"> The items. </param>
  203. /// <param name="topItem"> The top item. </param>
  204. /// <returns> The result production years. </returns>
  205. private ReportStatResult GetResultProductionYears(ReportStatResult result, BaseItem[] items, int topItem = 5)
  206. {
  207. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderYears"), topItem,
  208. items.Where(x => x.ProductionYear != null && x.ProductionYear > 0)
  209. .GroupBy(x => x.ProductionYear)
  210. .OrderByDescending(x => x.Count())
  211. .Take(topItem)
  212. .Select(x => new ReportStatItem
  213. {
  214. Name = x.Key.ToString(),
  215. Value = x.Count().ToString()
  216. })
  217. );
  218. return result;
  219. }
  220. /// <summary> Gets result studios. </summary>
  221. /// <param name="result"> The result. </param>
  222. /// <param name="items"> The items. </param>
  223. /// <param name="topItem"> The top item. </param>
  224. /// <returns> The result studios. </returns>
  225. private ReportStatResult GetResultStudios(ReportStatResult result, BaseItem[] items, int topItem = 5)
  226. {
  227. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderStudios"), topItem,
  228. items.SelectMany(x => x.Studios)
  229. .GroupBy(x => x)
  230. .OrderByDescending(x => x.Count())
  231. .Take(topItem)
  232. .Select(x => new ReportStatItem
  233. {
  234. Name = x.Key,
  235. Value = x.Count().ToString(),
  236. Id = GetStudioID(x.Key)
  237. })
  238. );
  239. return result;
  240. }
  241. #endregion
  242. }
  243. }