Permalink
Browse files

Merge branch 'monitoring'

  • Loading branch information...
2 parents c73b099 + 5d1911f commit 7de3e02347a7b81ed7cbfc433c4fc3925cec73ae @jcxplorer committed Apr 28, 2011
View
@@ -13,7 +13,7 @@ gem "snowfinch-collector",
:require => "snowfinch/collector"
group :test do
- gem "autotest"
+ gem "autotest", :require => false
gem "autotest-growl", :require => false
gem "capybara"
gem "database_cleaner"
@@ -32,6 +32,6 @@ end
group :development, :test do
gem "rspec-rails"
- gem "sqlite3-ruby", :require => "sqlite3"
+ gem "sqlite3-ruby"
gem "steak"
end
View
@@ -1,16 +1,17 @@
GIT
remote: git@github.com:jcxplorer/snowfinch-collector.git
- revision: 3bb78ca4396542818d68fce82382538bff39152f
+ revision: bf63eaa84cc874f0ff5c669e03a48728eab93c3d
specs:
snowfinch-collector (0.4.1)
+ bson_ext
mongo
rack
radix62
tzinfo
GIT
remote: git@github.com:kiskolabs/kisko-compass.git
- revision: f51501b63871981439a5e3474488975bdb6ece24
+ revision: 6e604a5ca3da9aef2fd71b735038afc17cc450ef
specs:
kisko-compass (0.1.1)
compass
@@ -67,12 +68,15 @@ GEM
chalofa_ruby-progressbar (0.0.9.1)
childprocess (0.1.8)
ffi (~> 1.0.6)
- compass (0.10.6)
- haml (>= 3.0.4)
+ chunky_png (1.1.1)
+ compass (0.11.1)
+ chunky_png (~> 1.1)
+ fssm (>= 0.2.7)
+ sass (~> 3.1)
configuration (1.2.0)
culerity (0.2.15)
- database_cleaner (0.6.6)
- devise (1.2.1)
+ database_cleaner (0.6.7)
+ devise (1.3.3)
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
@@ -87,11 +91,12 @@ GEM
rails (>= 3.0.0)
ffi (1.0.7)
rake (>= 0.8.7)
+ fssm (0.2.7)
fuubar (0.0.4)
chalofa_ruby-progressbar (~> 0.0.9)
rspec (~> 2.0)
rspec-instafail (~> 0.1.4)
- haml (3.0.25)
+ haml (3.1.1)
i18n (0.5.0)
json_pure (1.5.1)
launchy (0.4.0)
@@ -107,7 +112,7 @@ GEM
bson (>= 1.3.0)
nokogiri (1.4.4)
orm_adapter (0.0.4)
- pg (0.10.1)
+ pg (0.11.0)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.6.14)
@@ -144,7 +149,8 @@ GEM
railties (~> 3.0)
rspec (~> 2.5.0)
rubyzip (0.9.4)
- selenium-webdriver (0.1.4)
+ sass (3.1.1)
+ selenium-webdriver (0.2.0)
childprocess (>= 0.1.7)
ffi (>= 1.0.7)
json_pure
@@ -163,10 +169,10 @@ GEM
timecop (0.3.5)
treetop (1.4.9)
polyglot (>= 0.3.1)
- tzinfo (0.3.26)
+ tzinfo (0.3.27)
warden (1.0.3)
rack (>= 1.0.0)
- xpath (0.1.3)
+ xpath (0.1.4)
nokogiri (~> 1.3)
PLATFORMS
@@ -1,11 +1,61 @@
class SensorsController < ApplicationController
- respond_to :html
+ respond_to :html, :except => [:chart]
+ respond_to :json, :only => [:chart]
+
+ navigation :sites
def index
- @site = Site.find(params[:site_id])
- @sensors = @site.sensors
+ @sensors = site.sensors
respond_with @sensors
end
+
+ def show
+ respond_with sensor
+ end
+
+ def new
+ @sensor = site.sensors.new
+ respond_with @sensor
+ end
+
+ def create
+ if @sensor = site.sensors.create(params[:sensor])
+ flash.notice = %{"#{@sensor.name}" has been created.}
+ end
+ respond_with [site, @sensor]
+ end
+
+ def edit
+ respond_with sensor
+ end
+
+ def update
+ if sensor.update_attributes(params[:sensor])
+ flash.notice = %{"#{sensor.name}" has been updated.}
+ end
+ respond_with [site, sensor]
+ end
+
+ def destroy
+ if sensor.destroy
+ flash.notice = %{"#{sensor.name}" has been removed.}
+ end
+ respond_with [site, sensor]
+ end
+
+ def chart
+ respond_with sensor.chart_data
+ end
+
+ private
+
+ def sensor
+ @sensor ||= site.sensors.find(params[:id])
+ end
+
+ def site
+ @site ||= Site.find(params[:site_id])
+ end
end
@@ -1,6 +1,7 @@
class UsersController < ApplicationController
respond_to :html
+
navigation :users
def index
View
@@ -1,7 +1,86 @@
class Sensor < ActiveRecord::Base
+ include TimeInTimeZone
+ include HashLookupHelper
+ has_many :hosts, :class_name => "SensorHost"
belongs_to :site
- validates_presence_of :name, :site
+ accepts_nested_attributes_for :hosts,
+ :allow_destroy => true,
+ :reject_if => proc { |attributes| attributes["host"].strip.empty? }
+ validates_associated :hosts
+
+ validates_presence_of :name, :site, :type
+ validates_presence_of :uri_query_key, :uri_query_value, :if => :query_based?
+
+ after_save :sync_with_mongodb
+ after_destroy :sync_with_mongodb
+
+ 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|
+ attributes.delete(key) if attributes.select { |k,v| v == attr }.count > 1
+ end
+ 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
+ sensors = site.sensors.all.map do |sensor|
+ case sensor.type
+ when "query"
+ {
+ "id" => sensor.id,
+ "type" => "query",
+ "key" => sensor.uri_query_key,
+ "value" => sensor.uri_query_value
+ }
+ when "referrer"
+ {
+ "id" => sensor.id,
+ "type" => "referrer",
+ "hosts" => sensor.hosts.all.map(&:host)
+ }
+ end
+ end
+
+ Mongo.db["sites"].update(
+ { :_id => site.bson_id },
+ { :$set => { "sensors" => sensors } }
+ )
+ end
+
+ 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
@@ -0,0 +1,16 @@
+class SensorHost < ActiveRecord::Base
+
+ belongs_to :sensor
+
+ validates_presence_of :host
+ validates_uniqueness_of :host, :scope => :sensor_id
+
+ after_save :sync_with_mongodb
+
+ private
+
+ def sync_with_mongodb
+ sensor.send(:sync_with_mongodb)
+ end
+
+end
View
@@ -7,6 +7,8 @@ class Site < ActiveRecord::Base
validates_presence_of :name, :time_zone
before_create :create_mongo_site
+ after_rollback :remove_mongo_site
+ after_destroy :remove_mongo_site
def time_zone_id
ActiveSupport::TimeZone::MAPPING[time_zone]
@@ -41,6 +43,10 @@ def create_mongo_site
self.token = Mongo.db["sites"].insert({ :tz => time_zone_id }).to_s
end
+ def remove_mongo_site
+ Mongo.db["sites"].remove("_id" => bson_id)
+ end
+
def chart_pageviews_for_date(date)
month = date.mon.to_s
day = date.day.to_s
@@ -23,17 +23,21 @@ $grid_width: 940px;
$gutter: 20px;
$grid_columns: 12;
-// plain values for the griddle.it
-$_grid_width: 940;
-$_gutter: 20;
+// set "true" to show griddle.it grid
+$grid: false;
// typography
$baseline: 18px;
+// plain values for the griddle.it
+$_grid_width: 940;
+$_gutter: 20;
+$_baseline: 18;
+
// box config
-$boxborderwidth: 1px;
-$boxpadding_vertical: $baseline - $boxborderwidth; // padding is equal to lineheight minus the top & bottom borders
-$boxpadding_horizontal: 10px;
+// $boxborderwidth: 1px;
+// $boxpadding_vertical: $baseline - $boxborderwidth; // padding is equal to lineheight minus the top & bottom borders
+// $boxpadding_horizontal: 10px;
// END CONFIG
@@ -1,3 +1,3 @@
@include kisko-grid;
-@include sensible-defaults;
-@include kisko-scaffolds;
+@include sensible-defaults;
+@include kisko-scaffolds; // comment out if you don't want the scaffold look
@@ -66,12 +66,4 @@ input#page_submit {
width: 438px;
font-size: 24px;
}
-
- input[type=submit] {
- width: 100px;
- }
-
- input[type=checkbox] {
- width: auto;
- }
}
@@ -0,0 +1,6 @@
+= simple_form_for [@site, @sensor], :html => { :id => "query_sensor_form" } do |f|
+ = f.hidden_field :type, :value => "query"
+ = f.input :name, :label => "Sensor name"
+ = f.input :uri_query_key, :label => "URI query key"
+ = f.input :uri_query_value, :label => "URI query value"
+ = f.button :submit, "Save"
@@ -0,0 +1,11 @@
+= simple_form_for [@site, @sensor], :html => { :id => "referrer_sensor_form" } do |f|
+ = f.hidden_field :type, :value => "referrer"
+ = f.input :name, :label => "Sensor name"
+ #sensor_referrers
+ %h2 Referrers
+ = f.simple_fields_for :hosts do |r|
+ .referrer
+ = r.input :host, :label => "Referrer host", :required => false
+ = r.input :_destroy, :as => :boolean, :label => "remove"
+ = link_to "Add a referrer", "#", :id => "add_host_link"
+ = f.button :submit, "Save"
@@ -0,0 +1,10 @@
+= header @sensor.name
+
+- case @sensor.type
+- when "query"
+ = render "query_form"
+- when "referrer"
+ = render "referrer_form"
+
+= button_to "Remove this sensor", [@site, @sensor], :method => :delete,
+ :confirm => "Are you sure? There is no going back!"
@@ -1,4 +1,5 @@
-= header "Monitoring"
+= header "Monitoring" do
+ = link_to "Add a sensor", new_site_sensor_path(@site)
#content
- if @sensors.any?
@@ -0,0 +1,7 @@
+= header "Add a sensor" do
+ = link_to "Query based", nil, :id => "query_based_toggle", :class => "active"
+ = link_to "Referrer based", nil, :id => "referrer_based_toggle"
+
+#content
+ = render "query_form"
+ = render "referrer_form"
Oops, something went wrong.

0 comments on commit 7de3e02

Please sign in to comment.