Sha256: 1e4c02f4bbbb88e47f6ba461b0a4955fe04d0c91755bf8d622c4c995ded64e18

Contents?: true

Size: 1.45 KB

Versions: 20

Compression:

Stored size: 1.45 KB

Contents

module Remi
  module DataSource
    class Postgres
      include DataSource

      def initialize(fields: {}, credentials:, query:, logger: Remi::Settings.logger)
        @fields = fields
        @credentials = credentials
        @query = query
        @logger = logger
      end

      attr_accessor :fields

      def extract
        @logger.info "Executing query #{@query}"
        @raw_result = pg_conn.exec @query
      end

      def raw_result
        @raw_result ||= extract
      end

      def pg_conn
        @pg_conn ||= PG.connect(
          host:     @credentials[:host] || 'localhost',
          port:     @credentials[:port] || 5432,
          dbname:   @credentials[:dbname],
          user:     @credentials[:user] || `whoami`.chomp,
          password: @credentials[:password],
          sslmode:  @credentials[:sslmode] || 'require'
        )
      end


      def to_dataframe
        # Performance for larger sets could be improved by using bulk query (via COPY)
        @logger.info "Converting query to a dataframe"

        hash_array = {}
        raw_result.each do |row|
          row.each do |field, value|
            (hash_array[field_symbolizer.call(field)] ||= []) << value
          end
        end

        # After converting to DF, clear the PG results to save memory.
        raw_result.clear

        Daru::DataFrame.new hash_array, order: hash_array.keys
      end

      def df
        @dataframe ||= to_dataframe
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
remi-0.2.21 lib/remi/data_source/postgres.rb
remi-0.2.20 lib/remi/data_source/postgres.rb
remi-0.2.19 lib/remi/data_source/postgres.rb
remi-0.2.18 lib/remi/data_source/postgres.rb
remi-0.2.17 lib/remi/data_source/postgres.rb
remi-0.2.16 lib/remi/data_source/postgres.rb
remi-0.2.15 lib/remi/data_source/postgres.rb
remi-0.2.14 lib/remi/data_source/postgres.rb
remi-0.2.13 lib/remi/data_source/postgres.rb
remi-0.2.12 lib/remi/data_source/postgres.rb
remi-0.2.11 lib/remi/data_source/postgres.rb
remi-0.2.10 lib/remi/data_source/postgres.rb
remi-0.2.9 lib/remi/data_source/postgres.rb
remi-0.2.8 lib/remi/data_source/postgres.rb
remi-0.2.7 lib/remi/data_source/postgres.rb
remi-0.2.6 lib/remi/data_source/postgres.rb
remi-0.2.5 lib/remi/data_source/postgres.rb
remi-0.2.4 lib/remi/data_source/postgres.rb
remi-0.2.3 lib/remi/data_source/postgres.rb
remi-0.2.2 lib/remi/data_source/postgres.rb