Sha256: 5811b350ffa7eeacbad60aeb429b0902013eed674a1782b8c006a94d762d2cdb

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Timescale
  class Hypertable < ActiveRecord::Base
    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: "Timescale::CompressionSetting"

    has_one :dimensions,
      foreign_key: "hypertable_name",
      class_name: "Timescale::Dimension"

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

    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

1 entries across 1 versions & 1 rubygems

Version Path
timescaledb-0.1.5 lib/timescale/hypertable.rb