UsageEntryPoint.cs 4.3 KB

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