2014-09-06 13:09:35 -07:00
|
|
|
|
using MediaBrowser.Common;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2015-01-01 22:36:27 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Connect;
|
2014-02-15 15:42:06 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-01-01 22:36:27 -07:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
2014-02-15 15:42:06 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-12-30 00:11:58 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-02-15 15:42:06 -07:00
|
|
|
|
|
2014-09-06 13:09:35 -07:00
|
|
|
|
namespace MediaBrowser.Server.Implementations.EntryPoints
|
2014-02-15 15:42:06 -07:00
|
|
|
|
{
|
|
|
|
|
public class UsageReporter
|
|
|
|
|
{
|
|
|
|
|
private readonly IApplicationHost _applicationHost;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2015-01-01 22:36:27 -07:00
|
|
|
|
private readonly IUserManager _userManager;
|
2015-12-30 00:11:58 -07:00
|
|
|
|
private readonly ILogger _logger;
|
2016-02-09 10:13:50 -07:00
|
|
|
|
private const string MbAdminUrl = "https://www.mb3admin.com/admin/";
|
2014-02-15 15:42:06 -07:00
|
|
|
|
|
2015-12-30 00:11:58 -07:00
|
|
|
|
public UsageReporter(IApplicationHost applicationHost, IHttpClient httpClient, IUserManager userManager, ILogger logger)
|
2014-02-15 15:42:06 -07:00
|
|
|
|
{
|
|
|
|
|
_applicationHost = applicationHost;
|
|
|
|
|
_httpClient = httpClient;
|
2015-01-01 22:36:27 -07:00
|
|
|
|
_userManager = userManager;
|
2015-12-30 00:11:58 -07:00
|
|
|
|
_logger = logger;
|
2014-02-15 15:42:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 11:21:35 -07:00
|
|
|
|
public async Task ReportServerUsage(CancellationToken cancellationToken)
|
2014-02-15 15:42:06 -07:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var data = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "feature", _applicationHost.Name },
|
2015-01-10 12:42:14 -07:00
|
|
|
|
{ "mac", _applicationHost.SystemId },
|
2014-09-06 13:07:24 -07:00
|
|
|
|
{ "serverid", _applicationHost.SystemId },
|
|
|
|
|
{ "deviceid", _applicationHost.SystemId },
|
2014-02-15 15:42:06 -07:00
|
|
|
|
{ "ver", _applicationHost.ApplicationVersion.ToString() },
|
2014-11-23 16:10:41 -07:00
|
|
|
|
{ "platform", _applicationHost.OperatingSystemDisplayName },
|
2014-08-24 20:54:45 -07:00
|
|
|
|
{ "isservice", _applicationHost.IsRunningAsService.ToString().ToLower()}
|
2014-02-15 15:42:06 -07:00
|
|
|
|
};
|
|
|
|
|
|
2015-01-01 22:36:27 -07:00
|
|
|
|
var users = _userManager.Users.ToList();
|
|
|
|
|
|
|
|
|
|
data["localusers"] = users.Count(i => !i.ConnectLinkType.HasValue).ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
data["guests"] = users.Count(i => i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.Guest).ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
data["linkedusers"] = users.Count(i => i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.LinkedUser).ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
2015-07-22 22:25:55 -07:00
|
|
|
|
data["plugins"] = string.Join(",", _applicationHost.Plugins.Select(i => i.Id).ToArray());
|
2015-12-30 00:11:58 -07:00
|
|
|
|
|
2016-01-23 11:21:35 -07:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = MbAdminUrl + "service/registration/ping",
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
|
|
|
|
|
// Seeing block length errors
|
|
|
|
|
EnableHttpCompression = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.SetPostData(data);
|
|
|
|
|
|
|
|
|
|
using (var response = await _httpClient.SendAsync(options, "POST").ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2014-02-15 15:42:06 -07:00
|
|
|
|
}
|
2014-05-28 07:21:07 -07:00
|
|
|
|
|
2016-01-23 11:21:35 -07:00
|
|
|
|
public async Task ReportAppUsage(ClientInfo app, CancellationToken cancellationToken)
|
2014-05-28 07:21:07 -07:00
|
|
|
|
{
|
2014-09-06 10:46:09 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(app.DeviceId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Client info must have a device Id");
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 00:11:58 -07:00
|
|
|
|
_logger.Info("App Activity: app: {0}, version: {1}, deviceId: {2}, deviceName: {3}",
|
|
|
|
|
app.AppName ?? "Unknown App",
|
|
|
|
|
app.AppVersion ?? "Unknown",
|
|
|
|
|
app.DeviceId,
|
|
|
|
|
app.DeviceName ?? "Unknown");
|
|
|
|
|
|
2014-05-28 09:30:38 -07:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var data = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "feature", app.AppName ?? "Unknown App" },
|
2014-09-06 13:07:24 -07:00
|
|
|
|
{ "serverid", _applicationHost.SystemId },
|
|
|
|
|
{ "deviceid", app.DeviceId },
|
2014-09-06 10:46:09 -07:00
|
|
|
|
{ "mac", app.DeviceId },
|
2014-05-28 09:30:38 -07:00
|
|
|
|
{ "ver", app.AppVersion ?? "Unknown" },
|
|
|
|
|
{ "platform", app.DeviceName },
|
|
|
|
|
};
|
2014-05-28 07:21:07 -07:00
|
|
|
|
|
2016-01-23 11:21:35 -07:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = MbAdminUrl + "service/registration/ping",
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
|
|
|
|
|
// Seeing block length errors
|
|
|
|
|
EnableHttpCompression = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.SetPostData(data);
|
|
|
|
|
|
|
|
|
|
using (var response = await _httpClient.SendAsync(options, "POST").ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2014-05-28 07:21:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ClientInfo
|
|
|
|
|
{
|
|
|
|
|
public string AppName { get; set; }
|
|
|
|
|
public string AppVersion { get; set; }
|
2014-05-28 07:30:55 -07:00
|
|
|
|
public string DeviceName { get; set; }
|
2014-05-28 07:25:53 -07:00
|
|
|
|
public string DeviceId { get; set; }
|
2014-02-15 15:42:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|