Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sensor chart done.

  • Loading branch information...
commit 5d1911f75b311f76199287916e5b77c242aedab1 1 parent eead4cf
@jcxplorer authored
View
7 app/controllers/sensors_controller.rb
@@ -1,6 +1,7 @@
class SensorsController < ApplicationController
- respond_to :html
+ respond_to :html, :except => [:chart]
+ respond_to :json, :only => [:chart]
navigation :sites
@@ -43,6 +44,10 @@ def destroy
respond_with [site, sensor]
end
+ def chart
+ respond_with sensor.chart_data
+ end
+
private
def sensor
View
27 app/models/sensor.rb
@@ -1,4 +1,6 @@
class Sensor < ActiveRecord::Base
+ include TimeInTimeZone
+ include HashLookupHelper
has_many :hosts, :class_name => "SensorHost"
belongs_to :site
@@ -17,6 +19,8 @@ class Sensor < ActiveRecord::Base
self.inheritance_column = nil
+ delegate :time_zone, :to => :site
+
alias_method :hosts_attributes_with_duplicates=, :hosts_attributes=
def hosts_attributes=(attributes)
attributes.each do |key, attr|
@@ -25,6 +29,13 @@ def hosts_attributes=(attributes)
self.hosts_attributes_with_duplicates = attributes
end
+ def chart_data
+ {
+ :today => chart_entries_for_date(today),
+ :yesterday => chart_entries_for_date(today-1)
+ }
+ end
+
private
def sync_with_mongodb
@@ -56,4 +67,20 @@ def query_based?
type == "query"
end
+ def chart_entries_for_date(date)
+ month = date.mon.to_s
+ day = date.day.to_s
+
+ sensor = Mongo.db["sensor_counts"].find_one(
+ { "s" => site.bson_id, "y" => date.year, "id" => id },
+ { :fields => ["#{month}.#{day}"] }
+ )
+
+ hours = date == today ? time_now.hour + 1 : 24
+
+ hours.times.map do |hour|
+ count = hash_lookup(sensor, month, day, hour.to_s, "c") || 0
+ [hour, count]
+ end
+ end
end
View
4 app/views/sensors/show.html.haml
@@ -1,2 +1,6 @@
= header @sensor.name do
= link_to "Edit", edit_site_sensor_path(@site, @sensor)
+
+#entries
+ %h2 Entries
+ #chart{ :data => { :source => chart_site_sensor_path(@site, @sensor, :format => :json) }}
View
6 config/routes.rb
@@ -10,7 +10,11 @@
get :chart
end
- resources :sensors
+ resources :sensors do
+ member do
+ get :chart
+ end
+ end
resources :pages do
collection do
View
42 spec/models/sensor_spec.rb
@@ -8,6 +8,7 @@
it { should validate_presence_of(:type) }
let(:site) { Factory :site }
+ let(:sensor_counts_collection) { Mongo.db["sensor_counts"] }
let :campaign do
Factory :sensor,
@@ -157,4 +158,45 @@
end
end
+ describe "#chart_data" do
+ let(:sensor) { Factory :sensor, :site => site }
+
+ it "contains today's entries" do
+ Timecop.freeze(Time.utc(2011, 7, 2, 12))
+
+ sensor_counts_collection.insert({
+ "s" => site.bson_id, "y" => 2011, "id" => sensor.id,
+ "7" => { "2" => { "10" => { "c" => 20 }, "12" => { "c" => 60 } } }
+ })
+
+ expected = 16.times.map { |i| [i, 0] }
+ expected[10][1] = 20
+ expected[12][1] = 60
+
+ sensor.chart_data[:today].should == expected
+ end
+
+ it "contains yesterday's entries" do
+ Timecop.freeze(Time.utc(2011, 7, 2, 12))
+
+ sensor_counts_collection.insert({
+ "s" => site.bson_id, "y" => 2011, "id" => sensor.id,
+ "7" => { "1" => { "14" => { "c" => 15 }, "19" => { "c" => 30 } } }
+ })
+
+ expected = 24.times.map { |i| [i, 0] }
+ expected[14][1] = 15
+ expected[19][1] = 30
+
+ sensor.chart_data[:yesterday].should == expected
+ end
+ end
+
+ describe "#time_zone" do
+ it "returns the time zone of the site" do
+ sensor = Factory :sensor, :site => site
+ sensor.time_zone.should == sensor.site.time_zone
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.