You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../Activity/ActivityRepository.cs

254 lines
9.4 KiB

using MediaBrowser.Controller;
using MediaBrowser.Controller.Activity;
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
{
public class ActivityRepository : BaseSqliteRepository, IActivityRepository
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
8 years ago
public ActivityRepository(ILogManager logManager, IServerApplicationPaths appPaths, IDbConnector connector)
: base(logManager, connector)
{
8 years ago
DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db");
}
8 years ago
public async Task Initialize()
{
8 years ago
using (var connection = await CreateConnection().ConfigureAwait(false))
{
string[] queries = {
"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)"
};
8 years ago
connection.RunQueries(queries, Logger);
}
}
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");
}
8 years ago
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)";
8 years ago
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");
8 years ago
IDbTransaction transaction = null;
8 years ago
try
{
transaction = connection.BeginTransaction();
8 years ago
var index = 0;
8 years ago
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();
8 years ago
saveActivityCommand.Transaction = transaction;
8 years ago
saveActivityCommand.ExecuteNonQuery();
8 years ago
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
8 years ago
throw;
}
catch (Exception e)
{
Logger.ErrorException("Failed to save record:", e);
8 years ago
if (transaction != null)
{
transaction.Rollback();
}
8 years ago
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
}
}
}
}
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
{
8 years ago
using (var connection = CreateConnection(true).Result)
{
8 years ago
using (var cmd = connection.CreateCommand())
{
8 years ago
cmd.CommandText = BaseActivitySelectText;
8 years ago
var whereClauses = new List<string>();
8 years ago
if (minDate.HasValue)
{
whereClauses.Add("DateCreated>=@DateCreated");
cmd.Parameters.Add(cmd, "@DateCreated", DbType.Date).Value = minDate.Value;
}
var whereTextWithoutPaging = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
8 years ago
if (startIndex.HasValue && startIndex.Value > 0)
{
var pagingWhereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
8 years ago
whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM ActivityLogEntries {0} ORDER BY DateCreated DESC LIMIT {1})",
pagingWhereText,
startIndex.Value.ToString(_usCulture)));
}
8 years ago
var whereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
8 years ago
cmd.CommandText += whereText;
8 years ago
cmd.CommandText += " ORDER BY DateCreated DESC";
8 years ago
if (limit.HasValue)
{
8 years ago
cmd.CommandText += " LIMIT " + limit.Value.ToString(_usCulture);
}
8 years ago
cmd.CommandText += "; select count (Id) from ActivityLogEntries" + whereTextWithoutPaging;
var list = new List<ActivityLogEntry>();
var count = 0;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
8 years ago
while (reader.Read())
{
list.Add(GetEntry(reader));
}
if (reader.NextResult() && reader.Read())
{
count = reader.GetInt32(0);
}
}
8 years ago
return new QueryResult<ActivityLogEntry>()
{
Items = list.ToArray(),
TotalRecordCount = count
};
}
}
}
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;
}
}
}