Class: PuppetHerald::Models::Node
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- PuppetHerald::Models::Node
- Defined in:
- lib/puppet-herald/models/node.rb
Overview
A node model
Class Method Summary collapse
-
.delete_empty ⇒ Integer
Deletes nodes that doesn’t have reports.
-
.paginate(pagination) ⇒ Node[]
Gets a paginated nodes.
-
.with_reports(id, pagination = PuppetHerald::Models::Pagination::DEFAULT) ⇒ Node?
Gets a node with prefetched reports.
Instance Method Summary collapse
-
#no_of_reports ⇒ Integer
Gets number of reports for node.
-
#paginate_reports(pagination) ⇒ Node
Paginete thru nodes reports.
Class Method Details
.delete_empty ⇒ Integer
Deletes nodes that doesn’t have reports
38 39 40 41 42 |
# File 'lib/puppet-herald/models/node.rb', line 38 def self.delete_empty joinsql = 'LEFT JOIN "reports" ON "reports"."node_id" = "nodes"."id"' wheresql = '"reports"."node_id" IS NULL' joins(joinsql).where(wheresql).delete_all if joins(joinsql).where(wheresql).count > 0 end |
.paginate(pagination) ⇒ Node[]
Gets a paginated nodes
58 59 60 61 |
# File 'lib/puppet-herald/models/node.rb', line 58 def self.paginate(pagination) pagination.total = count order(last_run: :desc).limit(pagination.limit).offset(pagination.offset) end |
.with_reports(id, pagination = PuppetHerald::Models::Pagination::DEFAULT) ⇒ Node?
Gets a node with prefetched reports
49 50 51 52 |
# File 'lib/puppet-herald/models/node.rb', line 49 def self.with_reports(id, pagination = PuppetHerald::Models::Pagination::DEFAULT) node = find_by_id(id) node.paginate_reports pagination unless node.nil? end |
Instance Method Details
#no_of_reports ⇒ Integer
Gets number of reports for node
31 32 33 |
# File 'lib/puppet-herald/models/node.rb', line 31 def no_of_reports PuppetHerald::Models::Report.where(node_id: id).count end |
#paginate_reports(pagination) ⇒ Node
Paginete thru nodes reports
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/puppet-herald/models/node.rb', line 17 def paginate_reports(pagination) pagination.total = no_of_reports paginated = reports.order(time: :desc).limit(pagination.limit) .offset(pagination.offset) duplicate = dup duplicate.reports = paginated duplicate.id = id duplicate.readonly! duplicate end |