UsageEntryPoint.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Plugins;
  6. using MediaBrowser.Controller.Session;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Controller.Configuration;
  14. namespace MediaBrowser.Server.Implementations.EntryPoints
  15. {
  16. /// <summary>
  17. /// Class UsageEntryPoint
  18. /// </summary>
  19. public class UsageEntryPoint : IServerEntryPoint
  20. {
  21. private readonly IApplicationHost _applicationHost;
  22. private readonly IHttpClient _httpClient;
  23. private readonly ILogger _logger;
  24. private readonly ISessionManager _sessionManager;
  25. private readonly IUserManager _userManager;
  26. private readonly IServerConfigurationManager _config;
  27. private readonly ConcurrentDictionary<Guid, ClientInfo> _apps = new ConcurrentDictionary<Guid, ClientInfo>();
  28. public UsageEntryPoint(ILogger logger, IApplicationHost applicationHost, IHttpClient httpClient, ISessionManager sessionManager, IUserManager userManager, IServerConfigurationManager config)
  29. {
  30. _logger = logger;
  31. _applicationHost = applicationHost;
  32. _httpClient = httpClient;
  33. _sessionManager = sessionManager;
  34. _userManager = userManager;
  35. _config = config;
  36. _sessionManager.SessionStarted += _sessionManager_SessionStarted;
  37. }
  38. void _sessionManager_SessionStarted(object sender, SessionEventArgs e)
  39. {
  40. var session = e.SessionInfo;
  41. if (!string.IsNullOrEmpty(session.Client) &&
  42. !string.IsNullOrEmpty(session.DeviceName) &&
  43. !string.IsNullOrEmpty(session.DeviceId) &&
  44. !string.IsNullOrEmpty(session.ApplicationVersion))
  45. {
  46. var keys = new List<string>
  47. {
  48. session.Client,
  49. session.DeviceName,
  50. session.DeviceId,
  51. session.ApplicationVersion
  52. };
  53. var key = string.Join("_", keys.ToArray()).GetMD5();
  54. _apps.GetOrAdd(key, guid => GetNewClientInfo(session));
  55. }
  56. }
  57. private async void ReportNewSession(ClientInfo client)
  58. {
  59. if (!_config.Configuration.EnableAnonymousUsageReporting)
  60. {
  61. return;
  62. }
  63. try
  64. {
  65. await new UsageReporter(_applicationHost, _httpClient, _userManager, _logger)
  66. .ReportAppUsage(client, CancellationToken.None)
  67. .ConfigureAwait(false);
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  72. }
  73. }
  74. private ClientInfo GetNewClientInfo(SessionInfo session)
  75. {
  76. var info = new ClientInfo
  77. {
  78. AppName = session.Client,
  79. AppVersion = session.ApplicationVersion,
  80. DeviceName = session.DeviceName,
  81. DeviceId = session.DeviceId
  82. };
  83. ReportNewSession(info);
  84. return info;
  85. }
  86. public async void Run()
  87. {
  88. await Task.Delay(5000).ConfigureAwait(false);
  89. OnTimerFired();
  90. }
  91. /// <summary>
  92. /// Called when [timer fired].
  93. /// </summary>
  94. private async void OnTimerFired()
  95. {
  96. if (!_config.Configuration.EnableAnonymousUsageReporting)
  97. {
  98. return;
  99. }
  100. try
  101. {
  102. await new UsageReporter(_applicationHost, _httpClient, _userManager, _logger)
  103. .ReportServerUsage(CancellationToken.None)
  104. .ConfigureAwait(false);
  105. }
  106. catch (Exception ex)
  107. {
  108. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  109. }
  110. }
  111. public void Dispose()
  112. {
  113. _sessionManager.SessionStarted -= _sessionManager_SessionStarted;
  114. }
  115. }
  116. }