Tag Archives: Glimpse

PetaPoco.Glimpse – NuGet Package

A couple of weeks ago I showed how we can use the awesome Glimpse project with PetaPoco to show all the SQL’s processed in the current request. The problem was there was a few things that needed to make it into both PetaPoco and Glimpse to support this. The PetaPoco changes were in place as of v3.0.2 (thanks Brad) and a bug fix I submitted to the Glimpse team was just merged into (thanks Nik and Anthony) v0.82.

Therefore I give you PetaPoco.Glimpse. A simple NuGet package that brings it all together. Firstly create a new Asp.Net MVC 3 Project. Right click on References and select “Add Library Package Reference”. The following dialog will appear. Search for petapoco in the top right corner. It will look similar to below. Select PetaPoco.Glimpse and select Install. This will install PetaPoco.Core as well as Glimpse.

PetaPoco.Glimpse

Once it downloads the package and installs it you are almost ready to go. Here is the simplest way to get under way, however usually you would create your own DB class which inherits DatabaseWithProfiling.

// "Peta" is the name of my connection string
var db = new DatabaseWithProfiling("Peta");
var data = db.Fetch<dynamic>("select id, name from users where id = @0", 1);

Placed the above code into the HomeController and run the application. Note: You will also have to turn Glimpse On by heading to /Glimpse/Config. Once done, head back to the default url and you see an Eye icon in the bottom right corner. Clicking this will result it the following output.

PetaPoco.Glimpse1

As you can see, the PetaPoco tab shows you all the details about the SQL’s that were run in the order they were run.

There are a few caveats on the SQL’s showing up.

1. Debug must be on.

2. You can force the logging by setting –> db.ForceLogging = true;

Please let me know if you have any queries and enjoy.

Adam

Creating Your Own Glimpse Plugin

Last weekend I created a Glimpse plugin for PetaPoco. It was surprisingly easy. In this post I’ll show you how to create your own.

Please note as Glimpse is still in Beta, this example may change. but I will endeavour to update the post when and if the interface changes. This plugin is based on Glimpse 0.81.

Here is the complete code for our first plugin.

    [GlimpsePlugin(ShouldSetupInInit=true)]
    public class MyFirstGlimpsePlugin : IGlimpsePlugin
    {
        private int MaxValue;

        public object GetData(HttpApplication application)
        {
            // Return the data you want to display on your tab
            var data = new List<object[]> {new[]{ "Column1", "Column2", "Column3" }};

            for (int i = 0; i < MaxValue; i++)
            {
                // Usually get your data from application.Context.Items
                data.Add(new object[] {"Data " + i, 10*i, DateTime.Now.AddHours(i)});
            }

            return data;
        }

        public void SetupInit(HttpApplication application)
        {
            // Perform an initialisation that needs to be done.
            // Run once at startup
            MaxValue = 5;
        }

        public string Name
        {
            get { return "MyFirstPlugin"; }
        }
    }

There are three parts.

1. The Name get accessor: This is the name of the tab.

2. SetupInit method: This method will run once when the Plugin is loaded. Perform any initialisation code here.

3. GetData method: This is where the action happens. All data returned from here will be serialized into JSON and sent to the client to be rendered by the Glimpse client side framework. How it is exactly rendered depends on whether you return arrays or objects or arrays of objects etc. Initial details for this can be found on the Protocol page. Usually you would put the data you need to display in the HttpContext.Items dictionary in part of your application. You can then pull the data out using the application variable provided to you in the GetData method.

For example

-> data goes in:  HttpContext.Current.Items[“test”] = “Some data”;

-> data comes out:  var testdata = application.Context.Items[“test”]

The following is rendered by Glimpse.

Glimpse-firstPlugin

As you will see, it doesn’t matter what data you put in what column as Glimpse will do its best to render the data in the most approriate way. If I change the GetData method to be:

        public object GetData(HttpApplication application)
        {
            // Return the data you want to display on your tab
            var data = new List<object[]> {new[]{ "Column1", "Column2", "Column3" }};

            for (int i = 0; i < MaxValue; i++)
            {
                // Usually get your data from application.Context.Items
                data.Add(new object[] {"Data " + i, 10*i, DateTime.Now.AddHours(i)});
            }

            var nestedData = new Dictionary<string, object>
                                 {
                                     {"NestKey1", "NestedValue"},
                                     {"NestKey2", 2 },
                                     {"NestKey3", DateTime.Now }
                                 };

            data.Add(new object[] { "Data Nested", 60, nestedData });

            return data;
        }

the following is rendered by Glimpse.

Glimpse-firstPlugin2

As you can see the Glimpse team have made it super easy to create your own plugin. It literally takes minutes.

Enjoy

Adam

A Glimpse into PetaPoco

Last week I blogged about upcoming performance statistics that enable you to see all the Sql’s that were executed by PetaPoco in the current request.

It was also just over a week ago that I learned about Glimpse from this Mix video by Scott Hanselman. And wow what an impact. Anthony van der Hoorn (@anthony_vdh) and Nik Molnar (@nikmd23) had created a wonderful diagnostics system for Asp.net MVC. Firebug for the server if you will.

I then wondered if I could somehow plug PetaPoco diagnostics into the display for Glimpse and it was unbelievably easy. Implementing a single interface was all it took. Less than 50 lines of code in total to produce the following results. I will show you how to create your own tab in a future post. Update: Creating Your Own Glimpse Plugin

petapoco_glimpse2

This gives a pretty clear display of exactly what queries were performed during the current request.

Another powerful feature of Glimpse is that it saves the last n (default 5) requests so it is indeed possible to get access to the Sql’s performed when posting data back to the server.

It really is a credit to the guys for a wonderful project which still hasn’t reached a V1.

My aim is to have both the integration to Glimpse and html source statistics available within the week but it all depends on whether the integration points make it into the main branch before hand.

Update (01/06/2011): This is now available as a package from NuGet called PetaPoco.Glimpse.

Until then, check out Glimpse (on NuGet as well) and support it by providing valuable feedback to the team.

Adam