Class: StockCruncher::InfluxDB
- Inherits:
-
Object
- Object
- StockCruncher::InfluxDB
- Defined in:
- lib/stockcruncher/influxdb.rb
Overview
this is a class to write time series to database
Instance Method Summary collapse
-
#create_hash(descriptions, values) ⇒ Object
Method to create a new hash from two arrays of keys and values.
-
#export_history(raw) ⇒ Object
Method to export historical data to database.
-
#export_last_day(raw) ⇒ Object
Method to export latest data to database.
-
#format_values(values) ⇒ Object
Method to format and array of values into comma separated string.
-
#initialize(config, insecure = false) ⇒ InfluxDB
constructor
Class constructor method.
-
#request(url, body) ⇒ Object
Method to send http post request.
-
#write(name, tags, values, date) ⇒ Object
Method to write data in bucket.
Constructor Details
#initialize(config, insecure = false) ⇒ InfluxDB
Class constructor method
11 12 13 14 |
# File 'lib/stockcruncher/influxdb.rb', line 11 def initialize(config, insecure = false) @cfg = config[self.class.name.split('::').last] @insecure = insecure end |
Instance Method Details
#create_hash(descriptions, values) ⇒ Object
Method to create a new hash from two arrays of keys and values
17 18 19 |
# File 'lib/stockcruncher/influxdb.rb', line 17 def create_hash(descriptions, values) descriptions.split(',').zip(values.split(',')).to_h end |
#export_history(raw) ⇒ Object
Method to export historical data to database
34 35 36 |
# File 'lib/stockcruncher/influxdb.rb', line 34 def export_history(raw) # TODO: export whole history and calculated missing values end |
#export_last_day(raw) ⇒ Object
Method to export latest data to database
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/stockcruncher/influxdb.rb', line 22 def export_last_day(raw) raise StandardError, 'No data to export' if raw.match?(/{}/) values = create_hash(*raw.split("\r\n")) values['close'] = values.delete('price') values['changePercent'] = values['changePercent'].delete('%') = { 'symbol' => values.delete('symbol') } date = values.delete('latestDay') write('daily', , values, date) end |
#format_values(values) ⇒ Object
Method to format and array of values into comma separated string
39 40 41 42 43 44 45 46 |
# File 'lib/stockcruncher/influxdb.rb', line 39 def format_values(values) string = '' values.each_pair do |k, v| string += "#{k}=#{v}" string += ',' unless k == values.keys.last end string end |
#request(url, body) ⇒ Object
Method to send http post request
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/stockcruncher/influxdb.rb', line 49 def request(url, body) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme.eql?('https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @insecure req = Net::HTTP::Post.new(uri.request_uri) req.basic_auth(@cfg['user'], @cfg['password']) req.body = body http.request(req) end |
#write(name, tags, values, date) ⇒ Object
Method to write data in bucket
61 62 63 64 65 66 67 68 |
# File 'lib/stockcruncher/influxdb.rb', line 61 def write(name, , values, date) url = "#{@cfg['scheme']}://#{@cfg['host']}:#{@cfg['port']}/write?" \ "db=#{@cfg['dbname']}" = DateTime.parse(date + 'T18:00:00').strftime('%s%N') body = "#{name},#{format_values(tags)} #{format_values(values)} " \ "#{timestamp}" request(url, body) end |