Created
July 5, 2011 23:50
-
-
Save csainty/1066239 to your computer and use it in GitHub Desktop.
Glimpse.RavenDb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Raven.Client; | |
using Raven.Client.Document; | |
using Raven.Client.MvcIntegration; | |
namespace RavenProfilerWeb | |
{ | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
public static IDocumentStore DocumentStore { get { return HttpContext.Current.Application["DocumentStore"] as IDocumentStore; } } | |
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { | |
filters.Add(new HandleErrorAttribute()); | |
} | |
public static void RegisterRoutes(RouteCollection routes) { | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute( | |
"Default", // Route name | |
"{controller}/{action}/{id}", // URL with parameters | |
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults | |
); | |
} | |
protected void Application_Start() { | |
AreaRegistration.RegisterAllAreas(); | |
RegisterGlobalFilters(GlobalFilters.Filters); | |
RegisterRoutes(RouteTable.Routes); | |
var store = new DocumentStore() { | |
Url = ConfigurationManager.AppSettings["RavenDbServer"] | |
}; | |
store.Conventions.IdentityPartsSeparator = "-"; | |
store.Initialize(); | |
Glimpse.RavenDb.Profiler.AttachTo(store); | |
Application["DocumentStore"] = store; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Glimpse.Core.Extensibility; | |
using Raven.Client.Document; | |
namespace Glimpse.RavenDb | |
{ | |
[GlimpsePlugin()] | |
public class Profiler : IGlimpsePlugin | |
{ | |
public string Name { get { return "RavenDb"; } } | |
public void SetupInit() { } | |
public object GetData(HttpContextBase context) { | |
var data = new List<object[]>(); | |
data.Add(new object[] { "Key", "Value" }); | |
data.Add(new object[] { "Stores", GetStoreList() }); | |
data.Add(new object[] { "Sessions", GetSessionList() }); | |
data.Add(new object[] { "Requests", GetRequestList() }); | |
return data; | |
} | |
private List<object[]> GetStoreList() { | |
List<object[]> data = new List<object[]>(); | |
data.Add(new object[] { "Url", "Database" }); | |
data.AddRange(stores.Keys.Select(store => new object[] { | |
store.Url, | |
store.DefaultDatabase, | |
})); | |
return data; | |
} | |
private List<object[]> GetSessionList() { | |
List<object[]> data = new List<object[]>(); | |
data.Add(new object[] { "Session Id", "Request Count", "At" }); | |
var sessions = from id in ContextualSessionList | |
from store in stores.Keys | |
let info = store.GetProfilingInformationFor(id) | |
where info != null | |
select info; | |
data.AddRange(sessions.Select(session => new object[] { | |
session.Id, | |
session.Requests.Count, | |
session.At | |
})); | |
return data; | |
} | |
private List<object[]> GetRequestList() { | |
List<object[]> data = new List<object[]>(); | |
data.Add(new object[] { "At", "HttpMetod", "Url", "Data", "HttpResult", "Status", "Result" }); | |
var requests = from id in ContextualSessionList | |
from store in stores.Keys | |
let info = store.GetProfilingInformationFor(id) | |
where info != null | |
from request in info.Requests | |
select request; | |
data.AddRange(requests.Select(req => new object[] { | |
req.At, | |
req.Method, | |
req.Url, | |
req.PostedData, | |
req.HttpResult, | |
req.Status.ToString(), | |
FormatResult(req.Result) | |
})); | |
return data; | |
} | |
private object FormatResult(string data) { | |
if (data.StartsWith("{")) { | |
// Json data | |
return "...coming soon..."; | |
} else { | |
// Server message | |
return data; | |
} | |
} | |
private static ConcurrentDictionary<DocumentStore, object> stores = new ConcurrentDictionary<DocumentStore, object>(); | |
public static void AttachTo(DocumentStore store) { | |
store.SessionCreatedInternal += TrackSession; | |
store.AfterDispose += StopTrackingStore; | |
stores.TryAdd(store, null); | |
} | |
private static void StopTrackingStore(object sender, EventArgs e) { | |
object _; | |
stores.TryRemove(sender as DocumentStore, out _); | |
} | |
private static void TrackSession(InMemoryDocumentSessionOperations session) { | |
ContextualSessionList.Add(session.Id); | |
} | |
private static List<Guid> ContextualSessionList { | |
get { | |
const string key = "Glimpse.RavenDb.SessionList"; | |
if (HttpContext.Current == null) | |
return new List<Guid>(); | |
if (!HttpContext.Current.Items.Contains(key)) | |
HttpContext.Current.Items.Add(key, new List<Guid>()); | |
return HttpContext.Current.Items[key] as List<Guid>; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment