jellyfin-server/Emby.Server.Implementations/Activity/ActivityRepository.cs

242 lines
8.7 KiB
C#
Raw Normal View History

2016-11-18 08:39:20 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Emby.Server.Implementations.Data;
using MediaBrowser.Controller;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using SQLitePCL.pretty;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.IO;
2016-11-18 08:39:20 +00:00
namespace Emby.Server.Implementations.Activity
{
public class ActivityRepository : BaseSqliteRepository, IActivityRepository
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
protected IFileSystem FileSystem { get; private set; }
2016-11-18 08:39:20 +00:00
public ActivityRepository(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem)
2016-11-18 08:39:20 +00:00
: base(logger)
{
DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db");
FileSystem = fileSystem;
2016-11-18 08:39:20 +00:00
}
public void Initialize()
{
try
{
InitializeInternal();
}
catch (Exception ex)
{
Logger.ErrorException("Error loading database file. Will reset and retry.", ex);
FileSystem.DeleteFile(DbFilePath);
InitializeInternal();
}
}
private void InitializeInternal()
2016-11-18 08:39:20 +00:00
{
using (var connection = CreateConnection())
{
2016-11-29 19:12:37 +00:00
RunDefaultInitialization(connection);
2016-11-21 03:52:58 +00:00
2016-11-18 08:39:20 +00:00
string[] queries = {
2016-11-29 19:12:37 +00:00
"create table if not exists ActivityLogEntries (Id GUID PRIMARY KEY, Name TEXT, Overview TEXT, ShortOverview TEXT, Type TEXT, ItemId TEXT, UserId TEXT, DateCreated DATETIME, LogSeverity TEXT)",
"create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)"
2016-11-18 08:39:20 +00:00
};
connection.RunQueries(queries);
}
}
private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLogEntries";
public void Create(ActivityLogEntry entry)
2016-11-18 08:39:20 +00:00
{
Update(entry);
2016-11-18 08:39:20 +00:00
}
public void Update(ActivityLogEntry entry)
2016-11-18 08:39:20 +00:00
{
if (entry == null)
{
throw new ArgumentNullException("entry");
}
2016-12-11 05:12:00 +00:00
using (WriteLock.Write())
2016-11-18 08:39:20 +00:00
{
2016-12-11 05:12:00 +00:00
using (var connection = CreateConnection())
2016-11-18 08:39:20 +00:00
{
connection.RunInTransaction(db =>
{
2016-11-20 05:59:36 +00:00
using (var statement = db.PrepareStatement("replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)"))
{
2017-05-07 20:02:32 +00:00
statement.TryBind("@Id", entry.Id.ToGuidBlob());
2016-11-20 07:10:07 +00:00
statement.TryBind("@Name", entry.Name);
2016-11-18 08:39:20 +00:00
2016-11-20 07:10:07 +00:00
statement.TryBind("@Overview", entry.Overview);
statement.TryBind("@ShortOverview", entry.ShortOverview);
statement.TryBind("@Type", entry.Type);
statement.TryBind("@ItemId", entry.ItemId);
statement.TryBind("@UserId", entry.UserId);
statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue());
statement.TryBind("@LogSeverity", entry.Severity.ToString());
2016-11-20 05:59:36 +00:00
statement.MoveNext();
}
2016-11-28 19:26:48 +00:00
}, TransactionMode);
2016-11-18 08:39:20 +00:00
}
}
}
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
{
2016-12-11 05:12:00 +00:00
using (WriteLock.Read())
2016-11-18 08:39:20 +00:00
{
2016-12-11 05:12:00 +00:00
using (var connection = CreateConnection(true))
2016-11-18 08:39:20 +00:00
{
var commandText = BaseActivitySelectText;
var whereClauses = new List<string>();
if (minDate.HasValue)
{
2016-11-20 20:31:55 +00:00
whereClauses.Add("DateCreated>=@DateCreated");
2016-11-18 08:39:20 +00:00
}
var whereTextWithoutPaging = whereClauses.Count == 0 ?
2016-11-20 20:31:55 +00:00
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray(whereClauses.Count));
2016-11-18 08:39:20 +00:00
if (startIndex.HasValue && startIndex.Value > 0)
{
var pagingWhereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray(whereClauses.Count));
2016-11-18 08:39:20 +00:00
whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM ActivityLogEntries {0} ORDER BY DateCreated DESC LIMIT {1})",
pagingWhereText,
startIndex.Value.ToString(_usCulture)));
}
var whereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray(whereClauses.Count));
2016-11-18 08:39:20 +00:00
commandText += whereText;
commandText += " ORDER BY DateCreated DESC";
if (limit.HasValue)
{
commandText += " LIMIT " + limit.Value.ToString(_usCulture);
}
2016-12-12 05:49:19 +00:00
var statementTexts = new List<string>();
statementTexts.Add(commandText);
statementTexts.Add("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging);
2016-11-18 08:39:20 +00:00
2016-12-13 07:36:30 +00:00
return connection.RunInTransaction(db =>
2016-11-20 20:31:55 +00:00
{
2016-12-13 07:36:30 +00:00
var list = new List<ActivityLogEntry>();
var result = new QueryResult<ActivityLogEntry>();
2016-12-13 08:45:04 +00:00
var statements = PrepareAllSafe(db, statementTexts).ToList();
2016-11-20 20:31:55 +00:00
2016-12-12 05:49:19 +00:00
using (var statement = statements[0])
2016-11-20 20:31:55 +00:00
{
2016-12-12 05:49:19 +00:00
if (minDate.HasValue)
{
statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
}
foreach (var row in statement.ExecuteQuery())
{
list.Add(GetEntry(row));
}
2016-11-20 20:31:55 +00:00
}
2016-12-12 05:49:19 +00:00
using (var statement = statements[1])
2016-11-20 20:31:55 +00:00
{
2016-12-12 05:49:19 +00:00
if (minDate.HasValue)
{
statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
}
2016-11-20 20:31:55 +00:00
2016-12-12 08:53:25 +00:00
result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First();
2016-12-12 05:49:19 +00:00
}
2016-11-18 08:39:20 +00:00
result.Items = list.ToArray(list.Count);
2016-12-13 07:36:30 +00:00
return result;
}, ReadTransactionMode);
2016-11-18 08:39:20 +00:00
}
}
}
private ActivityLogEntry GetEntry(IReadOnlyList<IResultSetValue> reader)
{
var index = 0;
var info = new ActivityLogEntry
{
2017-05-07 20:02:32 +00:00
Id = reader[index].ReadGuidFromBlob().ToString("N")
2016-11-18 08:39:20 +00:00
};
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.Name = reader[index].ToString();
}
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.Overview = reader[index].ToString();
}
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.ShortOverview = reader[index].ToString();
}
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.Type = reader[index].ToString();
}
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.ItemId = reader[index].ToString();
}
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.UserId = reader[index].ToString();
}
index++;
info.Date = reader[index].ReadDateTime();
index++;
if (reader[index].SQLiteType != SQLiteType.Null)
{
info.Severity = (LogSeverity)Enum.Parse(typeof(LogSeverity), reader[index].ToString(), true);
}
return info;
}
}
}