Module: Telemetry::Snmp::Data

Extended by:
DefaultOpts
Defined in:
lib/telemetry/snmp/data.rb,
lib/telemetry/snmp/data/models/oid.rb,
lib/telemetry/snmp/data/models/user.rb,
lib/telemetry/snmp/data/default_opts.rb,
lib/telemetry/snmp/data/models/device.rb,
lib/telemetry/snmp/data/models/oid_walk.rb,
lib/telemetry/snmp/data/models/oid_group.rb,
lib/telemetry/snmp/data/models/device_cred.rb,
lib/telemetry/snmp/data/models/device_lock.rb,
lib/telemetry/snmp/data/models/oid_oid_groups.rb,
lib/telemetry/snmp/data/models/user_audit_log.rb

Defined Under Namespace

Modules: DefaultOpts, Model

Class Method Summary collapse

Methods included from DefaultOpts

adapter, database, default_credentials, env_key, host, max_connections, password, pool_timeout, port, preconnect, username

Class Method Details

.connected?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/telemetry/snmp/data.rb', line 48

def connected?
  connection.test_connection
end

.connection(**opts) ⇒ Object



44
45
46
# File 'lib/telemetry/snmp/data.rb', line 44

def connection(**opts)
  @connection ||= Sequel.connect(**opts.merge(default_credentials))
end

.load_models(*models_array) ⇒ Object



33
34
35
36
37
38
# File 'lib/telemetry/snmp/data.rb', line 33

def load_models(*models_array)
  models_array = models if models_array.empty?
  models_array.each do |model|
    require "telemetry/snmp/data/models/#{model}.rb"
  end
end

.migrationObject



18
19
20
# File 'lib/telemetry/snmp/data.rb', line 18

def migration
  @migration_version = Sequel::Migrator.run(connection, migration_path, use_transactions: true)
end

.migration_pathObject



14
15
16
# File 'lib/telemetry/snmp/data.rb', line 14

def migration_path
  "#{__dir__}/data/migrations"
end

.migration_versionObject



22
23
24
# File 'lib/telemetry/snmp/data.rb', line 22

def migration_version
  @migration_version || migration
end

.migrations_up_to_date?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/telemetry/snmp/data.rb', line 26

def migrations_up_to_date?
  Sequel::Migrator.check_current(connection, migration_path)
  true
rescue Sequel::Migrator::NotCurrentError
  false
end

.modelsObject



40
41
42
# File 'lib/telemetry/snmp/data.rb', line 40

def models
  %w[user device_cred device device_lock oid oid_group oid_oid_groups oid_walk user_audit_log]
end

.setup_oj_serializerObject



52
53
54
55
56
# File 'lib/telemetry/snmp/data.rb', line 52

def setup_oj_serializer
  Sequel::Plugins::Serialization.register_format(:oj_json,
                                                 ->(value) { Oj.dump(value) },
                                                 ->(value) { Oj.load(value.nil? ? 'null' : value) })
end

.start!Object



58
59
60
61
62
63
64
65
# File 'lib/telemetry/snmp/data.rb', line 58

def start!
  raise 'failed to start db connection' unless connected?

  migration
  setup_oj_serializer

  load_models
end