-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathDatabaseStorageBase.cs
More file actions
269 lines (240 loc) · 12.4 KB
/
DatabaseStorageBase.cs
File metadata and controls
269 lines (240 loc) · 12.4 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
namespace StackExchange.Profiling.Storage
{
/// <summary>
/// Understands how to save MiniProfiler results to a MSSQL database, allowing more permanent storage and querying of slow results.
/// </summary>
public abstract class DatabaseStorageBase : IAsyncStorage, IDatabaseStorageConnectable
{
/// <summary>
/// The table <see cref="MiniProfiler"/>s are stored in.
/// </summary>
public readonly string MiniProfilersTable = "MiniProfilers";
/// <summary>
/// The table <see cref="Timing"/>s are stored in.
/// </summary>
public readonly string MiniProfilerTimingsTable = "MiniProfilerTimings";
/// <summary>
/// The table <see cref="ClientTiming"/>s are stored in.
/// </summary>
public readonly string MiniProfilerClientTimingsTable = "MiniProfilerClientTimings";
/// <summary>
/// Gets or sets how we connect to the database used to save/load MiniProfiler results.
/// </summary>
protected string ConnectionString { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseStorageBase"/> class.
/// Returns a new <c>SqlServerDatabaseStorage</c> object that will insert into the database identified by connectionString.
/// </summary>
/// <param name="connectionString">The connection String</param>
protected DatabaseStorageBase(string connectionString)
{
ConnectionString = connectionString;
}
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseStorageBase"/> class.
/// Returns a new <c>SqlServerDatabaseStorage</c> object that will insert into the database identified by connectionString.
/// </summary>
/// <param name="connectionString">The connection String</param>
/// <param name="profilersTable">The table name to use for MiniProfilers.</param>
/// <param name="timingsTable">The table name to use for MiniProfiler Timings.</param>
/// <param name="clientTimingsTable">The table name to use for MiniProfiler Client Timings.</param>
protected DatabaseStorageBase(string connectionString, string profilersTable, string timingsTable, string clientTimingsTable)
{
ConnectionString = connectionString;
MiniProfilersTable = profilersTable;
MiniProfilerTimingsTable = timingsTable;
MiniProfilerClientTimingsTable = clientTimingsTable;
}
/// <summary>
/// Returns a connection to the data store.
/// </summary>
protected abstract DbConnection GetConnection();
/// <summary>
/// Saves 'profiler' to a database under its <see cref="MiniProfiler.Id"/>.
/// </summary>
/// <param name="profiler">The <see cref="MiniProfiler"/> to save.</param>
public abstract void Save(MiniProfiler profiler);
/// <summary>
/// Asynchronously saves 'profiler' to a database under its <see cref="MiniProfiler.Id"/>.
/// </summary>
/// <param name="profiler">The <see cref="MiniProfiler"/> to save.</param>
public abstract Task SaveAsync(MiniProfiler profiler);
/// <summary>
/// Returns the MiniProfiler identified by 'id' from the database or null when no MiniProfiler exists under that 'id'.
/// </summary>
/// <param name="id">The profiler ID to load.</param>
/// <returns>The loaded <see cref="MiniProfiler"/>.</returns>
public abstract MiniProfiler Load(Guid id);
/// <summary>
/// Asynchronously returns the MiniProfiler identified by 'id' from the database or null when no MiniProfiler exists under that 'id'.
/// </summary>
/// <param name="id">The profiler ID to load.</param>
/// <returns>The loaded <see cref="MiniProfiler"/>.</returns>
public abstract Task<MiniProfiler> LoadAsync(Guid id);
/// <summary>
/// Whether this storage provider should call SetUnviewed methods (separately) after saving.
/// </summary>
public virtual bool SetUnviewedAfterSave => false;
/// <summary>
/// Sets a particular profiler session so it is considered "unviewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as unviewed for.</param>
/// <param name="id">The profiler ID to set unviewed.</param>
public abstract void SetUnviewed(string user, Guid id);
/// <summary>
/// Asynchronously sets a particular profiler session so it is considered "unviewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as unviewed for.</param>
/// <param name="id">The profiler ID to set unviewed.</param>
public abstract Task SetUnviewedAsync(string user, Guid id);
/// <summary>
/// Sets a particular profiler session to "viewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as viewed for.</param>
/// <param name="id">The profiler ID to set viewed.</param>
public abstract void SetViewed(string user, Guid id);
/// <summary>
/// Asynchronously sets a particular profiler session to "viewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as viewed for.</param>
/// <param name="id">The profiler ID to set viewed.</param>
public abstract Task SetViewedAsync(string user, Guid id);
/// <summary>
/// Asynchronously sets the provided profiler sessions to "viewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as viewed for.</param>
/// <param name="ids">The profiler IDs to set viewed.</param>
public virtual async Task SetViewedAsync(string user, IEnumerable<Guid> ids)
{
foreach (var id in ids)
{
await this.SetViewedAsync(user, id).ConfigureAwait(false);
}
}
/// <summary>
/// Returns a list of <see cref="MiniProfiler.Id"/>s that haven't been seen by <paramref name="user"/>.
/// </summary>
/// <param name="user">User identified by the current <c>MiniProfilerOptions.UserProvider</c>.</param>
/// <returns>The list of keys for the supplied user</returns>
public abstract List<Guid> GetUnviewedIds(string user);
/// <summary>
/// Asynchronously returns a list of <see cref="MiniProfiler.Id"/>s that haven't been seen by <paramref name="user"/>.
/// </summary>
/// <param name="user">User identified by the current <c>MiniProfilerOptions.UserProvider</c>.</param>
/// <returns>The list of keys for the supplied user</returns>
public abstract Task<List<Guid>> GetUnviewedIdsAsync(string user);
/// <summary>
/// Returns the MiniProfiler Ids for the given search criteria.
/// </summary>
/// <param name="maxResults">The max number of results.</param>
/// <param name="start">Search window start.</param>
/// <param name="finish">Search window end.</param>
/// <param name="orderBy">Result order.</param>
/// <returns>The list of GUID keys.</returns>
public abstract IEnumerable<Guid> List(int maxResults, DateTime? start = null, DateTime? finish = null, ListResultsOrder orderBy = ListResultsOrder.Descending);
/// <summary>
/// Asynchronously returns the MiniProfiler Ids for the given search criteria.
/// </summary>
/// <param name="maxResults">The max number of results.</param>
/// <param name="start">Search window start.</param>
/// <param name="finish">Search window end.</param>
/// <param name="orderBy">Result order.</param>
/// <returns>The list of GUID keys.</returns>
public abstract Task<IEnumerable<Guid>> ListAsync(int maxResults, DateTime? start = null, DateTime? finish = null, ListResultsOrder orderBy = ListResultsOrder.Descending);
/// <summary>
/// Connects timings from the database, shared here for use in multiple providers.
/// </summary>
/// <param name="profiler">The profiler to connect the timing tree to.</param>
/// <param name="timings">The raw list of Timings to construct the tree from.</param>
/// <param name="clientTimings">The client timings to connect to the profiler.</param>
protected void ConnectTimings(MiniProfiler profiler, List<Timing> timings, List<ClientTiming> clientTimings)
{
if (profiler?.RootTimingId.HasValue == true && timings.Count > 0)
{
var rootTiming = timings.SingleOrDefault(x => x.Id == profiler.RootTimingId.Value);
if (rootTiming != null)
{
profiler.Root = rootTiming;
foreach (var timing in timings)
{
timing.Profiler = profiler;
}
timings.Remove(rootTiming);
var timingsLookupByParent = timings.ToLookup(x => x.ParentTimingId, x => x);
PopulateChildTimings(rootTiming, timingsLookupByParent);
}
if (clientTimings.Count > 0 || profiler.ClientTimingsRedirectCount.HasValue)
{
profiler.ClientTimings = new ClientTimings
{
RedirectCount = profiler.ClientTimingsRedirectCount ?? 0,
Timings = clientTimings
};
}
}
}
/// <summary>
/// Build the subtree of <see cref="Timing"/> objects with <paramref name="parent"/> at the top.
/// Used recursively.
/// </summary>
/// <param name="parent">Parent <see cref="Timing"/> to be evaluated.</param>
/// <param name="timingsLookupByParent">Key: parent timing Id; Value: collection of all <see cref="Timing"/> objects under the given parent.</param>
private void PopulateChildTimings(Timing parent, ILookup<Guid, Timing> timingsLookupByParent)
{
if (timingsLookupByParent.Contains(parent.Id))
{
foreach (var timing in timingsLookupByParent[parent.Id].OrderBy(x => x.StartMilliseconds))
{
parent.AddChild(timing);
PopulateChildTimings(timing, timingsLookupByParent);
}
}
}
/// <summary>
/// Flattens the timings down into a single list.
/// </summary>
/// <param name="timing">The <see cref="Timing"/> to flatten into <paramref name="timingsCollection"/>.</param>
/// <param name="timingsCollection">The collection to add all timings in the <paramref name="timing"/> tree to.</param>
protected void FlattenTimings(Timing timing, List<Timing> timingsCollection)
{
timingsCollection.Add(timing);
if (timing.HasChildren)
{
foreach (var child in timing.Children)
{
FlattenTimings(child, timingsCollection);
}
}
}
private List<string> _tableCreationScripts;
/// <summary>
/// The table creation scripts for this database storage.
/// Generated by the <see cref="GetTableCreationScripts"/> implemented by the provider.
/// </summary>
public List<string> TableCreationScripts => _tableCreationScripts ??= GetTableCreationScripts().ToList();
/// <summary>
/// Creates needed tables. Run this once on your database.
/// </summary>
/// <remarks>
/// Works in SQL server and <c>sqlite</c> (with documented removals).
/// </remarks>
protected abstract IEnumerable<string> GetTableCreationScripts();
DbConnection IDatabaseStorageConnectable.GetConnection() => GetConnection();
}
/// <summary>
/// Interface for accessing the <see cref="DatabaseStorageBase"/>'s connection, for testing.
/// </summary>
public interface IDatabaseStorageConnectable
{
/// <summary>
/// Gets the connection for a <see cref="IDatabaseStorageConnectable"/> for testing.
/// </summary>
/// <returns>The connection for this storage.</returns>
DbConnection GetConnection();
}
}