rdavin

Quick DB Counts at your fingertips

Discussion created by rdavin Employee on Mar 20, 2012
Latest reply on Mar 26, 2012 by MvanderVeeken

When I set up UI applications, I like to offer a progress bar as feedback to the end user.   When I'm lazy, I'll just have the bar progress from 0 to 100, jump back to 0 and start again just to keep something moving in front of the user's eyes.  But a more precise progress bar would be to know the number of steps I need to process so that the user is presented with an accurate gauge of where they are at in the process.  One practical example would be if I am crawling over every element and event frame in a database.  I want to avoid a two-pass technique where I first pass over all the items to get a count, and then a second pass to do the main work.  What I need is a quick way to get the number of elements and event frames in my database.

 

There are quick counts available in PSE in more than 1 location.  You may have seen it before.  Something like:

 

5340.QuickCounts.png

 

 It's also nice to OSIsoft has provided those same counts in a UI control named AFIdentityCountPage in the OSIsoft.AF.UI.PropertyPage namespace.  In addition to these counts being extremely fast, it was also cool to find out that you can get these counts with a single line of code!

 
// The following ONE line of code is all you need to get quick counts!
afIdentityCountPage1.AFObject = afDatabasePicker1.AFDatabase;

I love it when someone else has done most of the heavy lifting for me.  Now it's up to me to do a little more coding to extract individual counts out of the control.

 
        private int ExtractCount(string IdentityName)
        {
            // This was kind of fun to figure out.  That AFIdentityCountPage is cool and already has
            // counts inside of it.  So how do we pull specific counts out of it? 
            AFListView InnerList = (AFListView) afIdentityCountPage1.Controls[0];
            if (InnerList.Items.Count > 0)
            {
                foreach (ListViewItem item in InnerList.Items)
                {
                    if (item.Text == IdentityName)
                    {
                        // We're not worried about column 1 (item.SubItems[0])
                        // since it's the same value as item.Text.
                        // But column 2 has our counts as a text string.
                        ListViewItem.ListViewSubItem Column2 = item.SubItems[1];
                        if (!string.IsNullOrWhiteSpace(Column2.Text))
                        {
                            int Value = 0;
                            if (int.TryParse(Column2.Text, out Value))
                            {
                                return Value;
                            }
                        }
                    }
                }
            }
            return 0;
        }

 For VB.NET, that would be:

 
    Private Function ExtractCount(IdentityName As String) As Integer
        ' This was kind of fun to figure out.  That AFIdentityCountPage is cool and already has
        ' counts inside of it.  So how do we pull specific counts out of it? 
        Dim InnerList As AFListView = DirectCast(AfIdentityCountPage1.Controls(0), AFListView)
        If InnerList.Items.Count > 0 Then
            For Each item As ListViewItem In InnerList.Items
                If item.Text = IdentityName Then
                    ' We're not worried about column 1 (item.SubItems[0])
                    ' since it's the same value as item.Text.
                    ' But column 2 has our counts as a text string.
                    Dim Column2 As ListViewItem.ListViewSubItem = item.SubItems(1)
                    If Not String.IsNullOrWhiteSpace(Column2.Text) Then
                        Dim Value As Integer = 0
                        If Integer.TryParse(Column2.Text, Value) Then
                            Return Value
                        End If
                    End If
                End If
            Next
        End If
        Return 0
    End Function

 That's the main guts of the task.  Not only can I get counts in a blink of an eye, but I can extract the individual counts according to my application.  Again a very practical example would be to then set a progress bar to a known number of steps.

 

A sample C# project is included.  It lacks the progress bar but it does nicely illustrate the use of the AFIdentityCountPage and how to squeeze some functionality out of it.

Outcomes