onrails.org home

Rails Log Analyzer - Rails and Flex with JSON

I started to write a small Rails Log Analyzer that provides some insight on how a given application is used. I’ve just spent three hours so far, so not too much to show, but I have found the integration of Flex with Rails for read-only purpose of the different time series pretty straight forward.

In two words…

RAILS: data.to_json

FLEX: JSON.decode(String(srv.lastResult));

On the Rails side

The controller simply transforms the Hash return by the model into a json textual representation.

class DataController < ApplicationController

def overview render :text => Hit.overview_data.to_json end

end

This is an extract of the method that returns a Hash that contains the time series in an Array.

def Hit.overview_data
result = {}
result[:header] = {:period => {:start => Hit.minimum(:time).to_s(:db), :end => Hit.maximum(:time).to_s(:db)}}
result[:sessions_series] =
{:by_day => Hit.data_serie(Hit.count(:session, :group => :day, :conditions => ‘controller <> “HeartbeatController”’), “sessions by day”) }
result
end

On the Flex side

import com.macromedia.serialization.json.*; private function resultHandler(event:ResultEvent) : void { status = “Loaded. Parsing data…”; var result:Object = JSON.decode(String(srv.lastResult)); header = result.header; ts = getSerie(result.sessions_series.by_day.data); }

The service is invoked by the following actionscript call

srv.send()

JSON doesn’t support Date objects out of the box, but it’s a nice way to exchange complex data such a Hash and Map between Rails and Flex.

Fork me on GitHub