Rails Log Analyzer - Rails and Flex with JSON 7

Posted by Daniel Wanja Mon, 15 May 2006 21:15:00 GMT

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.

the controller
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.

the model
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

the view

   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);

  }

    <mx:HTTPService id="srv" url="http://10.37.129.2:3000/data/overview" result="resultHandler(event)" />

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.

Comments

Leave a response

  1. Daniel Schmitz Wed, 02 May 2007 21:30:49 GMT

    Hi,

    why the to_json donts use a ” in the variables names…

    ex:

    { name : “Daniel” }

    and the corrects is { “name” : “Daniel” }

    You have any ideia ?

  2. Daniel Wanja Fri, 04 May 2007 01:48:04 GMT

    JSON describes javascript objects and {name:”Daniel”} is a valid javascript object with an attribute of named ‘name’. I.e. You can declare and refere to the object and it’s attributes in the following manner:

       var person = {name:"Daniel"}
       alert(person.name)

    Note you can also think of the {} as an hasmap and use a string as key, i.e. var person = {‘name’:’Daniel’}; This has the same effect. I hope this answers your question.

  3. Fluke Mon, 02 Jul 2007 20:39:35 GMT

    Hi, fine article but I have encountered some issues similar to Daniel above. Rails method to_json has output without ” as Daniel S. suggested which is fine by definition, but that JSON decoder which I use doesnt seem to parse it well (it is the one from corelib).

    If I fake JSON response and hardcode it in rails controller without using to_json (something like [{“name” : “Fluke” ....}]) everything is just fine. I ve played with this for some time and I am getting kinda desperate, so any help would be appreciated since you obviously get it to work. Thx in Advance and have a nice day

  4. Fluke Mon, 02 Jul 2007 21:34:02 GMT

    Ok me again. It is really funny that after two days of tweaking and experimenting I finaly dare to ask someone for help and in next ten minutes I found the solution nearly accidentaly. It seems that though something like x = { name:”Fluke”} works fine (tried it in Firebug), it violates strict specification (RFC whatever). There is even a ticket in rails Trac for this issue (http://dev.rubyonrails.org/ticket/8762) but solution is pretty simple

    ActiveSupport::JSON.unquote_hash_key_identifiers = false

    After putting this in my controller everything started working like a charm so thx anyway and I hope this helps anyone.

  5. Daniel Wanja Tue, 03 Jul 2007 03:57:08 GMT

    Thanks for following up your question, this will be useful to the people that will encounter this issue. I am doing more using xml communication these days, so I wasn’t bugged by this.

  6. Chris Wed, 18 Jul 2007 15:19:41 GMT

    Thanks Luke – your solution solved me some hours :-)

  7. pilot guy Thu, 21 Feb 2008 01:43:43 GMT

    I will use this idea for parts of my flex and ruby website.

    Will make it live for people, when it is live.

    ~r http://www.pilotoutlook.com

Comments