ReportStatBuilder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /// <summary>
  16. /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportStatBuilder class. </summary>
  17. /// <param name="libraryManager"> Manager for library. </param>
  18. public ReportStatBuilder(ILibraryManager libraryManager)
  19. : base(libraryManager)
  20. {
  21. }
  22. /// <summary> Gets report stat result. </summary>
  23. /// <param name="items"> The items. </param>
  24. /// <param name="reportRowType"> Type of the report row. </param>
  25. /// <param name="topItem"> The top item. </param>
  26. /// <returns> The report stat result. </returns>
  27. public ReportStatResult GetReportStatResult(BaseItem[] items, ReportViewType reportRowType, 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 (reportRowType)
  38. {
  39. case ReportViewType.Season:
  40. case ReportViewType.Series:
  41. case ReportViewType.MusicAlbum:
  42. case ReportViewType.MusicArtist:
  43. case ReportViewType.Game:
  44. break;
  45. case ReportViewType.Movie:
  46. case ReportViewType.BoxSet:
  47. break;
  48. case ReportViewType.Book:
  49. case ReportViewType.Episode:
  50. case ReportViewType.Video:
  51. case ReportViewType.MusicVideo:
  52. case ReportViewType.Trailer:
  53. case ReportViewType.Audio:
  54. case ReportViewType.BaseItem:
  55. default:
  56. break;
  57. }
  58. result.Groups = result.Groups.OrderByDescending(n => n.Items.Count()).ToList();
  59. return result;
  60. }
  61. private ReportStatResult GetResultGenres(ReportStatResult result, BaseItem[] items, int topItem = 5)
  62. {
  63. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderGenres"), topItem,
  64. items.SelectMany(x => x.Genres)
  65. .GroupBy(x => x)
  66. .OrderByDescending(x => x.Count())
  67. .Take(topItem)
  68. .Select(x => new ReportStatItem
  69. {
  70. Name = x.Key,
  71. Value = x.Count().ToString(),
  72. Id = GetGenreID(x.Key)
  73. }));
  74. return result;
  75. }
  76. private ReportStatResult GetResultStudios(ReportStatResult result, BaseItem[] items, int topItem = 5)
  77. {
  78. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderStudios"), topItem,
  79. items.SelectMany(x => x.Studios)
  80. .GroupBy(x => x)
  81. .OrderByDescending(x => x.Count())
  82. .Take(topItem)
  83. .Select(x => new ReportStatItem
  84. {
  85. Name = x.Key,
  86. Value = x.Count().ToString(),
  87. Id = GetStudioID(x.Key)
  88. })
  89. );
  90. return result;
  91. }
  92. private ReportStatResult GetResultPersons(ReportStatResult result, BaseItem[] items, int topItem = 5)
  93. {
  94. List<string> t = new List<string> { PersonType.Actor, PersonType.Composer, PersonType.Director, PersonType.GuestStar, PersonType.Producer, PersonType.Writer, "Artist", "AlbumArtist" };
  95. foreach (var item in t)
  96. {
  97. this.GetGroups(result, ReportHelper.GetServerLocalizedString("Option" + item), topItem,
  98. items.SelectMany(x => x.People)
  99. .Where(n => n.Type == item)
  100. .GroupBy(x => x.Name)
  101. .OrderByDescending(x => x.Count())
  102. .Take(topItem)
  103. .Select(x => new ReportStatItem
  104. {
  105. Name = x.Key,
  106. Value = x.Count().ToString(),
  107. Id = GetPersonID(x.Key)
  108. })
  109. );
  110. }
  111. return result;
  112. }
  113. private ReportStatResult GetResultCommunityRatings(ReportStatResult result, BaseItem[] items, int topItem = 5)
  114. {
  115. this.GetGroups(result, ReportHelper.GetServerLocalizedString("LabelCommunityRating"), topItem,
  116. items.Where(x => x.CommunityRating != null && x.CommunityRating > 0)
  117. .GroupBy(x => x.CommunityRating)
  118. .OrderByDescending(x => x.Count())
  119. .Take(topItem)
  120. .Select(x => new ReportStatItem
  121. {
  122. Name = x.Key.ToString(),
  123. Value = x.Count().ToString()
  124. })
  125. );
  126. return result;
  127. }
  128. private ReportStatResult GetResultParentalRatings(ReportStatResult result, BaseItem[] items, int topItem = 5)
  129. {
  130. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderParentalRatings"), topItem,
  131. items.Where(x => x.OfficialRating != null)
  132. .GroupBy(x => x.OfficialRating)
  133. .OrderByDescending(x => x.Count())
  134. .Take(topItem)
  135. .Select(x => new ReportStatItem
  136. {
  137. Name = x.Key.ToString(),
  138. Value = x.Count().ToString()
  139. })
  140. );
  141. return result;
  142. }
  143. private ReportStatResult GetResultProductionYears(ReportStatResult result, BaseItem[] items, int topItem = 5)
  144. {
  145. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderYears"), topItem,
  146. items.Where(x => x.ProductionYear != null && x.ProductionYear > 0)
  147. .GroupBy(x => x.ProductionYear)
  148. .OrderByDescending(x => x.Count())
  149. .Take(topItem)
  150. .Select(x => new ReportStatItem
  151. {
  152. Name = x.Key.ToString(),
  153. Value = x.Count().ToString()
  154. })
  155. );
  156. return result;
  157. }
  158. private ReportStatResult GetResulProductionLocations(ReportStatResult result, BaseItem[] items, int topItem = 5)
  159. {
  160. this.GetGroups(result, ReportHelper.GetServerLocalizedString("HeaderCountries"), topItem,
  161. items.OfType<IHasProductionLocations>()
  162. .Where(x => x.ProductionLocations != null)
  163. .SelectMany(x => x.ProductionLocations)
  164. .GroupBy(x => x)
  165. .OrderByDescending(x => x.Count())
  166. .Take(topItem)
  167. .Select(x => new ReportStatItem
  168. {
  169. Name = x.Key.ToString(),
  170. Value = x.Count().ToString()
  171. })
  172. );
  173. return result;
  174. }
  175. /// <summary> Gets the groups. </summary>
  176. /// <param name="result"> The result. </param>
  177. /// <param name="header"> The header. </param>
  178. /// <param name="topItem"> The top item. </param>
  179. /// <param name="top"> The top. </param>
  180. private void GetGroups(ReportStatResult result, string header, int topItem, IEnumerable<ReportStatItem> top)
  181. {
  182. if (top.Count() > 0)
  183. {
  184. var group = new ReportStatGroup { Header = ReportStatGroup.FormatedHeader(header, topItem) };
  185. group.Items.AddRange(top);
  186. result.Groups.Add(group);
  187. }
  188. }
  189. }
  190. }