UsageEntryPoint.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. {
  43. var keys = new List<string>
  44. {
  45. session.Client,
  46. session.DeviceName
  47. };
  48. if (!string.IsNullOrEmpty(session.DeviceVersion))
  49. {
  50. keys.Add(session.DeviceVersion);
  51. }
  52. else
  53. {
  54. keys.Add(DefaultDeviceVersion);
  55. }
  56. var key = string.Join("_", keys.ToArray()).GetMD5();
  57. _apps.GetOrAdd(key, guid => GetNewClientInfo(session));
  58. }
  59. }
  60. private async void ReportNewSession(ClientInfo client)
  61. {
  62. try
  63. {
  64. await new UsageReporter(_applicationHost, _networkManager, _httpClient)
  65. .ReportAppUsage(client, CancellationToken.None)
  66. .ConfigureAwait(false);
  67. }
  68. catch (Exception ex)
  69. {
  70. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  71. }
  72. }
  73. private ClientInfo GetNewClientInfo(SessionInfo session)
  74. {
  75. var info = new ClientInfo
  76. {
  77. AppName = session.Client,
  78. AppVersion = session.ApplicationVersion,
  79. DeviceVersion = session.DeviceVersion
  80. };
  81. if (string.IsNullOrEmpty(info.DeviceVersion))
  82. {
  83. info.DeviceVersion = DefaultDeviceVersion;
  84. }
  85. // Report usage to remote server, except for web client, since we already have data on that
  86. if (!string.Equals(info.AppName, "Dashboard", StringComparison.OrdinalIgnoreCase))
  87. {
  88. ReportNewSession(info);
  89. }
  90. return info;
  91. }
  92. public void Run()
  93. {
  94. _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(5000), _frequency);
  95. }
  96. /// <summary>
  97. /// Called when [timer fired].
  98. /// </summary>
  99. /// <param name="state">The state.</param>
  100. private async void OnTimerFired(object state)
  101. {
  102. try
  103. {
  104. await new UsageReporter(_applicationHost, _networkManager, _httpClient)
  105. .ReportServerUsage(CancellationToken.None)
  106. .ConfigureAwait(false);
  107. }
  108. catch (Exception ex)
  109. {
  110. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  111. }
  112. }
  113. public void Dispose()
  114. {
  115. _sessionManager.SessionStarted -= _sessionManager_SessionStarted;
  116. if (_timer != null)
  117. {
  118. _timer.Dispose();
  119. _timer = null;
  120. }
  121. }
  122. }
  123. }