UsageEntryPoint.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Implementations.Security;
  4. using MediaBrowser.Common.Net;
  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. namespace MediaBrowser.Server.Implementations.EntryPoints
  13. {
  14. /// <summary>
  15. /// Class UsageEntryPoint
  16. /// </summary>
  17. public class UsageEntryPoint : IServerEntryPoint
  18. {
  19. private readonly IApplicationHost _applicationHost;
  20. private readonly INetworkManager _networkManager;
  21. private readonly IHttpClient _httpClient;
  22. private readonly ILogger _logger;
  23. private readonly ISessionManager _sessionManager;
  24. private Timer _timer;
  25. private readonly TimeSpan _frequency = TimeSpan.FromHours(24);
  26. private const string DefaultDeviceVersion = "Unknown version";
  27. private readonly ConcurrentDictionary<Guid, ClientInfo> _apps = new ConcurrentDictionary<Guid, ClientInfo>();
  28. public UsageEntryPoint(ILogger logger, IApplicationHost applicationHost, INetworkManager networkManager, IHttpClient httpClient, ISessionManager sessionManager)
  29. {
  30. _logger = logger;
  31. _applicationHost = applicationHost;
  32. _networkManager = networkManager;
  33. _httpClient = httpClient;
  34. _sessionManager = sessionManager;
  35. _sessionManager.SessionStarted += _sessionManager_SessionStarted;
  36. }
  37. void _sessionManager_SessionStarted(object sender, SessionEventArgs e)
  38. {
  39. var session = e.SessionInfo;
  40. if (!string.IsNullOrEmpty(session.Client) &&
  41. !string.IsNullOrEmpty(session.DeviceName) &&
  42. !string.IsNullOrEmpty(session.DeviceId))
  43. {
  44. var keys = new List<string>
  45. {
  46. session.Client,
  47. session.DeviceName,
  48. session.DeviceId
  49. };
  50. if (!string.IsNullOrEmpty(session.DeviceVersion))
  51. {
  52. keys.Add(session.DeviceVersion);
  53. }
  54. else
  55. {
  56. keys.Add(DefaultDeviceVersion);
  57. }
  58. var key = string.Join("_", keys.ToArray()).GetMD5();
  59. _apps.GetOrAdd(key, guid => GetNewClientInfo(session));
  60. }
  61. }
  62. private async void ReportNewSession(ClientInfo client)
  63. {
  64. try
  65. {
  66. await new UsageReporter(_applicationHost, _networkManager, _httpClient)
  67. .ReportAppUsage(client, CancellationToken.None)
  68. .ConfigureAwait(false);
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  73. }
  74. }
  75. private ClientInfo GetNewClientInfo(SessionInfo session)
  76. {
  77. var info = new ClientInfo
  78. {
  79. AppName = session.Client,
  80. AppVersion = session.ApplicationVersion,
  81. DeviceVersion = session.DeviceVersion,
  82. DeviceId = session.DeviceId
  83. };
  84. if (string.IsNullOrEmpty(info.DeviceVersion))
  85. {
  86. info.DeviceVersion = DefaultDeviceVersion;
  87. }
  88. // Report usage to remote server, except for web client, since we already have data on that
  89. if (!string.Equals(info.AppName, "Dashboard", StringComparison.OrdinalIgnoreCase))
  90. {
  91. ReportNewSession(info);
  92. }
  93. return info;
  94. }
  95. public void Run()
  96. {
  97. _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(5000), _frequency);
  98. }
  99. /// <summary>
  100. /// Called when [timer fired].
  101. /// </summary>
  102. /// <param name="state">The state.</param>
  103. private async void OnTimerFired(object state)
  104. {
  105. try
  106. {
  107. await new UsageReporter(_applicationHost, _networkManager, _httpClient)
  108. .ReportServerUsage(CancellationToken.None)
  109. .ConfigureAwait(false);
  110. }
  111. catch (Exception ex)
  112. {
  113. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  114. }
  115. }
  116. public void Dispose()
  117. {
  118. _sessionManager.SessionStarted -= _sessionManager_SessionStarted;
  119. if (_timer != null)
  120. {
  121. _timer.Dispose();
  122. _timer = null;
  123. }
  124. }
  125. }
  126. }