最近在使用 InfluxDB,没有看到啥 ORM,只有一个 palkan/influxer,看着使用方式不是很喜欢,于是自己撸了,虽然比较粗糙,不过胜在简单方便,代码也就三四百来行,有兴趣的可以试试 InfluxORM
InfluxORM.setup(
connection: { # 具体见 InfluxDB::Client.new
database: 'test'
}
)
class Memory
include InfluxORM
influx_tag :host
influx_tag :region
influx_value :free
influx_value :used, :int # support :int, :float, :boolean, :string
end
Memory.insert(host: 'A', region: 'US', free: 1234, used: 2234)
Memory.import([
{host: 'A', region: 'US', free: 1234, used: 2234, timestamp: 1234567890},
{host: 'A', region: 'US', free: 1244, used: 2224, timestamp: 1234567900},
{host: 'B', region: 'US', free: 234, used: 3234}
])
Memory.count # => 4
Memory.where(host: 'A').count # => 1
Memory.select('mean(*)') \
.where(host: 'A', time: {gte: Time.now - 10.day, lte: Time.now - 1.day}) \
.group_by('time(1m) fill(0)').result
Memory.where(host: 'B').or(host: 'C').limit(10).result
Memory.where("host = 'A' AND time > now() - 1d").where(free: {lt: 1024}).result
query_obj = Memory.where(host: 'A').or(host: 'B')
Memory.where(region: 'US').or(query_obj) # select * from memorys where region = 'US' OR (host = 'A' OR host = 'B')
More query methods
select
: select('mean(*)')
, select({mean: 'tag_name', sum: 'tag_name'})
where
: where('tag = \'value\'')
, where(tag: 'value', time: {gt: Time.now - 1.day})
or
: or('tag = \'value\'')
, or(tag: 'value', time: {gt: Time.now - 1.day})
group_by
: group_by('host')
fill
: fill(0)
limit
: limit(1)
slimit
: slimit(1)
offset
: offset(1)
soffset
: soffset(1)