2014-02-15 23:42:06 +01:00
|
|
|
|
using MediaBrowser.Common;
|
2014-05-28 16:21:07 +02:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2015-01-02 06:36:27 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2014-05-28 16:21:07 +02:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
2014-05-28 16:21:07 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
using System.Threading;
|
2016-01-29 04:40:21 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2017-01-21 21:27:07 +01:00
|
|
|
|
using MediaBrowser.Controller;
|
2016-03-03 06:17:00 +01:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2017-08-09 21:56:38 +02:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
|
2016-11-03 23:06:00 +01:00
|
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2014-02-15 23:42:06 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class UsageEntryPoint
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UsageEntryPoint : IServerEntryPoint
|
|
|
|
|
{
|
2017-01-21 21:27:07 +01:00
|
|
|
|
private readonly IServerApplicationHost _applicationHost;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-05-28 16:21:07 +02:00
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2015-01-02 06:36:27 +01:00
|
|
|
|
private readonly IUserManager _userManager;
|
2016-03-03 06:17:00 +01:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
|
2014-05-28 16:21:07 +02:00
|
|
|
|
private readonly ConcurrentDictionary<Guid, ClientInfo> _apps = new ConcurrentDictionary<Guid, ClientInfo>();
|
|
|
|
|
|
2017-01-21 21:27:07 +01:00
|
|
|
|
public UsageEntryPoint(ILogger logger, IServerApplicationHost applicationHost, IHttpClient httpClient, ISessionManager sessionManager, IUserManager userManager, IServerConfigurationManager config)
|
2014-02-15 23:42:06 +01:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_applicationHost = applicationHost;
|
|
|
|
|
_httpClient = httpClient;
|
2014-05-28 16:21:07 +02:00
|
|
|
|
_sessionManager = sessionManager;
|
2015-01-04 15:34:15 +01:00
|
|
|
|
_userManager = userManager;
|
2016-03-03 06:17:00 +01:00
|
|
|
|
_config = config;
|
2014-05-28 16:21:07 +02:00
|
|
|
|
|
|
|
|
|
_sessionManager.SessionStarted += _sessionManager_SessionStarted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _sessionManager_SessionStarted(object sender, SessionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var session = e.SessionInfo;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(session.Client) &&
|
2014-05-28 16:25:53 +02:00
|
|
|
|
!string.IsNullOrEmpty(session.DeviceName) &&
|
2014-05-28 16:30:55 +02:00
|
|
|
|
!string.IsNullOrEmpty(session.DeviceId) &&
|
|
|
|
|
!string.IsNullOrEmpty(session.ApplicationVersion))
|
2014-05-28 16:21:07 +02:00
|
|
|
|
{
|
|
|
|
|
var keys = new List<string>
|
2014-05-28 16:25:53 +02:00
|
|
|
|
{
|
|
|
|
|
session.Client,
|
|
|
|
|
session.DeviceName,
|
2014-05-28 16:30:55 +02:00
|
|
|
|
session.DeviceId,
|
|
|
|
|
session.ApplicationVersion
|
2014-05-28 16:25:53 +02:00
|
|
|
|
};
|
2014-05-28 16:21:07 +02:00
|
|
|
|
|
2017-08-09 21:56:38 +02:00
|
|
|
|
var key = string.Join("_", keys.ToArray(keys.Count)).GetMD5();
|
2014-05-28 16:21:07 +02:00
|
|
|
|
|
|
|
|
|
_apps.GetOrAdd(key, guid => GetNewClientInfo(session));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void ReportNewSession(ClientInfo client)
|
|
|
|
|
{
|
2016-03-03 06:17:00 +01:00
|
|
|
|
if (!_config.Configuration.EnableAnonymousUsageReporting)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-28 16:21:07 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-25 21:13:34 +02:00
|
|
|
|
await new UsageReporter(_applicationHost, _httpClient, _logger)
|
2014-05-28 16:21:07 +02:00
|
|
|
|
.ReportAppUsage(client, CancellationToken.None)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending anonymous usage statistics.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ClientInfo GetNewClientInfo(SessionInfo session)
|
|
|
|
|
{
|
|
|
|
|
var info = new ClientInfo
|
|
|
|
|
{
|
|
|
|
|
AppName = session.Client,
|
|
|
|
|
AppVersion = session.ApplicationVersion,
|
2014-05-28 16:30:55 +02:00
|
|
|
|
DeviceName = session.DeviceName,
|
2014-05-28 16:25:53 +02:00
|
|
|
|
DeviceId = session.DeviceId
|
2014-05-28 16:21:07 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-09-06 19:59:10 +02:00
|
|
|
|
ReportNewSession(info);
|
2014-05-28 16:21:07 +02:00
|
|
|
|
|
|
|
|
|
return info;
|
2014-02-15 23:42:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 04:40:21 +01:00
|
|
|
|
public async void Run()
|
2014-02-15 23:42:06 +01:00
|
|
|
|
{
|
2016-01-29 04:40:21 +01:00
|
|
|
|
await Task.Delay(5000).ConfigureAwait(false);
|
|
|
|
|
OnTimerFired();
|
2014-02-15 23:42:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [timer fired].
|
|
|
|
|
/// </summary>
|
2016-01-29 04:40:21 +01:00
|
|
|
|
private async void OnTimerFired()
|
2014-02-15 23:42:06 +01:00
|
|
|
|
{
|
2016-03-03 06:17:00 +01:00
|
|
|
|
if (!_config.Configuration.EnableAnonymousUsageReporting)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 23:42:06 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-25 21:13:34 +02:00
|
|
|
|
await new UsageReporter(_applicationHost, _httpClient, _logger)
|
2014-05-28 16:21:07 +02:00
|
|
|
|
.ReportServerUsage(CancellationToken.None)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-02-15 23:42:06 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending anonymous usage statistics.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-05-28 16:21:07 +02:00
|
|
|
|
_sessionManager.SessionStarted -= _sessionManager_SessionStarted;
|
2017-09-05 21:49:02 +02:00
|
|
|
|
GC.SuppressFinalize(this);
|
2014-02-15 23:42:06 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|