module InfluxORM::Attributes::ClassMethods

Public Instance Methods

attrs_to_point(hash) click to toggle source
# File lib/influx_orm/attributes.rb, line 22
def attrs_to_point(hash)
  point = {tags: {}, values: {}}

  hash.each do |k, v|
    next if k == :timestamp

    if k.to_sym == :time
      point[:timestamp] = format_timestamp(v)
      next
    end

    col_type, data_type = influx_attrs[k.to_sym]
    raise InfluxORM::Error.new("Invalid col_type '#{col_type}' of '#{k}'") unless col_type
    point[col_type][k] = convert_val(data_type, v)
  end

  point[:timestamp] ||= format_timestamp(Time.now)
  point
end
influx_attrs() click to toggle source
# File lib/influx_orm/attributes.rb, line 9
def influx_attrs
  @influx_attrs ||= {}
end
influx_tag(name) click to toggle source
# File lib/influx_orm/attributes.rb, line 13
def influx_tag(name)
  influx_attrs[name.to_sym] = [:tags, :string]
end
influx_value(name, type = :int) click to toggle source
# File lib/influx_orm/attributes.rb, line 17
def influx_value(name, type = :int)
  raise InfluxORM::Error.new("Invalid type '#{type}'") unless ATTR_TYPES.include?(type)
  influx_attrs[name.to_sym] = [:values, type]
end

Private Instance Methods

convert_val(data_type, val) click to toggle source
# File lib/influx_orm/attributes.rb, line 45
def convert_val(data_type, val)
  case data_type.to_sym
  when :int then val.to_i
  when :float then val.to_f
  when :string then val.to_s
  when :boolean then val ? true : false
  else
    raise InfluxORM::Error.new("Invalid data_type '#{data_type}'")
  end
end
format_timestamp(ts) click to toggle source
# File lib/influx_orm/attributes.rb, line 56
def format_timestamp(ts)
  case ts
  when Time, DateTime then ts.to_i
  when Date then ts.to_time.to_i
  when Numeric then ts.to_i
  else
    raise InfluxORM::Error.new("Invalid timestamp value: '#{ts.inspect}'")
  end
end