ActivityRepository.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Activity;
  3. using MediaBrowser.Model.Activity;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Querying;
  6. using MediaBrowser.Server.Implementations.Persistence;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.Activity
  14. {
  15. public class ActivityRepository : BaseSqliteRepository, IActivityRepository
  16. {
  17. private IDbConnection _connection;
  18. private readonly IServerApplicationPaths _appPaths;
  19. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  20. private IDbCommand _saveActivityCommand;
  21. public ActivityRepository(ILogManager logManager, IServerApplicationPaths appPaths)
  22. : base(logManager)
  23. {
  24. _appPaths = appPaths;
  25. }
  26. public async Task Initialize(IDbConnector dbConnector)
  27. {
  28. var dbFile = Path.Combine(_appPaths.DataPath, "activitylog.db");
  29. _connection = await dbConnector.Connect(dbFile).ConfigureAwait(false);
  30. string[] queries = {
  31. "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)",
  32. "create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)",
  33. //pragmas
  34. "pragma temp_store = memory",
  35. "pragma shrink_memory"
  36. };
  37. _connection.RunQueries(queries, Logger);
  38. PrepareStatements();
  39. }
  40. private void PrepareStatements()
  41. {
  42. _saveActivityCommand = _connection.CreateCommand();
  43. _saveActivityCommand.CommandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)";
  44. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Id");
  45. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Name");
  46. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Overview");
  47. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@ShortOverview");
  48. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Type");
  49. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@ItemId");
  50. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@UserId");
  51. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@DateCreated");
  52. _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@LogSeverity");
  53. }
  54. private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLogEntries";
  55. public Task Create(ActivityLogEntry entry)
  56. {
  57. return Update(entry);
  58. }
  59. public async Task Update(ActivityLogEntry entry)
  60. {
  61. if (entry == null)
  62. {
  63. throw new ArgumentNullException("entry");
  64. }
  65. await WriteLock.WaitAsync().ConfigureAwait(false);
  66. IDbTransaction transaction = null;
  67. try
  68. {
  69. transaction = _connection.BeginTransaction();
  70. var index = 0;
  71. _saveActivityCommand.GetParameter(index++).Value = new Guid(entry.Id);
  72. _saveActivityCommand.GetParameter(index++).Value = entry.Name;
  73. _saveActivityCommand.GetParameter(index++).Value = entry.Overview;
  74. _saveActivityCommand.GetParameter(index++).Value = entry.ShortOverview;
  75. _saveActivityCommand.GetParameter(index++).Value = entry.Type;
  76. _saveActivityCommand.GetParameter(index++).Value = entry.ItemId;
  77. _saveActivityCommand.GetParameter(index++).Value = entry.UserId;
  78. _saveActivityCommand.GetParameter(index++).Value = entry.Date;
  79. _saveActivityCommand.GetParameter(index++).Value = entry.Severity.ToString();
  80. _saveActivityCommand.Transaction = transaction;
  81. _saveActivityCommand.ExecuteNonQuery();
  82. transaction.Commit();
  83. }
  84. catch (OperationCanceledException)
  85. {
  86. if (transaction != null)
  87. {
  88. transaction.Rollback();
  89. }
  90. throw;
  91. }
  92. catch (Exception e)
  93. {
  94. Logger.ErrorException("Failed to save record:", e);
  95. if (transaction != null)
  96. {
  97. transaction.Rollback();
  98. }
  99. throw;
  100. }
  101. finally
  102. {
  103. if (transaction != null)
  104. {
  105. transaction.Dispose();
  106. }
  107. WriteLock.Release();
  108. }
  109. }
  110. public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
  111. {
  112. using (var cmd = _connection.CreateCommand())
  113. {
  114. cmd.CommandText = BaseActivitySelectText;
  115. var whereClauses = new List<string>();
  116. if (minDate.HasValue)
  117. {
  118. whereClauses.Add("DateCreated>=@DateCreated");
  119. cmd.Parameters.Add(cmd, "@DateCreated", DbType.Date).Value = minDate.Value;
  120. }
  121. var whereTextWithoutPaging = whereClauses.Count == 0 ?
  122. string.Empty :
  123. " where " + string.Join(" AND ", whereClauses.ToArray());
  124. if (startIndex.HasValue && startIndex.Value > 0)
  125. {
  126. var pagingWhereText = whereClauses.Count == 0 ?
  127. string.Empty :
  128. " where " + string.Join(" AND ", whereClauses.ToArray());
  129. whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM ActivityLogEntries {0} ORDER BY DateCreated DESC LIMIT {1})",
  130. pagingWhereText,
  131. startIndex.Value.ToString(_usCulture)));
  132. }
  133. var whereText = whereClauses.Count == 0 ?
  134. string.Empty :
  135. " where " + string.Join(" AND ", whereClauses.ToArray());
  136. cmd.CommandText += whereText;
  137. cmd.CommandText += " ORDER BY DateCreated DESC";
  138. if (limit.HasValue)
  139. {
  140. cmd.CommandText += " LIMIT " + limit.Value.ToString(_usCulture);
  141. }
  142. cmd.CommandText += "; select count (Id) from ActivityLogEntries" + whereTextWithoutPaging;
  143. var list = new List<ActivityLogEntry>();
  144. var count = 0;
  145. using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
  146. {
  147. while (reader.Read())
  148. {
  149. list.Add(GetEntry(reader));
  150. }
  151. if (reader.NextResult() && reader.Read())
  152. {
  153. count = reader.GetInt32(0);
  154. }
  155. }
  156. return new QueryResult<ActivityLogEntry>()
  157. {
  158. Items = list.ToArray(),
  159. TotalRecordCount = count
  160. };
  161. }
  162. }
  163. private ActivityLogEntry GetEntry(IDataReader reader)
  164. {
  165. var index = 0;
  166. var info = new ActivityLogEntry
  167. {
  168. Id = reader.GetGuid(index).ToString("N")
  169. };
  170. index++;
  171. if (!reader.IsDBNull(index))
  172. {
  173. info.Name = reader.GetString(index);
  174. }
  175. index++;
  176. if (!reader.IsDBNull(index))
  177. {
  178. info.Overview = reader.GetString(index);
  179. }
  180. index++;
  181. if (!reader.IsDBNull(index))
  182. {
  183. info.ShortOverview = reader.GetString(index);
  184. }
  185. index++;
  186. if (!reader.IsDBNull(index))
  187. {
  188. info.Type = reader.GetString(index);
  189. }
  190. index++;
  191. if (!reader.IsDBNull(index))
  192. {
  193. info.ItemId = reader.GetString(index);
  194. }
  195. index++;
  196. if (!reader.IsDBNull(index))
  197. {
  198. info.UserId = reader.GetString(index);
  199. }
  200. index++;
  201. info.Date = reader.GetDateTime(index).ToUniversalTime();
  202. index++;
  203. if (!reader.IsDBNull(index))
  204. {
  205. info.Severity = (LogSeverity)Enum.Parse(typeof(LogSeverity), reader.GetString(index), true);
  206. }
  207. return info;
  208. }
  209. protected override void CloseConnection()
  210. {
  211. if (_connection != null)
  212. {
  213. if (_connection.IsOpen())
  214. {
  215. _connection.Close();
  216. }
  217. _connection.Dispose();
  218. _connection = null;
  219. }
  220. }
  221. }
  222. }