UsageEntryPoint.cs 4.4 KB

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