Permalink
Browse files

A few more bug fixes and API changes

  • Loading branch information...
maccman committed Oct 9, 2008
1 parent 45fdbe9 commit 03b1b8b85468be42f78839d32ae690884ee5e4de
Showing with 86 additions and 19 deletions.
  1. +15 −12 application.rb
  2. +1 −1 config/init.rb
  3. +32 −0 lib/array_ext.rb
  4. +26 −0 lib/stat.rb
  5. +12 −6 public/counter.html
View
@@ -37,10 +37,13 @@ def index
args.insert(0, conds.join(' AND ')) if conds.any?
- result = IndexResult.new
- result.hits = Stat.sum(:hits, args)
- result.visits = Stat.sum(:visits, args)
-
+ if params[:sum]
+ result = SumResult.new
+ result.hits = Stat.sum(:hits, args)
+ result.visits = Stat.sum(:visits, args)
+ else
+ result = Stat.all(args)
+ end
display result
end
@@ -78,17 +81,10 @@ def commit
private
- class IndexResult
+ class SumResult
attr_accessor :visits,
:hits
- def to_json
- {
- :visits => visits,
- :hits => hits
- }.to_json
- end
-
def to_xml(options = {})
options[:indent] ||= 2
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
@@ -98,6 +94,13 @@ def to_xml(options = {})
xml.hits hits
end
end
+
+ def to_json
+ {
+ :visits => visits,
+ :hits => hits
+ }.to_json
+ end
end
def convert_time(str)
View
@@ -11,7 +11,6 @@
c[:environment] = 'production',
c[:framework] = {},
c[:log_level] = :debug,
- c[:log_file] = Merb.log_path + "/production.log"
c[:use_mutex] = false,
c[:session_store] = 'none',
c[:exception_details] = false,
@@ -24,6 +23,7 @@
require 'string_ext'
require 'time_ext'
+require 'array_ext'
require 'builder'
View
@@ -0,0 +1,32 @@
+require 'builder'
+class Array
+ def to_xml(options = {})
+ raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
+
+ options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
+ options[:children] ||= options[:root].singularize
+ options[:indent] ||= 2
+ options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
+
+ root = options.delete(:root).to_s
+ children = options.delete(:children)
+
+ if !options.has_key?(:dasherize) || options[:dasherize]
+ root = root.dasherize
+ end
+
+ options[:builder].instruct! unless options.delete(:skip_instruct)
+
+ opts = options.merge({ :root => children })
+
+ xml = options[:builder]
+ if empty?
+ xml.tag!(root, options[:skip_types] ? {} : {:type => "array"})
+ else
+ xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) {
+ yield xml if block_given?
+ each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) }
+ }
+ end
+ end
+end
View
@@ -211,6 +211,32 @@ def matched_url?(url)
:hits,
:visits,
:time_period
+
+
+ def to_xml(options = {})
+ options[:indent] ||= 2
+ xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
+ xml.instruct! unless options[:skip_instruct]
+ xml.stat do
+ xml.host host
+ xml.path path
+ xml.query query
+ xml.hits hits
+ xml.visits visits
+ xml.time_period time_period
+ end
+ end
+
+ def to_json
+ {
+ :host => host,
+ :path => path,
+ :query => query,
+ :hits => hits,
+ :visits => visits,
+ :time_period => time_period
+ }.to_json
+ end
end
END { Stat.commit! }
View
@@ -41,8 +41,10 @@
initialize: function(hit_element, visit_element) {
this.hit_element = $(hit_element);
this.visit_element = $(visit_element);
- this.r_n = 0;
- this.p_n = 0;
+ this.real_n = 0; // real number
+ this.pred_n = 0; // predicted number
+ this.refresh = 0.5; // refresh interval
+ this.increment = 0;
this.poll();
},
@@ -54,19 +56,23 @@
var date = new Date();
var time = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
var to_time = (date.getMonth() + 1) + '/' + (date.getDate() + 1) + '/' + date.getFullYear();
- new Ajax.Request('/stats.json?time=' + time + '&to_time=' + to_time, {
+ new Ajax.Request('/stats.json?sum=1&time=' + time + '&to_time=' + to_time, {
method: 'get',
onSuccess: this.pollCallback.bind(this)
});
},
pollCallback: function(t){
- this.update(t.responseJSON.hits, t.responseJSON.visits);
+ this.real_n = t.responseJSON.hits;
+ this.updateHits(t.responseJSON.hits);
+ this.updateVisits(t.responseJSON.visits);
},
- update: function(hits, visits){
- this.r_n = hits;
+ updateHits: function(hits){
this.hit_element.update(hits);
+ },
+
+ updateVisits: function(visits){
this.visit_element.update(visits);
}
});

0 comments on commit 03b1b8b

Please sign in to comment.