The users are unhappy. The weight tracking application worked pretty well initially, but now that they’ve been using it daily for a few months, it’s showing them too much information. They would like it to show the recent results.
That will take us into filtering…
Filtering is done using the aptly-named HealthRecordFilter class. Thus far, we have only been using this class to filter to data that has the weight type id. In this episode we will explore additional ways to perform filtering.
Common Data Filtering
There are some properties that are shared across all data types. This section discusses filtering on those items.
Filtering based on the number of items
The simplest thing we can do is to only fetch a specific number of items rather than all of them. First, we add the following checkbox before the c_tableWeight table in default.aspx:
<asp:CheckBox ID=”c_CheckboxLast5Items” runat=”server” Text=”Last 5 items only” AutoPostBack=”True” /><br />
and then we change the filter definition in default.aspx.cs to:
filter.View.Sections |= HealthRecordItemSections.Audits;
filter.MaxItemsReturned = 5;
If you run this, when you click the checkbox you will only see the last 5 weight measurements.
The HealthRecordItemCollection class implements lazy fetching of results. It will fetch chunks of instances at a time, going back to the server when it needs another chunk. The chunk size is set by the MaxFullItemsReturnedPerRequest property of the filter, and is 30 by default.
This is transparent to the calling application, and the default value is usually fine. Decreasing the value reduces the amount of work that the server has to do when processing each chunk, and might be required if your application deals objects that have significant amounts of data in them. This requires more trips to the server to fetch data, however, which will have performance implications.
Increasing the value reduces the number of trips to the server to fetch all the data, but there is a possibility that the longer operations may result in a timeout.
Filtering based on dates
We can also filter based on the
First, add a checkbox above the table that allows the user to toggle this setting. You can do that through the designer or by adding the following before the c_tableWeight table:
<asp:CheckBox ID=”c_CheckboxLast3MonthsOnly” runat=”server” Text=”Last 3 months only” AutoPostBack=”True” /><br />
and the following is added to the filter definition:
filter.UpdatedDateMin = DateTime.Now – new TimeSpan(30, 0, 0, 0);
Filtering based on application id
In a previous episode, we added some code to fetch the name of the application that last updated the data in HealthVault. We can filter on the application that last updated this data item so we only retrieve that items that our application retrieved (in general, apps should try to read all the data a user has, but it may make sense to only read this subset at times).
This is done with the following:
filter.UpdatedApplication = ApplicationConnection.ApplicationId;
Adding the checkbox and the appropriate logic is left as an exercise to the reader.
Filtering on a value that is specific to a type – the value of a weight measurement, the name of a medication is a bit more complex. The type-specific data for an instance is stored in HealthVault XML, so a filter needs to be able to find a specific piece of data inside of a larger chunk of XML and then compare it to a desired value.
This is done using a query language that is known as XPath. To write an xpath expression for a specific HealthVault data type requires that you understand that XML format that is uses to store the information, which either requires getting the XML for a specific instance (by calling GetItemXml() on the instance), or understanding the XSD schema for the data type.
To make this easier, I wrote the HealthVault XPath Explorer. This utility will let you easily walk the XML for instances that are in a specific HealthVault record, though you will have to figure out how to write the condition yourself. There are a number of good XPath tutorial available on the net.
For our application, if we want to only return items where the weight value is less than 50 kilograms, we can use the following:
filter.XPath = “/thing/data-xml/weight/value/kg[.> 50]”;
In the “[. > 50]”, the “.” refers to the current node, so it means “kg > 50”.
Automatic user sign in
You may have noticed that our demo app requires us to sign in every time we run it. This behavior is configurable:
- Go to the application configuration center
- Select the demo application
- Go to the “Misc” tab
- Toggle the “automatic user sign-in” radio button to “yes”
- Set a sign-in duration in seconds (I chose 3600).
- Click “Make changes”.
That will start the configuration change process. Within 30 minutes, the update will make it to the servers, and a new “stay signed in” checkbox will appear on the log in page for the application.
Next time we’ll modify our application to work for the whole family.