feat: 🚧 begin work on a proper Timeseries table
This commit is contained in:
parent
3d15464521
commit
2e74de359c
3 changed files with 47 additions and 0 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
require_relative "hoarder/version"
|
||||
|
||||
require_relative 'hoarder/TimeseriesDatabase.rb'
|
||||
|
||||
module Timeseries
|
||||
module Hoarder
|
||||
class Error < StandardError; end
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
require 'pg'
|
||||
|
||||
require_relative 'CachingTable.rb'
|
||||
require_relative 'TimeseriesTable.rb'
|
||||
|
||||
module Timeseries
|
||||
module Hoarder
|
||||
|
@ -18,6 +19,14 @@ module Timeseries
|
|||
@pg.exec("CREATE SCHEMA IF NOT EXISTS ts_hoarder")
|
||||
|
||||
@data_sources = CachingTable.new(self, 'sources', 'source')
|
||||
|
||||
@series = {}
|
||||
end
|
||||
|
||||
def add_timeseries(name, **opts)
|
||||
return @series[name] if @series.include? name
|
||||
|
||||
new_series = SeriesTable.new(self, name, **opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
36
lib/timeseries/hoarder/TimeseriesTable.rb
Normal file
36
lib/timeseries/hoarder/TimeseriesTable.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue