Sha256: b7ee0fe34e99ef3ca3a633694b8cfac1152e93018aa23d71758891e9c96bf2c0

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

module Timescaledb
  class Hypertable < ::Timescaledb::ApplicationRecord
    self.table_name = "timescaledb_information.hypertables"
    self.primary_key = "hypertable_name"

    has_many :jobs, foreign_key: "hypertable_name"
    has_many :chunks, foreign_key: "hypertable_name"

    has_many :compression_settings,
      foreign_key: "hypertable_name",
      class_name: "Timescaledb::CompressionSetting"

    has_many :dimensions,
      foreign_key: "hypertable_name",
      class_name: "Timescaledb::Dimension"

    has_many :continuous_aggregates,
      foreign_key: "hypertable_name",
      class_name: "Timescaledb::ContinuousAggregate"

    def main_dimension
      dimensions.find_by dimension_number: 1
    end

    def chunks_detailed_size
      struct_from "SELECT * from chunks_detailed_size('#{self.hypertable_name}')"
    end

    def approximate_row_count
      struct_from("SELECT * FROM approximate_row_count('#{self.hypertable_name}')").first.approximate_row_count
    end

    def compression_stats
      @compression_stats ||=
        struct_from("SELECT * from hypertable_compression_stats('#{self.hypertable_name}')").first || {}
    end

    def detailed_size
      struct_from("SELECT * FROM hypertable_detailed_size('#{self.hypertable_name}')").first
    end

    def before_total_bytes
      compression_stats["before_compression_total_bytes"] || detailed_size.total_bytes
    end

    def after_total_bytes
      compression_stats["after_compression_total_bytes"] || 0
    end

    private
    def struct_from(sql)
      self.class.connection.execute(sql).map(&OpenStruct.method(:new))
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
timescaledb-0.3.0 lib/timescaledb/hypertable.rb
timescaledb-0.2.9 lib/timescaledb/hypertable.rb
timescaledb-0.2.8 lib/timescaledb/hypertable.rb
timescaledb-0.2.7 lib/timescaledb/hypertable.rb
timescaledb-0.2.6 lib/timescaledb/hypertable.rb