James Williams
LinkedInMastodonGithub

pMetrics is (addictive), partie deux

Tags: SwingX Groovy

As I mentioned in my last post, XML is one of the several formats in which pMetrics offers information from their API. My widget encloses a thin wrapper around the SwingX-WS APIs tuned for the queries that pMetrics responds to. Once retrieved, I took advantage of Groovy's awesome XML parsing capabilities.

If you saw the demo at the Tampa JUG, you may have noticed that when I selected a date range, I selected a small one. This was mostly because each date in the range is called separately to construct a visitors over time graph. To reduce this to a one-time penalty, I added a in-process instance of HSQLDB to catch the visitor counts. This could be done for the other graphs as well but I don't want pMetrics to come looking for me when someone decides to get data for a 31-day date range and hit their servers with over 100 requests. pMetrics, if you are reading this, help us out and put a day-by-day breakdown in your result sets.

Make sure to check out the chart node that is added at compile-time. I wanted to show how a non-Swing/non-Groovy component can integrate well into the builder:

panel(layout:new MigLayout()) {
  chart(type:'bar', dataset:results.visitorsOverTime)
}

and here is the factory creates it:

//Chart factory
def chartFactory = [newInstance:{builder, name, value, attrs ->
  def dataSetName = attrs.remove('dataset')
  def options, chart, type = attrs.remove('type')
  if (type == 'bar') {
    options = [false, false, false]
    chart = ChartFactory.createBarChart('', 'Days', 'Visitors', dataSetName, PlotOrientation.VERTICAL, *options)
    chart.backgroundPaint = Color.white
  }
  else {
    options = [true, true, true]
    chart = ChartFactory.createPieChart('',dataSetName, *options)
    chart.backgroundPaint = Color.black
  }
  return builder.widget(new ChartPanel(chart))
 }
] as groovy.swing.factory.Factory

Before you use it the first time, make sure to register it with the builder:

swing.registerFactory("chart", chartFactory)