|
public static class RavenProfiling |
|
{ |
|
private const string ContextualSessionStorageKey = "###RavenProfilerSessions###"; |
|
|
|
private static readonly |
|
ConcurrentDictionary |
|
<NancyContext, Tuple<Action<InMemoryDocumentSessionOperations>, Action<ProfilingInformation>>> |
|
Operations = |
|
new ConcurrentDictionary |
|
<NancyContext, Tuple<Action<InMemoryDocumentSessionOperations>, Action<ProfilingInformation>>>(); |
|
|
|
public static void InitializeFor(IDocumentStore documentStore, IPipelines pipelines) |
|
{ |
|
var store = documentStore as DocumentStore; |
|
if (store == null) |
|
throw new ArgumentException("Must be a Raven.Client.DocumentStore", "documentStore"); |
|
InitializeFor(store, pipelines); |
|
} |
|
|
|
private static void InitializeFor(DocumentStore documentStore, IPipelines pipelines) |
|
{ |
|
documentStore.Conventions.DisableProfiling = false; |
|
documentStore.InitializeProfiling(); |
|
|
|
pipelines.BeforeRequest.AddItemToStartOfPipeline( |
|
context => |
|
{ |
|
if (documentStore.WasDisposed) |
|
return null; |
|
|
|
Action<InMemoryDocumentSessionOperations> onSessionCreated = |
|
operation => SessionCreated(operation, context); |
|
|
|
Action<ProfilingInformation> onInformationCreated = |
|
information => InformationCreated(information, context); |
|
|
|
if (false == Operations.TryAdd(context, Tuple.Create(onSessionCreated, onInformationCreated))) |
|
return null; |
|
|
|
documentStore.SessionCreatedInternal += onSessionCreated; |
|
|
|
return null; |
|
}); |
|
|
|
pipelines.AfterRequest.AddItemToEndOfPipeline( |
|
context => |
|
{ |
|
if (documentStore.WasDisposed) |
|
return; |
|
|
|
//do something here to get the session ids into the rendered view. |
|
// I use cassette so I will add an inline script with a hook. |
|
|
|
//var bundesHelper = context.Items[BundlesHelper.BUNDLES_HELPER] as IBundlesHelper; |
|
// if (bundesHelper != null) |
|
// TrackSessionsInBundles(bundesHelper, context); |
|
|
|
Tuple<Action<InMemoryDocumentSessionOperations>, Action<ProfilingInformation>> operation; |
|
|
|
if (false == Operations.TryGetValue(context, out operation)) |
|
return; |
|
|
|
documentStore.SessionCreatedInternal -= operation.Item1; |
|
ProfilingInformation.OnContextCreated -= operation.Item2; |
|
}); |
|
} |
|
|
|
private static void InformationCreated(ProfilingInformation information, NancyContext context) |
|
{ |
|
// add additional context |
|
} |
|
|
|
private static void SessionCreated(InMemoryDocumentSessionOperations operations, NancyContext context) |
|
{ |
|
GetContextualSessionStorage(context).Add(operations.Id); |
|
} |
|
|
|
private static List<Guid> GetContextualSessionStorage(NancyContext context) |
|
{ |
|
object trackedSessionsThing; |
|
List<Guid> trackedSessions; |
|
|
|
if (context.Items.TryGetValue(ContextualSessionStorageKey, out trackedSessionsThing) |
|
&& (trackedSessions = trackedSessionsThing as List<Guid>) != null) |
|
return trackedSessions; |
|
|
|
trackedSessions = new List<Guid>(); |
|
context.Items[ContextualSessionStorageKey] = trackedSessions; |
|
|
|
return trackedSessions; |
|
} |
|
|
|
/* |
|
public static void TrackSessionsInBundles(IBundlesHelper bundles, NancyContext context) |
|
{ |
|
const string script = @" |
|
(function(document, window){{ |
|
var profiler = new window.RavenProfiler('{0}', document, window); |
|
profiler.forSession({1}); |
|
}})(document, window); |
|
"; |
|
var trackedSessions = GetContextualSessionStorage(context); |
|
if (false == trackedSessions.Any()) |
|
return; |
|
|
|
var sessionIdsInJson = "[" + String.Join( |
|
",", |
|
trackedSessions.Select(sessionId => "'" + sessionId + "'")) + "]"; |
|
|
|
bundles.AddInlineScript( |
|
String.Format(script, context.ToFullPath("~/_raven-profiler"), sessionIdsInJson), |
|
references: new[] {"~/scripts/features/raven-profiler"}); |
|
}*/ |
|
} |