Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Sensor chart done.

  • Loading branch information...
commit 5d1911f75b311f76199287916e5b77c242aedab1 1 parent eead4cf
Joao Carlos authored
7 app/controllers/sensors_controller.rb
... ... @@ -1,6 +1,7 @@
1 1 class SensorsController < ApplicationController
2 2
3   - respond_to :html
  3 + respond_to :html, :except => [:chart]
  4 + respond_to :json, :only => [:chart]
4 5
5 6 navigation :sites
6 7
@@ -43,6 +44,10 @@ def destroy
43 44 respond_with [site, sensor]
44 45 end
45 46
  47 + def chart
  48 + respond_with sensor.chart_data
  49 + end
  50 +
46 51 private
47 52
48 53 def sensor
27 app/models/sensor.rb
... ... @@ -1,4 +1,6 @@
1 1 class Sensor < ActiveRecord::Base
  2 + include TimeInTimeZone
  3 + include HashLookupHelper
2 4
3 5 has_many :hosts, :class_name => "SensorHost"
4 6 belongs_to :site
@@ -17,6 +19,8 @@ class Sensor < ActiveRecord::Base
17 19
18 20 self.inheritance_column = nil
19 21
  22 + delegate :time_zone, :to => :site
  23 +
20 24 alias_method :hosts_attributes_with_duplicates=, :hosts_attributes=
21 25 def hosts_attributes=(attributes)
22 26 attributes.each do |key, attr|
@@ -25,6 +29,13 @@ def hosts_attributes=(attributes)
25 29 self.hosts_attributes_with_duplicates = attributes
26 30 end
27 31
  32 + def chart_data
  33 + {
  34 + :today => chart_entries_for_date(today),
  35 + :yesterday => chart_entries_for_date(today-1)
  36 + }
  37 + end
  38 +
28 39 private
29 40
30 41 def sync_with_mongodb
@@ -56,4 +67,20 @@ def query_based?
56 67 type == "query"
57 68 end
58 69
  70 + def chart_entries_for_date(date)
  71 + month = date.mon.to_s
  72 + day = date.day.to_s
  73 +
  74 + sensor = Mongo.db["sensor_counts"].find_one(
  75 + { "s" => site.bson_id, "y" => date.year, "id" => id },
  76 + { :fields => ["#{month}.#{day}"] }
  77 + )
  78 +
  79 + hours = date == today ? time_now.hour + 1 : 24
  80 +
  81 + hours.times.map do |hour|
  82 + count = hash_lookup(sensor, month, day, hour.to_s, "c") || 0
  83 + [hour, count]
  84 + end
  85 + end
59 86 end
4 app/views/sensors/show.html.haml
... ... @@ -1,2 +1,6 @@
1 1 = header @sensor.name do
2 2 = link_to "Edit", edit_site_sensor_path(@site, @sensor)
  3 +
  4 +#entries
  5 + %h2 Entries
  6 + #chart{ :data => { :source => chart_site_sensor_path(@site, @sensor, :format => :json) }}
6 config/routes.rb
@@ -10,7 +10,11 @@
10 10 get :chart
11 11 end
12 12
13   - resources :sensors
  13 + resources :sensors do
  14 + member do
  15 + get :chart
  16 + end
  17 + end
14 18
15 19 resources :pages do
16 20 collection do
42 spec/models/sensor_spec.rb
@@ -8,6 +8,7 @@
8 8 it { should validate_presence_of(:type) }
9 9
10 10 let(:site) { Factory :site }
  11 + let(:sensor_counts_collection) { Mongo.db["sensor_counts"] }
11 12
12 13 let :campaign do
13 14 Factory :sensor,
@@ -157,4 +158,45 @@
157 158 end
158 159 end
159 160
  161 + describe "#chart_data" do
  162 + let(:sensor) { Factory :sensor, :site => site }
  163 +
  164 + it "contains today's entries" do
  165 + Timecop.freeze(Time.utc(2011, 7, 2, 12))
  166 +
  167 + sensor_counts_collection.insert({
  168 + "s" => site.bson_id, "y" => 2011, "id" => sensor.id,
  169 + "7" => { "2" => { "10" => { "c" => 20 }, "12" => { "c" => 60 } } }
  170 + })
  171 +
  172 + expected = 16.times.map { |i| [i, 0] }
  173 + expected[10][1] = 20
  174 + expected[12][1] = 60
  175 +
  176 + sensor.chart_data[:today].should == expected
  177 + end
  178 +
  179 + it "contains yesterday's entries" do
  180 + Timecop.freeze(Time.utc(2011, 7, 2, 12))
  181 +
  182 + sensor_counts_collection.insert({
  183 + "s" => site.bson_id, "y" => 2011, "id" => sensor.id,
  184 + "7" => { "1" => { "14" => { "c" => 15 }, "19" => { "c" => 30 } } }
  185 + })
  186 +
  187 + expected = 24.times.map { |i| [i, 0] }
  188 + expected[14][1] = 15
  189 + expected[19][1] = 30
  190 +
  191 + sensor.chart_data[:yesterday].should == expected
  192 + end
  193 + end
  194 +
  195 + describe "#time_zone" do
  196 + it "returns the time zone of the site" do
  197 + sensor = Factory :sensor, :site => site
  198 + sensor.time_zone.should == sensor.site.time_zone
  199 + end
  200 + end
  201 +
160 202 end

0 comments on commit 5d1911f

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