UsageReporter.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Connect;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.Model.Logging;
  13. namespace Emby.Server.Implementations.EntryPoints
  14. {
  15. public class UsageReporter
  16. {
  17. private readonly IServerApplicationHost _applicationHost;
  18. private readonly IHttpClient _httpClient;
  19. private readonly IUserManager _userManager;
  20. private readonly ILogger _logger;
  21. private const string MbAdminUrl = "https://www.mb3admin.com/admin/";
  22. public UsageReporter(IServerApplicationHost applicationHost, IHttpClient httpClient, IUserManager userManager, ILogger logger)
  23. {
  24. _applicationHost = applicationHost;
  25. _httpClient = httpClient;
  26. _userManager = userManager;
  27. _logger = logger;
  28. }
  29. public async Task ReportServerUsage(CancellationToken cancellationToken)
  30. {
  31. cancellationToken.ThrowIfCancellationRequested();
  32. var data = new Dictionary<string, string>
  33. {
  34. { "feature", _applicationHost.Name },
  35. { "mac", _applicationHost.SystemId },
  36. { "serverid", _applicationHost.SystemId },
  37. { "deviceid", _applicationHost.SystemId },
  38. { "ver", _applicationHost.ApplicationVersion.ToString() },
  39. { "platform", _applicationHost.OperatingSystemDisplayName }
  40. };
  41. var users = _userManager.Users.ToList();
  42. data["localusers"] = users.Count(i => !i.ConnectLinkType.HasValue).ToString(CultureInfo.InvariantCulture);
  43. data["guests"] = users.Count(i => i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.Guest).ToString(CultureInfo.InvariantCulture);
  44. data["linkedusers"] = users.Count(i => i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.LinkedUser).ToString(CultureInfo.InvariantCulture);
  45. data["plugins"] = string.Join(",", _applicationHost.Plugins.Select(i => i.Id).ToArray());
  46. var logErrors = false;
  47. #if DEBUG
  48. logErrors = true;
  49. #endif
  50. var options = new HttpRequestOptions
  51. {
  52. Url = MbAdminUrl + "service/registration/ping",
  53. CancellationToken = cancellationToken,
  54. // Seeing block length errors
  55. EnableHttpCompression = false,
  56. LogRequest = false,
  57. LogErrors = logErrors,
  58. BufferContent = false
  59. };
  60. options.SetPostData(data);
  61. using (var response = await _httpClient.SendAsync(options, "POST").ConfigureAwait(false))
  62. {
  63. }
  64. }
  65. public async Task ReportAppUsage(ClientInfo app, CancellationToken cancellationToken)
  66. {
  67. if (string.IsNullOrWhiteSpace(app.DeviceId))
  68. {
  69. throw new ArgumentException("Client info must have a device Id");
  70. }
  71. _logger.Info("App Activity: app: {0}, version: {1}, deviceId: {2}, deviceName: {3}",
  72. app.AppName ?? "Unknown App",
  73. app.AppVersion ?? "Unknown",
  74. app.DeviceId,
  75. app.DeviceName ?? "Unknown");
  76. cancellationToken.ThrowIfCancellationRequested();
  77. var data = new Dictionary<string, string>
  78. {
  79. { "feature", app.AppName ?? "Unknown App" },
  80. { "serverid", _applicationHost.SystemId },
  81. { "deviceid", app.DeviceId },
  82. { "mac", app.DeviceId },
  83. { "ver", app.AppVersion ?? "Unknown" },
  84. { "platform", app.DeviceName },
  85. };
  86. var logErrors = false;
  87. #if DEBUG
  88. logErrors = true;
  89. #endif
  90. var options = new HttpRequestOptions
  91. {
  92. Url = MbAdminUrl + "service/registration/ping",
  93. CancellationToken = cancellationToken,
  94. // Seeing block length errors
  95. EnableHttpCompression = false,
  96. LogRequest = false,
  97. LogErrors = logErrors,
  98. BufferContent = false
  99. };
  100. options.SetPostData(data);
  101. using (var response = await _httpClient.SendAsync(options, "POST").ConfigureAwait(false))
  102. {
  103. }
  104. }
  105. }
  106. public class ClientInfo
  107. {
  108. public string AppName { get; set; }
  109. public string AppVersion { get; set; }
  110. public string DeviceName { get; set; }
  111. public string DeviceId { get; set; }
  112. }
  113. }