Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

A few more bug fixes and API changes

  • Loading branch information...
commit 03b1b8b85468be42f78839d32ae690884ee5e4de 1 parent 45fdbe9
Alex MacCaw authored October 09, 2008
27  application.rb
@@ -37,10 +37,13 @@ def index
37 37
     
38 38
     args.insert(0, conds.join(' AND ')) if conds.any?
39 39
     
40  
-    result = IndexResult.new
41  
-    result.hits   = Stat.sum(:hits,   args)
42  
-    result.visits = Stat.sum(:visits, args)
43  
-    
  40
+    if params[:sum]
  41
+      result        = SumResult.new
  42
+      result.hits   = Stat.sum(:hits,   args)
  43
+      result.visits = Stat.sum(:visits, args)
  44
+    else
  45
+      result = Stat.all(args)
  46
+    end
44 47
     display result
45 48
   end
46 49
   
@@ -78,17 +81,10 @@ def commit
78 81
   
79 82
   private
80 83
   
81  
-    class IndexResult
  84
+    class SumResult
82 85
       attr_accessor :visits,
83 86
                     :hits
84 87
       
85  
-      def to_json
86  
-        {
87  
-          :visits => visits,
88  
-          :hits   => hits
89  
-        }.to_json
90  
-      end
91  
-      
92 88
       def to_xml(options = {})
93 89
         options[:indent] ||= 2
94 90
         xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
@@ -98,6 +94,13 @@ def to_xml(options = {})
98 94
           xml.hits    hits
99 95
         end
100 96
       end
  97
+      
  98
+      def to_json
  99
+        {
  100
+          :visits => visits,
  101
+          :hits   => hits
  102
+        }.to_json
  103
+      end
101 104
     end
102 105
   
103 106
     def convert_time(str)
2  config/init.rb
@@ -11,7 +11,6 @@
11 11
   c[:environment]         = 'production',
12 12
   c[:framework]           = {},
13 13
   c[:log_level]           = :debug,
14  
-  c[:log_file]            = Merb.log_path + "/production.log"
15 14
   c[:use_mutex]           = false,
16 15
   c[:session_store]       = 'none',
17 16
   c[:exception_details]   = false,
@@ -24,6 +23,7 @@
24 23
 
25 24
 require 'string_ext'
26 25
 require 'time_ext'
  26
+require 'array_ext'
27 27
 
28 28
 require 'builder'
29 29
 
32  lib/array_ext.rb
... ...
@@ -0,0 +1,32 @@
  1
+require 'builder'
  2
+class Array
  3
+  def to_xml(options = {})
  4
+    raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
  5
+
  6
+    options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
  7
+    options[:children] ||= options[:root].singularize
  8
+    options[:indent]   ||= 2
  9
+    options[:builder]  ||= Builder::XmlMarkup.new(:indent => options[:indent])
  10
+
  11
+    root     = options.delete(:root).to_s
  12
+    children = options.delete(:children)
  13
+
  14
+    if !options.has_key?(:dasherize) || options[:dasherize]
  15
+      root = root.dasherize
  16
+    end
  17
+
  18
+    options[:builder].instruct! unless options.delete(:skip_instruct)
  19
+
  20
+    opts = options.merge({ :root => children })
  21
+
  22
+    xml = options[:builder]
  23
+    if empty?
  24
+      xml.tag!(root, options[:skip_types] ? {} : {:type => "array"})
  25
+    else
  26
+      xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) {
  27
+        yield xml if block_given?
  28
+        each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) }
  29
+      }
  30
+    end
  31
+  end
  32
+end
26  lib/stat.rb
@@ -211,6 +211,32 @@ def matched_url?(url)
211 211
                 :hits,
212 212
                 :visits,
213 213
                 :time_period
  214
+                
  215
+                
  216
+  def to_xml(options = {})
  217
+    options[:indent] ||= 2
  218
+    xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  219
+    xml.instruct! unless options[:skip_instruct]
  220
+    xml.stat do
  221
+      xml.host          host
  222
+      xml.path          path
  223
+      xml.query         query
  224
+      xml.hits          hits
  225
+      xml.visits        visits
  226
+      xml.time_period   time_period
  227
+    end
  228
+  end
  229
+  
  230
+  def to_json
  231
+    {
  232
+      :host         => host,
  233
+      :path         => path,
  234
+      :query        => query,
  235
+      :hits         => hits,
  236
+      :visits       => visits,
  237
+      :time_period  => time_period
  238
+    }.to_json
  239
+  end
214 240
 end
215 241
 
216 242
 END { Stat.commit! }
18  public/counter.html
@@ -41,8 +41,10 @@
41 41
         initialize: function(hit_element, visit_element) {
42 42
           this.hit_element    = $(hit_element);
43 43
           this.visit_element  = $(visit_element);
44  
-          this.r_n = 0;
45  
-          this.p_n = 0;
  44
+          this.real_n     = 0; // real number
  45
+          this.pred_n     = 0; // predicted number
  46
+          this.refresh    = 0.5; // refresh interval
  47
+          this.increment  = 0;
46 48
           this.poll();
47 49
         },
48 50
         
@@ -54,19 +56,23 @@
54 56
           var date = new Date();
55 57
           var time = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
56 58
           var to_time = (date.getMonth() + 1) + '/' + (date.getDate() + 1) + '/' + date.getFullYear();
57  
-          new Ajax.Request('/stats.json?time=' + time + '&to_time=' + to_time, {
  59
+          new Ajax.Request('/stats.json?sum=1&time=' + time + '&to_time=' + to_time, {
58 60
             method: 'get',
59 61
             onSuccess: this.pollCallback.bind(this)
60 62
           });
61 63
         },
62 64
         
63 65
         pollCallback: function(t){
64  
-          this.update(t.responseJSON.hits, t.responseJSON.visits);
  66
+          this.real_n = t.responseJSON.hits;
  67
+          this.updateHits(t.responseJSON.hits);
  68
+          this.updateVisits(t.responseJSON.visits);
65 69
         },
66 70
         
67  
-        update: function(hits, visits){
68  
-          this.r_n = hits;
  71
+        updateHits: function(hits){
69 72
           this.hit_element.update(hits);
  73
+        },
  74
+        
  75
+        updateVisits: function(visits){
70 76
           this.visit_element.update(visits);
71 77
         }
72 78
       });

0 notes on commit 03b1b8b

Please sign in to comment.
Something went wrong with that request. Please try again.