timeseries-hoarder/lib/timeseries/hoarder/TimeseriesTable.rb

36 lines
No EOL
1.4 KiB
Ruby

require_relative 'Table.rb'
require_relative 'CachingTable.rb'
module Timeseries
module Hoarder
class SeriesTable < Table
def initialize(db, name, **opts)
@chunk_time = opts[:chunk_time] || '3h'
@compress_chunk_time = opts[:compress_chunk_time] || '24h'
@name = name
@tags = CachingTable.new(db, name + '_tags')
super(db, name, 'ts_hoarder')
end
def table_creation
@pg.exec("CREATE TABLE #{@table_id} ( time TIMESTAMPTZ NOT NULL, source_id INTEGER NOT NULL, tags_id INTEGER NOT NULL, value DOUBLE PRECISION NOT NULL)")
@pg.exec_params("SELECT create_hypertable('#{@table_schema}.#{@table_name}', 'time', chunk_time_interval => $1::interval)", [@chunk_time])
@pg.exec_params("ALTER TABLE #{@table_id} SET (timescaledb.compress, timescaledb.compress_segmentby = 'source_id, tags_id', timescaledb.compress_chunk_time_interval='#{@compress_chunk_time}')")
view_sql = <<SQL
CREATE OR REPLACE VIEW #{@name} AS
SELECT time, source, tags, value
FROM #{@table_id}
INNER JOIN #{@tags.table_id} USING ( tags_id )
INNER JOIN #{@db.data_sources.table_id} USING ( source_id )
SQL
@pg.exec(view_sql)
end
end
end
end