<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>OnRails.org: Rails Log Analyzer - Rails and Flex with JSON</title>
    <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Ruby On Rails and related matters.</description>
    <item>
      <title>Rails Log Analyzer - Rails and Flex with JSON</title>
      <description>&lt;p&gt;&lt;img src="/files/20060515_railsloganalyzer.gif" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;I started to write a small Rails Log Analyzer that provides some insight on how a given application is used. I&amp;#8217;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.&lt;/p&gt;


	&lt;h2&gt;In two words&amp;#8230;&lt;/h2&gt;


	&lt;p&gt;&lt;span class="caps"&gt;RAILS&lt;/span&gt;: data.to_json&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;FLEX&lt;/span&gt;: JSON.decode(String(srv.lastResult));&lt;/p&gt;


	&lt;h2&gt;On the Rails side&lt;/h2&gt;


	&lt;p&gt;The controller simply transforms the Hash return by the model into a json textual representation.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;the controller&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;DataController&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ApplicationController&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;overview&lt;/span&gt;
    &lt;span class="ident"&gt;render&lt;/span&gt; &lt;span class="symbol"&gt;:text&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Hit&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;overview_data&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_json&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is an extract of the method that returns a Hash that contains the time series in an Array.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;the model&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;Hit.overview_data&lt;/span&gt;
    &lt;span class="ident"&gt;result&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{}&lt;/span&gt;
    &lt;span class="ident"&gt;result&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:header&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="symbol"&gt;:period&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="symbol"&gt;:start&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Hit&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;minimum&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:time&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:db&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt; &lt;span class="symbol"&gt;:end&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Hit&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;maximum&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:time&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:db&lt;/span&gt;&lt;span class="punct"&gt;)}}&lt;/span&gt;
    &lt;span class="ident"&gt;result&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:sessions_series&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt;
           &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="symbol"&gt;:by_day&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Hit&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;data_serie&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Hit&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;count&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:session&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:group&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="symbol"&gt;:day&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;controller &amp;lt;&amp;gt; &amp;quot;HeartbeatController&amp;quot;&lt;/span&gt;&lt;span class="punct"&gt;'),&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;sessions by day&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;    &lt;span class="punct"&gt;}&lt;/span&gt;
    &lt;span class="ident"&gt;result&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;   

	&lt;h2&gt;On the Flex side&lt;/h2&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;the view&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;
   import com.macromedia.serialization.json.*;

   private function resultHandler(event:ResultEvent) : void
        {
            status = &amp;quot;Loaded. Parsing data...&amp;quot;;
            var result:Object = JSON.decode(String(srv.lastResult));
            header = result.header;
            ts = getSerie(result.sessions_series.by_day.data);

  }

    &amp;lt;mx:HTTPService id=&amp;quot;srv&amp;quot; url=&amp;quot;http://10.37.129.2:3000/data/overview&amp;quot; result=&amp;quot;resultHandler(event)&amp;quot; /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;   

	&lt;p&gt;The service is invoked by the following actionscript call&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;        &lt;span class="ident"&gt;srv&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;send&lt;/span&gt;&lt;span class="punct"&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;&lt;span class="caps"&gt;JSON&lt;/span&gt; doesn&amp;#8217;t support Date objects out of the box, but it&amp;#8217;s a nice way to exchange complex data such a Hash and Map between Rails and Flex.&lt;/p&gt;</description>
      <pubDate>Mon, 15 May 2006 21:15:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c53bb291-118a-4b1f-a26f-fdfc6924bf89</guid>
      <author>Daniel Wanja</author>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json</link>
      <category>Playground</category>
      <category>JSON</category>
      <category>Flex</category>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by pilot guy</title>
      <description>&lt;p&gt;I will use this idea for parts of my flex and ruby website.&lt;/p&gt;


	&lt;p&gt;Will make it live for people, when it is live.&lt;/p&gt;


	&lt;p&gt;~r
&lt;a href="http://www.pilotoutlook.com" rel="nofollow"&gt;http://www.pilotoutlook.com&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 21 Feb 2008 01:43:43 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2656ddfd-3372-40e3-9ea2-9191df5517b8</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-3290</link>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by Chris</title>
      <description>&lt;p&gt;Thanks Luke &amp;#8211; your solution solved me some hours :-)&lt;/p&gt;</description>
      <pubDate>Wed, 18 Jul 2007 15:19:41 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bc3f09ec-a06e-45bc-b6d3-9f3f0c1f1e86</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-2454</link>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by Daniel Wanja</title>
      <description>&lt;p&gt;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&amp;#8217;t bugged by this.&lt;/p&gt;</description>
      <pubDate>Tue, 03 Jul 2007 03:57:08 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8bb9c267-754b-48f0-ae39-da2f9ea31f06</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-2353</link>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by Fluke</title>
      <description>&lt;p&gt;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:&amp;#8221;Fluke&amp;#8221;} works fine (tried it in Firebug), it violates strict specification (RFC whatever). There is even a ticket in rails Trac for this issue (&lt;a href="http://dev.rubyonrails.org/ticket/8762"&gt;http://dev.rubyonrails.org/ticket/8762&lt;/a&gt;) but solution is pretty simple&lt;/p&gt;


	&lt;p&gt;ActiveSupport::JSON.unquote_hash_key_identifiers = false&lt;/p&gt;


	&lt;p&gt;After putting this in my controller everything started working like a charm so thx anyway and I hope this helps anyone.&lt;/p&gt;</description>
      <pubDate>Mon, 02 Jul 2007 21:34:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:af55f6ca-46b5-4635-98ff-5f87f9041266</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-2351</link>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by Fluke</title>
      <description>&lt;p&gt;Hi, fine article but I have encountered some issues similar to Daniel above. Rails method to_json has output without &amp;#8221; 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).&lt;/p&gt;


	&lt;p&gt;If I fake JSON response and hardcode it in rails controller without using to_json (something like [{&amp;#8220;name&amp;#8221; : &amp;#8220;Fluke&amp;#8221; ....}]) 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&lt;/p&gt;</description>
      <pubDate>Mon, 02 Jul 2007 20:39:35 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d08dd852-81ad-41ea-8e16-a4285e926525</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-2350</link>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by Daniel Wanja</title>
      <description>&lt;p&gt;JSON describes javascript objects and {name:&amp;#8221;Daniel&amp;#8221;} is a valid javascript object with an attribute of named &amp;#8216;name&amp;#8217;. I.e. You  can declare and refere to the object and it&amp;#8217;s attributes in the following manner:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;   var person = {name:&amp;quot;Daniel&amp;quot;}
   alert(person.name)&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Note you can also think of the {} as an hasmap and use a string as key, i.e. var person = {&amp;#8216;name&amp;#8217;:&amp;#8217;Daniel&amp;#8217;};  This has the same effect. I hope this answers your question.&lt;/p&gt;</description>
      <pubDate>Fri, 04 May 2007 01:48:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:86dddfdd-507f-47db-a0d4-7f31e40362e8</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-358</link>
    </item>
    <item>
      <title>"Rails Log Analyzer - Rails and Flex with JSON" by Daniel Schmitz</title>
      <description>&lt;p&gt;Hi,&lt;/p&gt;


	&lt;p&gt;why the to_json donts use a &amp;#8221; in the variables names&amp;#8230;&lt;/p&gt;


	&lt;p&gt;ex:&lt;/p&gt;


	&lt;p&gt;{ name : &amp;#8220;Daniel&amp;#8221; }&lt;/p&gt;


	&lt;p&gt;and the corrects is { &amp;#8220;name&amp;#8221; : &amp;#8220;Daniel&amp;#8221; }&lt;/p&gt;


	&lt;p&gt;You have any ideia ?&lt;/p&gt;</description>
      <pubDate>Wed, 02 May 2007 21:30:49 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4faefe84-e222-4ba3-b270-31d06c85ed8e</guid>
      <link>http://onrails.org/articles/2006/05/15/rails-log-analyzer-rails-and-flex-with-json#comment-357</link>
    </item>
  </channel>
</rss>
