jellyfin/MediaBrowser.Server.Implementations/Activity/ActivityRepository.cs

253 lines
9.4 KiB
C#
Raw Normal View History

2014-08-10 22:13:17 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using MediaBrowser.Server.Implementations.Persistence;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Activity
{
2016-02-10 18:13:24 +00:00
public class ActivityRepository : BaseSqliteRepository, IActivityRepository
2014-08-10 22:13:17 +00:00
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2016-06-11 15:56:15 +00:00
public ActivityRepository(ILogManager logManager, IServerApplicationPaths appPaths, IDbConnector connector)
: base(logManager, connector)
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db");
2014-08-10 22:13:17 +00:00
}
2016-06-11 15:56:15 +00:00
public async Task Initialize()
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
{
string[] queries = {
2014-08-10 22:13:17 +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)",
2016-06-11 20:12:01 +00:00
"create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)"
2014-08-10 22:13:17 +00:00
};
2016-06-11 15:56:15 +00:00
connection.RunQueries(queries, Logger);
}
2014-08-10 22:13:17 +00:00
}
private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLogEntries";
public Task Create(ActivityLogEntry entry)
{
return Update(entry);
}
public async Task Update(ActivityLogEntry entry)
{
if (entry == null)
{
throw new ArgumentNullException("entry");
}
2016-06-11 15:56:15 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
{
using (var saveActivityCommand = connection.CreateCommand())
{
saveActivityCommand.CommandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)";
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
saveActivityCommand.Parameters.Add(saveActivityCommand, "@Id");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@Name");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@Overview");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@ShortOverview");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@Type");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@ItemId");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@UserId");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@DateCreated");
saveActivityCommand.Parameters.Add(saveActivityCommand, "@LogSeverity");
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
IDbTransaction transaction = null;
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
try
{
transaction = connection.BeginTransaction();
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
var index = 0;
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
saveActivityCommand.GetParameter(index++).Value = new Guid(entry.Id);
saveActivityCommand.GetParameter(index++).Value = entry.Name;
saveActivityCommand.GetParameter(index++).Value = entry.Overview;
saveActivityCommand.GetParameter(index++).Value = entry.ShortOverview;
saveActivityCommand.GetParameter(index++).Value = entry.Type;
saveActivityCommand.GetParameter(index++).Value = entry.ItemId;
saveActivityCommand.GetParameter(index++).Value = entry.UserId;
saveActivityCommand.GetParameter(index++).Value = entry.Date;
saveActivityCommand.GetParameter(index++).Value = entry.Severity.ToString();
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
saveActivityCommand.Transaction = transaction;
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
saveActivityCommand.ExecuteNonQuery();
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
throw;
}
catch (Exception e)
{
Logger.ErrorException("Failed to save record:", e);
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
if (transaction != null)
{
transaction.Rollback();
}
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
}
}
2014-08-10 22:13:17 +00:00
}
}
2014-08-14 13:24:30 +00:00
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
using (var connection = CreateConnection(true).Result)
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
using (var cmd = connection.CreateCommand())
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
cmd.CommandText = BaseActivitySelectText;
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
var whereClauses = new List<string>();
2014-08-14 13:24:30 +00:00
2016-06-11 15:56:15 +00:00
if (minDate.HasValue)
{
whereClauses.Add("DateCreated>=@DateCreated");
cmd.Parameters.Add(cmd, "@DateCreated", DbType.Date).Value = minDate.Value;
}
var whereTextWithoutPaging = whereClauses.Count == 0 ?
2014-08-14 13:24:30 +00:00
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
2016-06-11 15:56:15 +00:00
if (startIndex.HasValue && startIndex.Value > 0)
{
var pagingWhereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
2014-08-14 13:24:30 +00:00
2016-06-11 15:56:15 +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)));
}
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
var whereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
cmd.CommandText += whereText;
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
cmd.CommandText += " ORDER BY DateCreated DESC";
2014-08-10 22:13:17 +00:00
2016-06-11 15:56:15 +00:00
if (limit.HasValue)
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
cmd.CommandText += " LIMIT " + limit.Value.ToString(_usCulture);
2014-08-10 22:13:17 +00:00
}
2016-06-11 15:56:15 +00:00
cmd.CommandText += "; select count (Id) from ActivityLogEntries" + whereTextWithoutPaging;
var list = new List<ActivityLogEntry>();
var count = 0;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
2014-08-10 22:13:17 +00:00
{
2016-06-11 15:56:15 +00:00
while (reader.Read())
{
list.Add(GetEntry(reader));
}
if (reader.NextResult() && reader.Read())
{
count = reader.GetInt32(0);
}
2014-08-10 22:13:17 +00:00
}
2016-06-11 15:56:15 +00:00
return new QueryResult<ActivityLogEntry>()
{
Items = list.ToArray(),
TotalRecordCount = count
};
}
2014-08-10 22:13:17 +00:00
}
}
private ActivityLogEntry GetEntry(IDataReader reader)
{
var index = 0;
var info = new ActivityLogEntry
{
Id = reader.GetGuid(index).ToString("N")
};
index++;
if (!reader.IsDBNull(index))
{
info.Name = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(index))
{
info.Overview = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(index))
{
info.ShortOverview = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(index))
{
info.Type = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(index))
{
info.ItemId = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(index))
{
info.UserId = reader.GetString(index);
}
index++;
info.Date = reader.GetDateTime(index).ToUniversalTime();
index++;
if (!reader.IsDBNull(index))
{
info.Severity = (LogSeverity)Enum.Parse(typeof(LogSeverity), reader.GetString(index), true);
}
return info;
}
}
}