Permalink
Browse files

move to activerecord; still lots of refactoring/cleanup to do

  • Loading branch information...
1 parent 7e98acd commit 66069ed78f1d9fddfd3f81095b1114d9234f49a9 @erikh committed Mar 29, 2012
View
@@ -2,7 +2,7 @@ source :rubygems
gem "sinatra"
gem "haml"
-gem "sequel"
+gem "activerecord"
gem "sqlite3"
gem "highline"
gem "yajl-ruby"
View
@@ -1,20 +1,33 @@
GEM
remote: http://rubygems.org/
specs:
+ activemodel (3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ activerecord (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activesupport (3.2.2)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.0)
faker (1.0.1)
i18n (~> 0.4)
haml (3.1.2)
highline (1.6.11)
i18n (0.6.0)
mime-types (1.18)
minitest (2.11.4)
+ multi_json (1.2.0)
rack (1.4.1)
rack-protection (1.2.0)
rack
rake (0.9.2.2)
rest-client (1.6.7)
mime-types (>= 1.16)
- sequel (3.33.0)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.2)
@@ -23,19 +36,20 @@ GEM
tilt (~> 1.3, >= 1.3.3)
sqlite3 (1.3.5)
tilt (1.3.3)
+ tzinfo (0.3.32)
yajl-ruby (1.1.0)
PLATFORMS
ruby
DEPENDENCIES
+ activerecord
faker
haml
highline
minitest
rake
rest-client
- sequel
shotgun
sinatra
sqlite3
View
@@ -4,15 +4,14 @@
require 'dashboard'
require 'db'
-$db = Chef::Dashboard::DB.new("sqlite://dashboard.db")
+$db = Chef::Dashboard::DB.new({ :adapter => "sqlite3", :database => "dashboard.db" })
set :haml, :layout => :application_layout
get '/' do
- $nodes = @nodes = Node.reporting_nodes.all #FIXME remove $nodes
- p @nodes
- @success, @failure = $nodes.partition(&:last_run_success?)
- @last_node = $nodes.sort_by { |x| x.last_report.created_at }.last
+ @nodes = Node.reporting_nodes.all #FIXME remove $nodes
+ @success, @failure = @nodes.partition(&:last_run_success?)
+ @last_node = @nodes.sort_by { |x| x.last_report.created_at }.last
@groups = Node.group_by_execution
@failure_groups = @groups["failure"].sort_by { |k,v| v.count }.reverse
@@ -26,7 +26,7 @@
%a{:href => "#"} Credits
%span.divider-vertical.pull-right
%p.navbar-text.pull-right
- #{$nodes.count} reporting nodes
+ #{@nodes.count} reporting nodes
%div.well-large
%div.container-fluid.well-large
%div.row-fluid
View
@@ -36,7 +36,7 @@
%div.span9
%div.hero-unit
%h1{:align => "center"}
- Nodes - #{$nodes.count}: #{@success.count} pass, #{@failure.count} fail
+ Nodes - #{@nodes.count}: #{@success.count} pass, #{@failure.count} fail
%p{:align => "center"}
Last Reporting Node: [#{@last_node[:fqdn] rescue "none"}] - #{@last_node.last_report.created_at rescue "nil"}
:javascript
View
@@ -11,4 +11,4 @@ if HighLine.new.ask("Remove database? ")
rm_f 'dashboard.db'
end
-Chef::Dashboard::DB.new("sqlite://dashboard.db").create_schema
+Chef::Dashboard::DB.new({ :adapter => "sqlite3", :database => "dashboard.db" }).migrate
View
@@ -5,7 +5,7 @@ require 'faker'
$:.unshift 'lib'
require 'db'
-db = Chef::Dashboard::DB.new("sqlite://dashboard.db")
+db = Chef::Dashboard::DB.new({ :adapter => "sqlite3", :database => "dashboard.db" })
resources = [
"bash[hello yes]",
@@ -17,10 +17,10 @@ resources = [
domain = Faker::Internet.domain_name
100.times do
- db.transaction do
+ ActiveRecord::Base.transaction do
name = Faker::Name.first_name.downcase
ipaddress = Faker::Internet.ip_v4_address
- #100.times do
+ 100.times do
report_hash = {
"name" => name,
"fqdn" => "#{name}.int.#{domain}",
@@ -30,7 +30,7 @@ domain = Faker::Internet.domain_name
}
node = Node.create_report(report_hash)
- #node.last_report.update(:created_at => DateTime.now - Rational(rand(24).to_i, rand(48).to_i + 24))
- #end
+ node.last_report.update_attributes(:created_at => DateTime.now - Rational(rand(24).to_i, rand(48).to_i + 24))
+ end
end
end
View
@@ -1,5 +1,3 @@
-require 'db'
-
class Chef # in case we need to import the chef libraries
module Dashboard
VERSION = "0.0.1"
View
@@ -1,19 +1,30 @@
-require 'sequel'
+require 'active_record'
+require 'active_support'
require 'dashboard'
-require 'db/schema'
class Chef::Dashboard::DB
attr_reader :db
def initialize(dsn, do_require_models = true)
- Sequel.connect(dsn)
- @db = Sequel::Model.db
+ ActiveRecord::Base.establish_connection(dsn)
require_models if do_require_models
end
- def create_schema
- Schema.create(@db)
+ #--
+ # This needs to be more of a migration system and less of a "slam the latest schema into the database" system.
+ #++
+ def migrate(quiet=false)
+ migrate_files = "#{File.dirname(File.expand_path(__FILE__))}/../migrate/*.rb"
+ Dir[migrate_files].sort.each do |x|
+ require x
+ obj = File.basename(x).sub(/^\d+/, '').sub(/\.rb$/, '').camelize.constantize.new
+ if quiet
+ obj.suppress_messages { obj.up }
+ else
+ obj.up
+ end
+ end
end
def require_models
@@ -23,13 +34,4 @@ def require_models
end
end
- def transaction(&block)
- @db.transaction(&block)
- end
-
- def disconnect
- @db.disconnect
- Sequel::Model.db = nil
- end
-
end
View
@@ -1,58 +1,54 @@
require 'db'
-class Node < Sequel::Model
- one_to_many :reports
+class Node < ActiveRecord::Base
+ has_many :reports
- plugin :validation_helpers
-
- def validate
- super
- validates_presence([:name, :fqdn, :ipaddress], :allow_nil => false)
- validates_unique([:name, :fqdn])
- validates_unique(:name)
- end
+ #def validate
+ #super
+ #validates_presence([:name, :fqdn, :ipaddress], :allow_nil => false)
+ #validates_unique([:name, :fqdn])
+ #validates_unique(:name)
+ #end
def self.create_report(report_hash)
raise ArgumentError, "report_hash is not a Hash" unless report_hash.kind_of?(Hash)
raise ArgumentError, "resources is not an Array" unless report_hash['resources'].kind_of?(Array)
- node = Node.filter(:name => report_hash['name']).first
+ node = Node.where(:name => report_hash['name']).first
if node
- node.update(
- :fqdn => report_hash['fqdn'],
- :ipaddress => report_hash['ipaddress']
- )
+ node.fqdn = report_hash['fqdn']
+ node.ipaddress = report_hash['ipaddress']
else
- node = Node.create(
+ node = Node.new(
:name => report_hash['name'],
:fqdn => report_hash['fqdn'],
:ipaddress => report_hash['ipaddress']
)
end
- node.add_report(
- Report.create(
+ node.reports <<
+ Report.new(
:success => report_hash['success'],
:created_at => DateTime.now,
- :resources => report_hash['resources'].map { |x| Resource.create(:resource => x) }
+ :resources => report_hash['resources'].map { |x| Resource.new(:resource => x) }
)
- )
+ node.save
node
-
end
def last_run_success?
return last_report.success
end
def self.reporting_nodes
- p Node.select { ["distinct(nodes.id)", "nodes.*"] }.
- join_table(:inner, :reports, :node_id => :id).
- filter("reports.created_at" => (DateTime.now - Rational(1,24))..DateTime.now).
- group { "nodes.id" }
+ Node.
+ joins(:reports).
+ where("reports.created_at BETWEEN ? and ?", 1.day.ago, DateTime.now).
+ order("reports.created_at DESC").
+ select("distinct(nodes.id), nodes.*")
end
def self.reporting_nodes_old
@@ -68,9 +64,7 @@ def self.reporting_nodes_old
end
def last_report
- x = reports_dataset.order(:created_at).limit(1)
- p x
- x.all.first
+ reports.order("created_at desc").limit(1).first
end
def self.group_by_execution
View
@@ -1,26 +1,11 @@
require 'db'
-class Report < Sequel::Model
- one_to_many :resources
- many_to_one :node
-
- plugin :validation_helpers
-
- def validate
- super
- validates_presence(:success, :allow_nil => false)
- end
-
- def resources=(resources)
- save unless id
- remove_all_resources
- add_resources(resources)
- end
-
- def add_resources(resources)
- resources.each do |resource|
- add_resource(resource)
- end
- end
-
+class Report < ActiveRecord::Base
+ has_many :resources
+ has_one :node
+
+ #def validate
+ #super
+ #validates_presence(:success, :allow_nil => false)
+ #end
end
View
@@ -1,11 +1,10 @@
-class Resource < Sequel::Model(:report_resources)
- many_to_one :report
+class Resource < ActiveRecord::Base
+ self.table_name = :report_resources
+ has_one :report
- plugin :validation_helpers
-
- def validate
- super
- validates_presence(:resource, :allow_nil => false)
- validates_format(/^[^\[]+\[[^\]]+\]$/, :resource)
- end
+ #def validate
+ #super
+ #validates_presence(:resource, :allow_nil => false)
+ #validates_format(/^[^\[]+\[[^\]]+\]$/, :resource)
+ #end
end
View
@@ -1,45 +0,0 @@
-class Chef
- module Dashboard
- class DB
- module Schema
- class << self
-
- def create(db)
- create_nodes(db)
- create_reports(db)
- create_report_resources(db)
- end
-
- def create_nodes(db)
- db.create_table(:nodes) do
- primary_key :id
- String :name, :index => true
- String :fqdn, :index => true
- String :ipaddress, :index => true
- end
- end
-
- def create_reports(db)
- db.create_table(:reports) do
- primary_key :id
- Integer :node_id, :index => true
- DateTime :created_at, :index => true
- TrueClass :success
- index %w[node_id created_at]
- end
- end
-
- def create_report_resources(db)
- db.create_table(:report_resources) do
- primary_key :id
- Integer :report_id, :index => true
- String :resource, :index => true
- index %w[report_id resource]
- end
- end
-
- end
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit 66069ed

Please sign in to comment.