aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Activity/ActivityRepository.cs
blob: c992def39d1dcec486079ac50fbce67eacffba71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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");

        public ActivityRepository(ILogManager logManager, IServerApplicationPaths appPaths, IDbConnector connector)
            : base(logManager, connector)
        {
            DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db");
        }

        public async Task Initialize()
        {
            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)"
                               };

                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");
            }

            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)";

                    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");

                    IDbTransaction transaction = null;

                    try
                    {
                        transaction = connection.BeginTransaction();

                        var index = 0;

                        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();

                        saveActivityCommand.Transaction = transaction;

                        saveActivityCommand.ExecuteNonQuery();

                        transaction.Commit();
                    }
                    catch (OperationCanceledException)
                    {
                        if (transaction != null)
                        {
                            transaction.Rollback();
                        }

                        throw;
                    }
                    catch (Exception e)
                    {
                        Logger.ErrorException("Failed to save record:", e);

                        if (transaction != null)
                        {
                            transaction.Rollback();
                        }

                        throw;
                    }
                    finally
                    {
                        if (transaction != null)
                        {
                            transaction.Dispose();
                        }
                    }
                }
            }
        }

        public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
        {
            using (var connection = CreateConnection(true).Result)
            {
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = BaseActivitySelectText;

                    var whereClauses = new List<string>();

                    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());

                    if (startIndex.HasValue && startIndex.Value > 0)
                    {
                        var pagingWhereText = whereClauses.Count == 0 ?
                            string.Empty :
                            " where " + string.Join(" AND ", whereClauses.ToArray());

                        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());

                    cmd.CommandText += whereText;

                    cmd.CommandText += " ORDER BY DateCreated DESC";

                    if (limit.HasValue)
                    {
                        cmd.CommandText += " LIMIT " + limit.Value.ToString(_usCulture);
                    }

                    cmd.CommandText += "; select count (Id) from ActivityLogEntries" + whereTextWithoutPaging;

                    var list = new List<ActivityLogEntry>();
                    var count = 0;

                    using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        while (reader.Read())
                        {
                            list.Add(GetEntry(reader));
                        }

                        if (reader.NextResult() && reader.Read())
                        {
                            count = reader.GetInt32(0);
                        }
                    }

                    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;
        }
    }
}