UsageReporter.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 ILogger _logger;
  20. private const string MbAdminUrl = "https://www.mb3admin.local/admin/";
  21. public UsageReporter(IServerApplicationHost applicationHost, IHttpClient httpClient, ILogger logger)
  22. {
  23. _applicationHost = applicationHost;
  24. _httpClient = httpClient;
  25. _logger = logger;
  26. }
  27. public async Task ReportServerUsage(CancellationToken cancellationToken)
  28. {
  29. cancellationToken.ThrowIfCancellationRequested();
  30. var data = new Dictionary<string, string>
  31. {
  32. { "feature", _applicationHost.Name },
  33. { "mac", _applicationHost.SystemId },
  34. { "serverid", _applicationHost.SystemId },
  35. { "deviceid", _applicationHost.SystemId },
  36. { "ver", _applicationHost.ApplicationVersion.ToString() },
  37. { "platform", _applicationHost.OperatingSystemDisplayName }
  38. };
  39. data["plugins"] = string.Join(",", _applicationHost.Plugins.Select(i => i.Id).ToArray());
  40. var logErrors = false;
  41. #if DEBUG
  42. logErrors = true;
  43. #endif
  44. var options = new HttpRequestOptions
  45. {
  46. Url = MbAdminUrl + "service/registration/ping",
  47. CancellationToken = cancellationToken,
  48. // Seeing block length errors
  49. EnableHttpCompression = false,
  50. LogRequest = false,
  51. LogErrors = logErrors,
  52. BufferContent = false
  53. };
  54. options.SetPostData(data);
  55. using (var response = await _httpClient.SendAsync(options, "POST").ConfigureAwait(false))
  56. {
  57. }
  58. }
  59. public async Task ReportAppUsage(ClientInfo app, CancellationToken cancellationToken)
  60. {
  61. if (string.IsNullOrEmpty(app.DeviceId))
  62. {
  63. throw new ArgumentException("Client info must have a device Id");
  64. }
  65. _logger.Info("App Activity: app: {0}, version: {1}, deviceId: {2}, deviceName: {3}",
  66. app.AppName ?? "Unknown App",
  67. app.AppVersion ?? "Unknown",
  68. app.DeviceId,
  69. app.DeviceName ?? "Unknown");
  70. cancellationToken.ThrowIfCancellationRequested();
  71. var data = new Dictionary<string, string>
  72. {
  73. { "feature", app.AppName ?? "Unknown App" },
  74. { "serverid", _applicationHost.SystemId },
  75. { "deviceid", app.DeviceId },
  76. { "mac", app.DeviceId },
  77. { "ver", app.AppVersion ?? "Unknown" },
  78. { "platform", app.DeviceName },
  79. };
  80. var logErrors = false;
  81. #if DEBUG
  82. logErrors = true;
  83. #endif
  84. var options = new HttpRequestOptions
  85. {
  86. Url = MbAdminUrl + "service/registration/ping",
  87. CancellationToken = cancellationToken,
  88. // Seeing block length errors
  89. EnableHttpCompression = false,
  90. LogRequest = false,
  91. LogErrors = logErrors,
  92. BufferContent = false
  93. };
  94. options.SetPostData(data);
  95. using (var response = await _httpClient.SendAsync(options, "POST").ConfigureAwait(false))
  96. {
  97. }
  98. }
  99. }
  100. public class ClientInfo
  101. {
  102. public string AppName { get; set; }
  103. public string AppVersion { get; set; }
  104. public string DeviceName { get; set; }
  105. public string DeviceId { get; set; }
  106. }
  107. }