Ruminations of J.net idle rants and ramblings of a code monkey

A couple of helpful StreamInsight Query extension methods

StreamInsight

When you are working with StreamInsight queries, there is a certain amount of information that isn’t available from the Query object, but should be … or, that is, I think should be and, since this is my blog, that’s all that really matters, right? The biggest one is the query’s current state. You can’t tell, from the query object, if it is running, stopped, stopping or anything else. Instead, you have to get the diagnostic view and then dig into that. I’m sorry … I am really loving StreamInsight but I find that just a little on the warty side. And … since you need the DiagnosticView to get this, it’s ideal to just add that one on there while we are at it. There are a bunch of things available in the diagnostic view that can be helpful but I find the query state to be the most important.

First, an enum for the various query states. This isn’t defined in the StreamInsight assemblies; you get a string from the diagnostic view.

/// <summary>
/// Enum for the query's current state./// </summary>public enum QueryState{
/// <summary>
/// The query has been suspended/// </summary>Suspended,
/// <summary>
/// The query is in the process of stopping/// </summary>Stopping,
/// <summary>
/// The query is running and processing events/// </summary>Running,
/// <summary>
/// The query has been aborted due to an error/// </summary>Aborted,
/// <summary>
/// The state cannot be determined or the query has been deleted/// </summary>Unknown
}

Next, the extension methods. I’ve used Get in the title as they aren’t really properties; instead the value needs to be gotten so I feel that it’s best and proper to have that clearly indicated in the method name. They are both quite simple and, I feel, help make working with the query object a bit smoother.

/// <summary>
/// Class with extension methods for Queries./// </summary>public static class QueryExtensionMethods{
/// <summary>
/// Returns the diagnostic view for the query./// </summary>
/// <param name="query">Query object </param>
/// <returns>The Diagnostic view for the query.</returns>public static DiagnosticView GetDiagnosticView (this Query query)
{
DiagnosticView diagnosticView =
query.Application.Server.GetDiagnosticView(query.Name);return diagnosticView; 
}
/// <summary>
/// Returns the current state of the query/// </summary>
/// <param name="query"></param>
/// <returns></returns>public static QueryState GetQueryState(this Query query)
{
QueryState queryState = QueryState.Unknown;try{
DiagnosticView diagnosticView = query.GetDiagnosticView();if (!Enum.TryParse<QueryState>(
diagnosticView[DiagnosticViewProperty.QueryState].ToString(), true, out queryState))
{
queryState = QueryState.Unknown; 
}   
}
catch(ManagementException)
{}
return queryState; 
}
}

And … a shout-out to Tony, one of the developers on my team, for coming up with this idea.