Class: Kriterion::Backend::MongoDB

Inherits:
Kriterion::Backend show all
Defined in:
lib/kriterion/backend/mongodb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Kriterion::Backend

get, set

Constructor Details

#initialize(opts) ⇒ MongoDB

Returns a new instance of MongoDB.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kriterion/backend/mongodb.rb', line 28

def initialize(opts)
  logger.info 'Initializing MongoDB backend'
  @metrics             = opts[:metrics] || Kriterion::Metrics.new
  @hostname            = opts[:hostname]
  @port                = opts[:port]
  @database            = opts[:database]
  @client              = Mongo::Client.new(
    ["#{@hostname}:#{@port}"], database: @database
  )
  @client.logger.level = logger.level
  @standards_db        = @client[:standards]
  @sections_db         = @client[:sections]
  @items_db            = @client[:items]
  @resources_db        = @client[:resources]
  @events_db           = @client[:events]
  @standard_details_db = @client[:standard_details]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



19
20
21
# File 'lib/kriterion/backend/mongodb.rb', line 19

def client
  @client
end

#databaseObject (readonly)

Returns the value of attribute database.



18
19
20
# File 'lib/kriterion/backend/mongodb.rb', line 18

def database
  @database
end

#events_dbObject (readonly)

Returns the value of attribute events_db.



24
25
26
# File 'lib/kriterion/backend/mongodb.rb', line 24

def events_db
  @events_db
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



16
17
18
# File 'lib/kriterion/backend/mongodb.rb', line 16

def hostname
  @hostname
end

#items_dbObject (readonly)

Returns the value of attribute items_db.



22
23
24
# File 'lib/kriterion/backend/mongodb.rb', line 22

def items_db
  @items_db
end

#metricsObject (readonly)

Returns the value of attribute metrics.



26
27
28
# File 'lib/kriterion/backend/mongodb.rb', line 26

def metrics
  @metrics
end

#portObject (readonly)

Returns the value of attribute port.



17
18
19
# File 'lib/kriterion/backend/mongodb.rb', line 17

def port
  @port
end

#resources_dbObject (readonly)

Returns the value of attribute resources_db.



23
24
25
# File 'lib/kriterion/backend/mongodb.rb', line 23

def resources_db
  @resources_db
end

#sections_dbObject (readonly)

Returns the value of attribute sections_db.



21
22
23
# File 'lib/kriterion/backend/mongodb.rb', line 21

def sections_db
  @sections_db
end

#standard_details_dbObject (readonly)

Returns the value of attribute standard_details_db.



25
26
27
# File 'lib/kriterion/backend/mongodb.rb', line 25

def standard_details_db
  @standard_details_db
end

#standards_dbObject (readonly)

Returns the value of attribute standards_db.



20
21
22
# File 'lib/kriterion/backend/mongodb.rb', line 20

def standards_db
  @standards_db
end

Instance Method Details

#add_event(event) ⇒ Object



85
86
87
# File 'lib/kriterion/backend/mongodb.rb', line 85

def add_event(event)
  insert_into_db(events_db, event)
end

#add_item(item) ⇒ Object



77
78
79
# File 'lib/kriterion/backend/mongodb.rb', line 77

def add_item(item)
  insert_into_db(items_db, item)
end

#add_resource(resource) ⇒ Object



81
82
83
# File 'lib/kriterion/backend/mongodb.rb', line 81

def add_resource(resource)
  insert_into_db(resources_db, resource)
end

#add_section(section) ⇒ Object



73
74
75
# File 'lib/kriterion/backend/mongodb.rb', line 73

def add_section(section)
  insert_into_db(sections_db, section)
end

#add_standard(standard) ⇒ Object



69
70
71
# File 'lib/kriterion/backend/mongodb.rb', line 69

def add_standard(standard)
  insert_into_db(standards_db, standard)
end

#add_unchanged_node(resource, certname) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/kriterion/backend/mongodb.rb', line 89

def add_unchanged_node(resource, certname)
  resources_db.update_one(
    { resource: resource.resource },
    '$addToSet' => {
      unchanged_nodes: certname
    }
  )
end

#find_sections(query) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/kriterion/backend/mongodb.rb', line 61

def find_sections(query)
  sections_db.find(
    query
  ).map do |section|
    Kriterion::Section.new(section)
  end
end

#get_standard(name, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kriterion/backend/mongodb.rb', line 46

def get_standard(name, opts = {})
  standard = nil
  metrics[:backend_get_standard] += Benchmark.realtime do
    # Set recursion to false by default
    opts[:recurse] = opts[:recurse] || false

    standard = sanitise_standard(find_standard(name))
    return nil if standard.nil?

    find_children!(standard) if opts[:recurse]
  end

  standard
end

#purge_events!(certname) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/kriterion/backend/mongodb.rb', line 114

def purge_events!(certname)
  # Delete all events for this certname
  events_db.delete_many(
    certname: certname
  )

  # Delete all instances of this certname under "unchanged_nodes"
  resources_db.update_many(
    {}, # Don't pass a query as we want to purge everything
    '$pull' => { # Remove this node from unchanged nodes
      unchanged_nodes: certname
    }
  )
end

#update_compliance!(thing) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kriterion/backend/mongodb.rb', line 98

def update_compliance!(thing)
  databases = {
    Kriterion::Standard => standards_db,
    Kriterion::Section  => sections_db,
    Kriterion::Item     => items_db,
    Kriterion::Resource => resources_db
  }

  databases[thing.class].update_one(
    { uuid: thing.uuid },
    '$set' => {
      compliance: thing.compliance
    }
  )
end