This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 418
/
ApplicationPersistenceStore.cs
141 lines (122 loc) · 4.56 KB
/
ApplicationPersistenceStore.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Extensions;
namespace Glimpse.Core.Framework
{
/// <summary>
/// An <see cref="IPersistenceStore"/> which stores Glimpse request and configuration data in application store.
/// </summary>
/// <remarks>
/// An example of an application store is <c>HttpContext.Current.Application</c> in ASP.NET.
/// </remarks>
public class ApplicationPersistenceStore : IPersistenceStore
{
private const string PersistenceStoreKey = "__GlimpsePersistenceKey";
private const int BufferSize = 25;
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationPersistenceStore" /> class.
/// </summary>
/// <param name="dataStore">The data store.</param>
public ApplicationPersistenceStore(IDataStore dataStore)
{
DataStore = dataStore;
var glimpseRequests = DataStore.Get<Queue<GlimpseRequest>>(PersistenceStoreKey);
if (glimpseRequests == null)
{
glimpseRequests = new Queue<GlimpseRequest>(BufferSize);
DataStore.Set(PersistenceStoreKey, glimpseRequests);
}
GlimpseRequests = glimpseRequests;
}
internal Queue<GlimpseRequest> GlimpseRequests { get; set; }
private IDataStore DataStore { get; set; }
private GlimpseMetadata Metadata { get; set; }
/// <summary>
/// Saves the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Save(GlimpseRequest request)
{
if (GlimpseRequests.Count >= BufferSize)
{
GlimpseRequests.Dequeue();
}
GlimpseRequests.Enqueue(request);
}
/// <summary>
/// Saves the specified system metadata.
/// </summary>
/// <param name="metadata">The metadata.</param>
public void Save(GlimpseMetadata metadata)
{
Metadata = metadata;
}
/// <summary>
/// Gets the by request id.
/// </summary>
/// <param name="requestId">The request id.</param>
/// <returns>
/// Instance of the request that matches the request id.
/// </returns>
public GlimpseRequest GetByRequestId(Guid requestId)
{
return GlimpseRequests.FirstOrDefault(r => r.RequestId == requestId);
}
/// <summary>
/// Gets the by request id and tab key.
/// </summary>
/// <param name="requestId">The request id.</param>
/// <param name="tabKey">The tab key.</param>
/// <returns>
/// Instance of the tab data that matches the request id and tab key.
/// </returns>
/// <exception cref="System.ArgumentException">Throws an exception if <paramref name="tabKey"/> is <c>null</c>.</exception>
public TabResult GetByRequestIdAndTabKey(Guid requestId, string tabKey)
{
if (string.IsNullOrEmpty(tabKey))
{
throw new ArgumentException("tabKey");
}
var request = GlimpseRequests.FirstOrDefault(r => r.RequestId == requestId);
if (request == null || !request.TabData.ContainsKey(tabKey))
{
return null;
}
return request.TabData[tabKey];
}
/// <summary>
/// Gets the by request parent id.
/// </summary>
/// <param name="parentRequestId">The parent request id.</param>
/// <returns>
/// Collection of requests that matches the parent request id.
/// </returns>
public IEnumerable<GlimpseRequest> GetByRequestParentId(Guid parentRequestId)
{
return GlimpseRequests.Where(r => r.ParentRequestId == parentRequestId).ToList();
}
/// <summary>
/// Gets the top.
/// </summary>
/// <param name="count">The count.</param>
/// <returns>
/// Collection of requests that represent the top x number of requests.
/// </returns>
public IEnumerable<GlimpseRequest> GetTop(int count)
{
return GlimpseRequests.Take(count).ToList();
}
/// <summary>
/// Gets the metadata.
/// </summary>
/// <returns>
/// Metadata that is currently applied.
/// </returns>
public GlimpseMetadata GetMetadata()
{
return Metadata;
}
}
}