Skip to content

Commit

Permalink
Upgrade readme and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Jul 9, 2020
1 parent f0304bd commit c9dd095
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion MIT-LICENSE → LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2014-2019 palkan
Copyright 2014-2020 Vladimir Dementyev

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
86 changes: 71 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,72 @@
[![Gem Version](https://badge.fury.io/rb/influxer.svg)](https://rubygems.org/gems/influxer) [![Build Status](https://travis-ci.org/palkan/influxer.svg?branch=master)](https://travis-ci.org/palkan/influxer)

## Influxer
# Influxer

**NOTE**: Version 0.3.x supports InfluxDB >= 0.9.0. For InfluxDB 0.8.x use [version 0.2.5](https://github.com/palkan/influxer/tree/0.2.5).
Influxer provides an ActiveRecord-style way to work with [InfluxDB](https://influxdb.com/) with many useful features, such as:

**NOTE**: Influxer is Rails 4+ compatible! (Rails 3.2 support is still included but no longer required to pass all the tests).
## Installation

Influxer provides an ActiveRecord-style way to work with [InfluxDB](https://influxdb.com/) with many useful features, such as:
- Familar query language (use `select`, `where`, `not`, `group` etc).
- Support for Regex conditions: `where(page_id: /^home\/.*/) #=> select * ... where page_id=~/^home\/.*/`.
- Special query methods for InfluxDB:
- `time` - group by time (e.g. `Metrics.time(:hour) => # select * ... group by time(1h)`);
- `past` - get only points for last hour/minute/whatever (e.g. `Metrics.past(:day) => # select * ... where time > now() - 1d`);
- `since` - get only points since date (e.g. `Metrics.since(Time.utc(2014,12,31)) => # select * ... where time > 1419984000s`);
- `merge` - merge series.
- Scopes support
Adding to a gem:

```ruby
# my-cool-gem.gemspec
Gem::Specification.new do |spec|
# ...
spec.add_dependency "influxer", ">= 1.2.0"
# ...
end
```

Or adding to your project:

```ruby
# Gemfile
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):

```ruby
class VisitsMetrics < Influxer::Metrics
# Define tags...
tags :account_id, :page_id
# ...and attributes
attributes :user_id, :browser
end
```

### Querying

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

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

Influer 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](https://github.com/palkan/influxer/wiki/Query-methods) for more.

### Scopes support

You can define scopes to re-use query conditions:

```ruby
class Metrics < Influxer::Metrics
default_scope -> { time(:hour).limit(1000) }
tags :account_id
attributes :value

default_scope -> { time(:hour).limit(1000) }

scope :unlimited, -> { limit(nil) }
scope :by_account, ->(id) { where(account_id: id) if id.present? }
end
Expand All @@ -32,7 +78,9 @@ Metrics.unlimited.by_account(1).time(:week)
# => select * from "metrics" group by time(1w) where account_id=1
```

- Integrate with your model:
### Active Record integration

You can association metrics with Active Record models:

```ruby
class UserVisits < Influxer::Metrics
Expand All @@ -50,4 +98,12 @@ user.visits.where(page_id: "home")
#=> select * from user_visits where page_id='home'
```

Find more on [Wiki](https://github.com/palkan/influxer/wiki).
Find more on [Wiki](https://github.com/palkan/influxer/wiki/ActiveRecord-integration).

## Contributing

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

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
6 changes: 2 additions & 4 deletions influxer.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path("lib", __dir__)

require "influxer/version"
require_relative "lib/influxer/version"

Gem::Specification.new do |s|
s.name = "influxer"
Expand All @@ -14,7 +12,7 @@ Gem::Specification.new do |s|
s.description = "InfluxDB the Rails way"
s.license = "MIT"

s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
s.files = Dir.glob("lib/**/*") + %w[README.md LICENSE.txt CHANGELOG.md]
s.require_paths = ["lib"]

s.required_ruby_version = ">= 2.4.0"
Expand Down

0 comments on commit c9dd095

Please sign in to comment.