class PuppetHerald::Models::Node
A node model
Public Class Methods
delete_empty()
click to toggle source
Deletes nodes that doesn’t have reports
@return [Integer] number of empty node deleted
# 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)
click to toggle source
Gets a paginated nodes
@param pagination [PuppetHerald::Models::Pagination] a pagination @return [Node nodes
# 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)
click to toggle source
Gets a node with prefetched reports
@param id [Integer] a in of node to get @param pagination [PuppetHerald::Models::Pagination] a pagination @return [Node, nil] fetched node or nil
# 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
Public Instance Methods
no_of_reports()
click to toggle source
Gets number of reports for node
@return [Integer] number of node’s reports
# 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)
click to toggle source
Paginete thru nodes reports
@param pagination [PuppetHerald::Models::Pagination] a pagination @return [Node] fetched node
# 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