项目作者: palkan

项目描述 :
InfluxDB ActiveRecord-style
高级语言: Ruby
项目地址: git://github.com/palkan/influxer.git
创建时间: 2014-07-30T14:25:39Z
项目社区:https://github.com/palkan/influxer

开源协议:MIT License

下载


Build

Influxer

Influxer provides an ActiveRecord-style way to work with InfluxDB with many useful features, such as:

Installation

Adding to a gem:

  1. # my-cool-gem.gemspec
  2. Gem::Specification.new do |spec|
  3. # ...
  4. spec.add_dependency "influxer", ">= 1.2.0"
  5. # ...
  6. end

Or adding to your project:

  1. # Gemfile
  2. gem "influxer", "~> 1.2"

Usage

Metrics classes

To query InfluxDB or write to it, you should define a metrics class first. Each metrics class represents a measurement/series (or multiple related measurements):

  1. class VisitsMetrics < Influxer::Metrics
  2. # Define tags...
  3. tags :account_id, :page_id
  4. # ...and attributes
  5. attributes :user_id, :browser
  6. end

Querying

Now you can use your metrics classes in a similar way to Active Record models to build queries. For example:

  1. VisitsMetrics.select(:account_id, :user_id).where(page_id: /^home\/.*/)

Influxer provides special query methods for dealing with time series:

  • Group by time: Metrics.time(:hour) => # select * ... group by time(1h).
  • Select only points for the last hour/minute/whatever: Metrics.past(:day) => # select * ... where time > now() - 1d.
  • Select only points since the specified time: Metrics.since(Time.utc(2014,12,31)) => # select * ... where time > 1419984000s.
  • and more.

See our Wiki for more.

Scopes support

You can define scopes to re-use query conditions:

  1. class Metrics < Influxer::Metrics
  2. tags :account_id
  3. attributes :value
  4. default_scope -> { time(:hour).limit(1000) }
  5. scope :unlimited, -> { limit(nil) }
  6. scope :by_account, ->(id) { where(account_id: id) if id.present? }
  7. end
  8. Metrics.by_account(1)
  9. # => select * from "metrics" group by time(1h) where account_id=1 limit 1000
  10. Metrics.unlimited.by_account(1).time(:week)
  11. # => select * from "metrics" group by time(1w) where account_id=1

Active Record integration

You can association metrics with Active Record models:

  1. class UserVisits < Influxer::Metrics
  2. end
  3. class User < ActiveRecord::Base
  4. has_metrics :visits
  5. end
  6. user = User.find(1)
  7. user.visits.write(page_id: "home")
  8. #=> < creates point {user_id: 1, page_id: 'home'} in 'user_visits' series >
  9. user.visits.where(page_id: "home")
  10. #=> select * from user_visits where page_id='home'

Find more on Wiki.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/influxer.

License

The gem is available as open source under the terms of the MIT License.