jellyfin/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs

133 lines
4.3 KiB
C#
Raw Normal View History

2014-02-15 15:42:06 -07:00
using MediaBrowser.Common;
2014-05-28 07:21:07 -07:00
using MediaBrowser.Common.Extensions;
2014-02-15 15:42:06 -07:00
using MediaBrowser.Common.Implementations.Security;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Plugins;
2014-05-28 07:21:07 -07:00
using MediaBrowser.Controller.Session;
2014-02-15 15:42:06 -07:00
using MediaBrowser.Model.Logging;
using System;
2014-05-28 07:21:07 -07:00
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-02-15 15:42:06 -07:00
using System.Threading;
namespace MediaBrowser.Server.Implementations.EntryPoints
{
/// <summary>
/// Class UsageEntryPoint
/// </summary>
public class UsageEntryPoint : IServerEntryPoint
{
private readonly IApplicationHost _applicationHost;
private readonly INetworkManager _networkManager;
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
2014-05-28 07:21:07 -07:00
private readonly ISessionManager _sessionManager;
2014-02-15 15:42:06 -07:00
private Timer _timer;
private readonly TimeSpan _frequency = TimeSpan.FromHours(24);
2014-05-28 07:21:07 -07:00
private readonly ConcurrentDictionary<Guid, ClientInfo> _apps = new ConcurrentDictionary<Guid, ClientInfo>();
public UsageEntryPoint(ILogger logger, IApplicationHost applicationHost, INetworkManager networkManager, IHttpClient httpClient, ISessionManager sessionManager)
2014-02-15 15:42:06 -07:00
{
_logger = logger;
_applicationHost = applicationHost;
_networkManager = networkManager;
_httpClient = httpClient;
2014-05-28 07:21:07 -07:00
_sessionManager = sessionManager;
_sessionManager.SessionStarted += _sessionManager_SessionStarted;
}
void _sessionManager_SessionStarted(object sender, SessionEventArgs e)
{
var session = e.SessionInfo;
if (!string.IsNullOrEmpty(session.Client) &&
2014-05-28 07:25:53 -07:00
!string.IsNullOrEmpty(session.DeviceName) &&
2014-05-28 07:30:55 -07:00
!string.IsNullOrEmpty(session.DeviceId) &&
!string.IsNullOrEmpty(session.ApplicationVersion))
2014-05-28 07:21:07 -07:00
{
var keys = new List<string>
2014-05-28 07:25:53 -07:00
{
session.Client,
session.DeviceName,
2014-05-28 07:30:55 -07:00
session.DeviceId,
session.ApplicationVersion
2014-05-28 07:25:53 -07:00
};
2014-05-28 07:21:07 -07:00
var key = string.Join("_", keys.ToArray()).GetMD5();
_apps.GetOrAdd(key, guid => GetNewClientInfo(session));
}
}
private async void ReportNewSession(ClientInfo client)
{
try
{
await new UsageReporter(_applicationHost, _networkManager, _httpClient)
.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 07:30:55 -07:00
DeviceName = session.DeviceName,
2014-05-28 07:25:53 -07:00
DeviceId = session.DeviceId
2014-05-28 07:21:07 -07:00
};
// Report usage to remote server, except for web client, since we already have data on that
if (!string.Equals(info.AppName, "Dashboard", StringComparison.OrdinalIgnoreCase))
{
ReportNewSession(info);
}
return info;
2014-02-15 15:42:06 -07:00
}
public void Run()
{
_timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(5000), _frequency);
}
/// <summary>
/// Called when [timer fired].
/// </summary>
/// <param name="state">The state.</param>
private async void OnTimerFired(object state)
{
try
{
2014-05-28 07:21:07 -07:00
await new UsageReporter(_applicationHost, _networkManager, _httpClient)
.ReportServerUsage(CancellationToken.None)
.ConfigureAwait(false);
2014-02-15 15:42:06 -07:00
}
catch (Exception ex)
{
_logger.ErrorException("Error sending anonymous usage statistics.", ex);
}
}
public void Dispose()
{
2014-05-28 07:21:07 -07:00
_sessionManager.SessionStarted -= _sessionManager_SessionStarted;
2014-02-15 15:42:06 -07:00
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
}
}