influxer 1.1.5 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +102 -0
- data/{MIT-LICENSE → LICENSE.txt} +1 -1
- data/README.md +106 -47
- data/lib/influxer.rb +10 -9
- data/lib/influxer/client.rb +2 -2
- data/lib/influxer/config.rb +19 -6
- data/lib/influxer/engine.rb +1 -1
- data/lib/influxer/metrics/active_model3/model.rb +2 -4
- data/lib/influxer/metrics/metrics.rb +10 -13
- data/lib/influxer/metrics/quoting/timestamp.rb +23 -11
- data/lib/influxer/metrics/relation.rb +33 -17
- data/lib/influxer/metrics/relation/calculations.rb +1 -1
- data/lib/influxer/metrics/relation/time_query.rb +15 -13
- data/lib/influxer/metrics/relation/where_clause.rb +15 -7
- data/lib/influxer/metrics/scoping.rb +4 -4
- data/lib/influxer/metrics/scoping/current_scope.rb +2 -1
- data/lib/influxer/metrics/scoping/default.rb +1 -1
- data/lib/influxer/metrics/scoping/named.rb +2 -1
- data/lib/influxer/model.rb +4 -9
- data/lib/influxer/rails/client.rb +6 -6
- data/lib/influxer/version.rb +1 -1
- metadata +32 -97
- data/.gitignore +0 -37
- data/.rspec +0 -2
- data/.rubocop.yml +0 -77
- data/.travis.yml +0 -10
- data/Changelog.md +0 -116
- data/Gemfile +0 -10
- data/Rakefile +0 -13
- data/gemfiles/rails32.gemfile +0 -7
- data/gemfiles/rails42.gemfile +0 -7
- data/gemfiles/rails5.gemfile +0 -7
- data/influxer.gemspec +0 -33
- data/spec/cases/points_spec.rb +0 -36
- data/spec/cases/write_points_spec.rb +0 -85
- data/spec/client_spec.rb +0 -46
- data/spec/fixtures/empty_result.json +0 -21
- data/spec/fixtures/single_series.json +0 -29
- data/spec/metrics/metrics_spec.rb +0 -283
- data/spec/metrics/relation_spec.rb +0 -485
- data/spec/metrics/scoping_spec.rb +0 -66
- data/spec/model/user_spec.rb +0 -46
- data/spec/spec_helper.rb +0 -64
- data/spec/support/metrics/action_metrics.rb +0 -5
- data/spec/support/metrics/custom_metrics.rb +0 -6
- data/spec/support/metrics/dummy_metrics.rb +0 -12
- data/spec/support/metrics/user_metrics.rb +0 -6
- data/spec/support/metrics/visits_metrics.rb +0 -8
- data/spec/support/shared_contexts/shared_query.rb +0 -16
- data/spec/support/user.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af367b48817287138ec7eddf616361dd101b69e90db4610f2640899a0c4bfcd1
|
4
|
+
data.tar.gz: 4a74d454aa9a9fb044530295878c0c75ab8a2a4705115dfeddf5c358c4373c01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd988870b77abda0c5db4d0f0f9d2d9c88796ed7d0833c2f32e54c27bbd9bbdf7de8fe0344bc67e62f4c85a45af000afe9f9aa02a17eceedb47b00ddb89422b7
|
7
|
+
data.tar.gz: c290df58fc6f0946ae88097fdbfc698a72d760fffb71451e6ab4fb7ff1cf1c256d500659c8bd59b8844bf837b4bb240e2e88826869c1d916214f4c76831e8406
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Change log
|
2
|
+
|
3
|
+
## master (unreleased)
|
4
|
+
|
5
|
+
## 1.3.0 (2020-10-27)
|
6
|
+
|
7
|
+
- Fixes [#53](https://github.com/palkan/influxer/issues/53) Influxer client configuration issue with anyway config v2 and higher.([@AlexanderShvaykin][])
|
8
|
+
|
9
|
+
## 1.2.2 (2020-10-27)
|
10
|
+
|
11
|
+
- Fixes [#49](https://github.com/palkan/influxer/issues/49) Cache hash configuration cannot be applied.([@AlexanderShvaykin][])
|
12
|
+
- Fixes [#47](https://github.com/palkan/influxer/issues/47) Can't delete data when retention policy is set for a metric. ([@MPursche][])
|
13
|
+
|
14
|
+
## 1.2.1 (2020-07-09)
|
15
|
+
|
16
|
+
- Support for setting timezone in queries to configure influx time calculations, e.g., time epoch aggregation ([@jklimke][])
|
17
|
+
|
18
|
+
[PR](https://github.com/palkan/influxer/pull/46)
|
19
|
+
|
20
|
+
## 1.2.0 (2019-05-20)
|
21
|
+
|
22
|
+
- **Require Ruby 2.4+**
|
23
|
+
|
24
|
+
## 1.1.6
|
25
|
+
|
26
|
+
- [Fixes [#41](https://github.com/palkan/influxer/issues/41)] Fix query building with empty arrays in `where` clause ([@dimiii][])
|
27
|
+
|
28
|
+
[PR](https://github.com/palkan/influxer/pull/44)
|
29
|
+
|
30
|
+
**BREAKING:** `where.not` now returns non-empty result for an empty array.
|
31
|
+
|
32
|
+
- Support of year alias in queries ([@dimiii][])
|
33
|
+
|
34
|
+
- [Fixes [#40](https://github.com/palkan/influxer/issues/40)] Avoid adding precision suffix to write queries. ([@palkan][])
|
35
|
+
|
36
|
+
## 1.1.5
|
37
|
+
|
38
|
+
- [Fixes [#37](https://github.com/palkan/influxer/issues/37)] Timestamp ranges are quoted again. ([@jklimke][])
|
39
|
+
|
40
|
+
## 1.1.4
|
41
|
+
|
42
|
+
- [Fixes [#35](https://github.com/palkan/influxer/issues/35)] Support time duration suffix and handle `'s'` and `'ms'` precision. ([@palkan][])
|
43
|
+
|
44
|
+
[PR](https://github.com/palkan/influxer/pull/36)
|
45
|
+
|
46
|
+
**BREAKING:** `Time`-like value are only type-casted for `time` key.
|
47
|
+
|
48
|
+
## 1.1.2
|
49
|
+
|
50
|
+
- Support exclusive ranges as `where` arguments. ([@MPursche][])
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# range including the end
|
54
|
+
where(a: 1..4)
|
55
|
+
#=> ... WHERE a >= 1 AND a <= 4
|
56
|
+
|
57
|
+
#range excluding the end
|
58
|
+
where(a: 1...4)
|
59
|
+
#=> ... WHERE a >= 1 AND a < 4
|
60
|
+
```
|
61
|
+
|
62
|
+
## 1.1.1
|
63
|
+
|
64
|
+
- [Fixes [#31](https://github.com/palkan/influxer/issues/31)] Fix bug with empty arrays in `where` clause
|
65
|
+
|
66
|
+
- Introduce `Relation#none` method
|
67
|
+
|
68
|
+
## 1.1.0
|
69
|
+
|
70
|
+
### Features
|
71
|
+
|
72
|
+
- Add ability to specify per-metrics retention-policy, precision and database
|
73
|
+
|
74
|
+
Now you can override default configuration for a specific metrics class:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class CustomMetrics < Influxer::Metrics
|
78
|
+
set_database "custom_db"
|
79
|
+
set_retention_policy :yearly
|
80
|
+
set_precision "ms"
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
### Fixes
|
85
|
+
|
86
|
+
- [Fixes [#30](https://github.com/palkan/influxer/issues/30)] Fix writing points with custom retention policy
|
87
|
+
|
88
|
+
### Misc
|
89
|
+
|
90
|
+
- Update Rubocop configuration and add Rubocop Rake task to defaults
|
91
|
+
|
92
|
+
## 1.0.1
|
93
|
+
|
94
|
+
- Fix missing `#delegate` in ActiveRecord 3.2
|
95
|
+
|
96
|
+
See [changelog](https://github.com/palkan/influxer/blob/1.0.0/Changelog.md) for earlier versions.
|
97
|
+
|
98
|
+
[@palkan]: https://github.com/palkan
|
99
|
+
[@MPursche]: https://github.com/MPursche
|
100
|
+
[@jklimke]: https://github.com/jklimke
|
101
|
+
[@dimiii]: https://github.com/dimiii
|
102
|
+
[@AlexanderShvaykin]: https://github.com/AlexanderShvaykin
|
data/{MIT-LICENSE → LICENSE.txt}
RENAMED
data/README.md
CHANGED
@@ -1,50 +1,109 @@
|
|
1
|
-
|
2
|
-
## Influxer
|
1
|
+
![Build](https://github.com/palkan/influxer/workflows/Build/badge.svg)
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
**NOTE**: Influxer is Rails 4+ compatible!
|
3
|
+
# Influxer
|
7
4
|
|
8
5
|
Influxer provides an ActiveRecord-style way to work with [InfluxDB](https://influxdb.com/) with many useful features, such as:
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Adding to a gem:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# my-cool-gem.gemspec
|
13
|
+
Gem::Specification.new do |spec|
|
14
|
+
# ...
|
15
|
+
spec.add_dependency "influxer", ">= 1.2.0"
|
16
|
+
# ...
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
Or adding to your project:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# Gemfile
|
24
|
+
gem "influxer", "~> 1.2"
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### Metrics classes
|
30
|
+
|
31
|
+
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):
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class VisitsMetrics < Influxer::Metrics
|
35
|
+
# Define tags...
|
36
|
+
tags :account_id, :page_id
|
37
|
+
# ...and attributes
|
38
|
+
attributes :user_id, :browser
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Querying
|
43
|
+
|
44
|
+
Now you can use your metrics classes in a similar way to Active Record models to build queries. For example:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
VisitsMetrics.select(:account_id, :user_id).where(page_id: /^home\/.*/)
|
48
|
+
```
|
49
|
+
|
50
|
+
Influxer provides special query methods for dealing with time series:
|
51
|
+
|
52
|
+
- Group by time: `Metrics.time(:hour) => # select * ... group by time(1h)`.
|
53
|
+
- Select only points for the last hour/minute/whatever: `Metrics.past(:day) => # select * ... where time > now() - 1d`.
|
54
|
+
- Select only points since the specified time: `Metrics.since(Time.utc(2014,12,31)) => # select * ... where time > 1419984000s`.
|
55
|
+
- and more.
|
56
|
+
|
57
|
+
See [our Wiki](https://github.com/palkan/influxer/wiki/Query-methods) for more.
|
58
|
+
|
59
|
+
### Scopes support
|
60
|
+
|
61
|
+
You can define scopes to re-use query conditions:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class Metrics < Influxer::Metrics
|
65
|
+
tags :account_id
|
66
|
+
attributes :value
|
67
|
+
|
68
|
+
default_scope -> { time(:hour).limit(1000) }
|
69
|
+
|
70
|
+
scope :unlimited, -> { limit(nil) }
|
71
|
+
scope :by_account, ->(id) { where(account_id: id) if id.present? }
|
72
|
+
end
|
73
|
+
|
74
|
+
Metrics.by_account(1)
|
75
|
+
# => select * from "metrics" group by time(1h) where account_id=1 limit 1000
|
76
|
+
|
77
|
+
Metrics.unlimited.by_account(1).time(:week)
|
78
|
+
# => select * from "metrics" group by time(1w) where account_id=1
|
79
|
+
```
|
80
|
+
|
81
|
+
### Active Record integration
|
82
|
+
|
83
|
+
You can association metrics with Active Record models:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class UserVisits < Influxer::Metrics
|
87
|
+
end
|
88
|
+
|
89
|
+
class User < ActiveRecord::Base
|
90
|
+
has_metrics :visits
|
91
|
+
end
|
92
|
+
|
93
|
+
user = User.find(1)
|
94
|
+
user.visits.write(page_id: "home")
|
95
|
+
#=> < creates point {user_id: 1, page_id: 'home'} in 'user_visits' series >
|
96
|
+
|
97
|
+
user.visits.where(page_id: "home")
|
98
|
+
#=> select * from user_visits where page_id='home'
|
99
|
+
```
|
100
|
+
|
101
|
+
Find more on [Wiki](https://github.com/palkan/influxer/wiki/ActiveRecord-integration).
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/palkan/influxer](https://github.com/palkan/influxer).
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/influxer.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "influxer/version"
|
4
|
+
require "active_model"
|
5
|
+
require "active_support/core_ext"
|
5
6
|
|
6
7
|
# Rails client for InfluxDB
|
7
8
|
module Influxer
|
@@ -9,16 +10,16 @@ module Influxer
|
|
9
10
|
ActiveModel::VERSION::MAJOR == 3
|
10
11
|
end
|
11
12
|
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
13
|
+
require "influxer/config"
|
14
|
+
require "influxer/client"
|
15
|
+
require "influxer/metrics/metrics"
|
15
16
|
|
16
17
|
module Model # :nodoc:
|
17
|
-
require
|
18
|
+
require "influxer/model"
|
18
19
|
end
|
19
20
|
|
20
|
-
require
|
21
|
-
require
|
21
|
+
require "influxer/rails/client" if defined?(Rails)
|
22
|
+
require "influxer/engine" if defined?(Rails)
|
22
23
|
|
23
24
|
def self.config
|
24
25
|
@config ||= Config.new
|
@@ -33,7 +34,7 @@ module Influxer
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def self.reset!
|
36
|
-
@client
|
37
|
+
@client&.stop!
|
37
38
|
@config = nil
|
38
39
|
@client = nil
|
39
40
|
end
|
data/lib/influxer/client.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "influxdb"
|
4
4
|
|
5
5
|
module Influxer
|
6
6
|
# InfluxDB API client
|
7
7
|
class Client < ::InfluxDB::Client
|
8
8
|
def initialize
|
9
|
-
super Influxer.config.
|
9
|
+
super Influxer.config.to_h.symbolize_keys
|
10
10
|
end
|
11
11
|
|
12
12
|
def time_precision
|
data/lib/influxer/config.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "anyway_config"
|
4
4
|
|
5
5
|
module Influxer
|
6
6
|
# Influxer configuration
|
@@ -28,15 +28,28 @@ module Influxer
|
|
28
28
|
:denormalize,
|
29
29
|
:udp,
|
30
30
|
:async,
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
:cache_enabled,
|
32
|
+
:cache,
|
33
|
+
database: "db",
|
34
|
+
time_precision: "ns",
|
34
35
|
time_duration_suffix_enabled: false
|
35
36
|
|
36
37
|
def load(*)
|
37
38
|
super
|
38
|
-
|
39
|
-
|
39
|
+
if cache_enabled.nil?
|
40
|
+
self.cache_enabled = cache_enabled_value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def cache=(value)
|
45
|
+
super
|
46
|
+
self.cache_enabled = cache_enabled_value
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def cache_enabled_value
|
52
|
+
!!cache
|
40
53
|
end
|
41
54
|
end
|
42
55
|
end
|
data/lib/influxer/engine.rb
CHANGED
@@ -5,10 +5,8 @@ module Influxer
|
|
5
5
|
# Replacement of ActiveModel::Model for ActiveModel 3
|
6
6
|
module Model
|
7
7
|
def initialize(attributes = {})
|
8
|
-
|
9
|
-
|
10
|
-
public_send("#{attr}=", value)
|
11
|
-
end
|
8
|
+
attributes&.each do |attr, value|
|
9
|
+
public_send("#{attr}=", value)
|
12
10
|
end
|
13
11
|
|
14
12
|
super()
|
@@ -1,16 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require "influxer/metrics/relation"
|
4
|
+
require "influxer/metrics/scoping"
|
5
|
+
require "influxer/metrics/quoting/timestamp"
|
6
|
+
require "influxer/metrics/active_model3/model"
|
7
7
|
|
8
8
|
module Influxer
|
9
9
|
class MetricsError < StandardError; end
|
10
10
|
class MetricsInvalid < MetricsError; end
|
11
11
|
|
12
12
|
# Base class for InfluxDB querying and writing
|
13
|
-
# rubocop:disable Metrics/ClassLength
|
14
13
|
class Metrics
|
15
14
|
TIME_FACTOR = 1_000_000_000
|
16
15
|
if Influxer.active_model3?
|
@@ -18,7 +17,6 @@ module Influxer
|
|
18
17
|
else
|
19
18
|
include ActiveModel::Model
|
20
19
|
end
|
21
|
-
# rubocop:enable Style/MixinUsage
|
22
20
|
|
23
21
|
include ActiveModel::Validations
|
24
22
|
extend ActiveModel::Callbacks
|
@@ -35,7 +33,7 @@ module Influxer
|
|
35
33
|
[
|
36
34
|
:write, :write!, :select, :where,
|
37
35
|
:group, :time, :past, :since,
|
38
|
-
:limit, :offset, :fill, :delete_all, :epoch
|
36
|
+
:limit, :offset, :fill, :delete_all, :epoch, :timezone
|
39
37
|
] + Influxer::Calculations::CALCULATION_METHODS
|
40
38
|
),
|
41
39
|
to: :all
|
@@ -125,7 +123,7 @@ module Influxer
|
|
125
123
|
quoted_series(val.call(instance), write: write)
|
126
124
|
when Array
|
127
125
|
if val.length > 1
|
128
|
-
"merge(#{val.map { |s| quoted_series(s, write: write) }.join(
|
126
|
+
"merge(#{val.map { |s| quoted_series(s, write: write) }.join(",")})"
|
129
127
|
else
|
130
128
|
quoted_series(val.first, write: write)
|
131
129
|
end
|
@@ -133,7 +131,7 @@ module Influxer
|
|
133
131
|
# Only add retention policy when selecting points
|
134
132
|
# and not writing
|
135
133
|
if !write && retention_policy.present?
|
136
|
-
[quote(@retention_policy), quote(val)].join(
|
134
|
+
[quote(@retention_policy), quote(val)].join(".")
|
137
135
|
else
|
138
136
|
quote(val)
|
139
137
|
end
|
@@ -169,6 +167,7 @@ module Influxer
|
|
169
167
|
|
170
168
|
def write!
|
171
169
|
raise MetricsInvalid if invalid?
|
170
|
+
|
172
171
|
write
|
173
172
|
end
|
174
173
|
|
@@ -225,13 +224,11 @@ module Influxer
|
|
225
224
|
end
|
226
225
|
|
227
226
|
def parsed_timestamp
|
228
|
-
|
227
|
+
quote_timestamp_for_write @timestamp, client
|
229
228
|
end
|
230
|
-
# rubocop:enable Metrics/MethodLength
|
231
|
-
# rubocop:enable Metrics/AbcSize
|
232
229
|
|
233
230
|
def unquote(name)
|
234
|
-
name.gsub(/(\A['"]|['"]\z)/,
|
231
|
+
name.gsub(/(\A['"]|['"]\z)/, "")
|
235
232
|
end
|
236
233
|
end
|
237
234
|
end
|