UsageEntryPoint.cs 4.3 KB

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