Class: MonitorTypeFluidDb

Inherits:
MonitorTypeThreshold show all
Defined in:
lib/monitor_type/fluiddb.rb

Overview

A database class for checking a single number against a threshold. For example,

get the max timestamp from a table as a date.
subtract this from now
=> check that the number is not greater than 2

Instance Method Summary collapse

Methods inherited from MonitorTypeThreshold

#check, #initialize, #process

Methods inherited from MonitorType

#alert, #initialize, #process, #run

Constructor Details

This class inherits a constructor from MonitorTypeThreshold

Instance Method Details

#derived_valueObject



54
55
56
57
58
59
60
61
# File 'lib/monitor_type/fluiddb.rb', line 54

def derived_value
  @fluid_db.query_for_value(@sql, [])
rescue StandardError
  string = "*** FluidDb encountered an error while running the sql\n" \
           "*** sql: #{@sql}\n" \
           "*** Please fix the query and run again\n"
  raise MonitorTypeExceptionHandled, string
end

#extract_paramsObject

Extract parameters

Parameters:

  • uri (String)

    Connection string to db

  • sql (String)

    SQL statement to gather a single value



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/monitor_type/fluiddb.rb', line 13

def extract_params
  if @params[:uri].nil?
    string = "*** FluidDb parameter missing, uri\n" \
             "*** :uri => <uri pointing to db to be monitored>"
    fail MonitorTypeParameterMissingError, string
  end
  begin
    @uri = URI.parse(@params[:uri])
  rescue URI::InvalidURIError
    string = '*** FluidDb encountered an error while parsing the uri' \
             "*** uri: #{@params[:uri]}" \
             "*** Please fix the uri and run again"
    raise MonitorTypeParameterMissingError, string
  end

  if @params[:sql].nil?
    string = '*** FluidDb parameter missing, sql' \
             "*** :sql => <sql statement, producing a single " \
             'column, single row which yeidls a number>'
    fail MonitorTypeParameterMissingError, string
  end
  @sql = @params[:sql]
  @context_sentence = "Checking result of sql query, #{@sql}"
end

#setupObject

Create the connection to the db, and get the value This ensures that all params are correct.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/monitor_type/fluiddb.rb', line 40

def setup
  begin
    @fluid_db = FluidDb2.db(@uri)
  rescue StandardError => e
    string = "*** FluidDb encountered an error while connecting to the db\n" \
             "*** Error: #{e.message}\n" \
             "*** uri: #{@uri}\n" \
             "*** Please fix the error and run again\n"
    raise MonitorTypeExceptionHandled, string
  end

  @params[:fluidDb] = @fluid_db
end

#teardownObject



63
64
65
# File 'lib/monitor_type/fluiddb.rb', line 63

def teardown
  @fluid_db.close
end