Class: PuppetHerald::Models::Node

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/puppet-herald/models/node.rb

Overview

A node model

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_emptyInteger

Deletes nodes that doesn’t have reports

Returns:

  • (Integer)

    number of empty node deleted



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

Parameters:

Returns:

  • (Node[])

    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

Parameters:

Returns:

  • (Node, nil)

    fetched node or nil



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_reportsInteger

Gets number of reports for node

Returns:

  • (Integer)

    number of node’s reports



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

Parameters:

Returns:

  • (Node)

    fetched node



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