influx_orm 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/model.rb +29 -0
- data/examples/multiple_config.rb +52 -0
- data/influx_orm.gemspec +33 -0
- data/lib/influx_orm/:noh +37 -0
- data/lib/influx_orm/attributes.rb +67 -0
- data/lib/influx_orm/configuration.rb +42 -0
- data/lib/influx_orm/connection.rb +37 -0
- data/lib/influx_orm/error.rb +5 -0
- data/lib/influx_orm/init_module_generator.rb +37 -0
- data/lib/influx_orm/model.rb +46 -0
- data/lib/influx_orm/query.rb +165 -0
- data/lib/influx_orm/version.rb +3 -0
- data/lib/influx_orm.rb +36 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8a7b5578eb6a3f2fd54b4a2a957e2e44fecefaf
|
4
|
+
data.tar.gz: 926aad7a901cdb7aa68953a30704e2f669c8b745
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d1f657aec7974ab2fe5f16eac459278e8b37e3707a38ac6e3ef0f4d5d39509cb3928d92d40dafebc13fde255d395be9e5a21f9f4dc4fb148c8e3e548339eb1b
|
7
|
+
data.tar.gz: e3fdffc820127be9810f2b5b0a07fa5977136124114bf3cd0f36203309755470f3e785b35bc0df15be20dc092137415652be697ef21d521c4b03f528927d8014
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 jiangzhi.xie
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# InfluxOrm
|
2
|
+
|
3
|
+
A simple influxdb orm for ruby, base [influxdb-ruby](https://github.com/influxdata/influxdb-ruby)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'influx_orm'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install influx_orm
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Init
|
24
|
+
|
25
|
+
```
|
26
|
+
InfluxORM.setup(
|
27
|
+
database: 'xyz'
|
28
|
+
)
|
29
|
+
```
|
30
|
+
|
31
|
+
### Define MEASUREMENTS
|
32
|
+
|
33
|
+
```
|
34
|
+
class Memory
|
35
|
+
include InfluxORM
|
36
|
+
|
37
|
+
influx_tag :host
|
38
|
+
influx_tag :region
|
39
|
+
influx_value :free
|
40
|
+
influx_value :used, :int # support :int, :float, :boolean, :string
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
### Write
|
45
|
+
|
46
|
+
```
|
47
|
+
Memory.insert(host: 'A', region: 'US', free: 1234, used: 2234)
|
48
|
+
|
49
|
+
Memory.import([
|
50
|
+
{host: 'A', region: 'US', free: 1234, used: 2234, timestamp: 1234567890},
|
51
|
+
{host: 'A', region: 'US', free: 1244, used: 2224, timestamp: 1234567900},
|
52
|
+
{host: 'B', region: 'US', free: 234, used: 3234}
|
53
|
+
])
|
54
|
+
```
|
55
|
+
|
56
|
+
### Query
|
57
|
+
|
58
|
+
```
|
59
|
+
Memory.count # => 4
|
60
|
+
Memory.where(host: 'A').count # => 1
|
61
|
+
Memory.select('mean(*)') \
|
62
|
+
.where(host: 'A', time: {gte: Time.now - 10.day, lte: Time.now - 1.day}) \
|
63
|
+
.group_by('time(1m) fill(0)').result
|
64
|
+
|
65
|
+
Memory.where(host: 'B').limit(10).result
|
66
|
+
Memory.where("host = 'a' AND time > now() - 1d")
|
67
|
+
|
68
|
+
query_obj = Memory.where(host: 'A').or(host: 'B')
|
69
|
+
Memory.where(region: 'US').or(query_obj) # select * from memorys where region = 'US' OR (host = 'A' OR host = 'B')
|
70
|
+
```
|
71
|
+
|
72
|
+
Support query methods
|
73
|
+
|
74
|
+
* `select`: `select('mean(*)')`, `select({mean: 'tag_name', sum: 'tag_name'})`
|
75
|
+
* `where`: `where('tag = \'value\'')`, `where(tag: 'value', time: {gt: Time.now - 1.day})`
|
76
|
+
* `or`: `or('tag = \'value\'')`, `or(tag: 'value', time: {gt: Time.now - 1.day})`
|
77
|
+
* `group_by`: `group_by('host')`
|
78
|
+
* `fill`: `fill(0)`
|
79
|
+
* `limit`: `limit(1)`
|
80
|
+
* `slimit`: `slimit(1)`
|
81
|
+
* `offset`: `offset(1)`
|
82
|
+
* `soffset`: `soffset(1)`
|
83
|
+
|
84
|
+
|
85
|
+
## Structure
|
86
|
+
|
87
|
+
`MyModel` has one instance of `Configuration` as `@configuration`
|
88
|
+
|
89
|
+
`@configuration.connection` is a instance of `Connection`
|
90
|
+
|
91
|
+
`MyModel` include modules: `Model` `Attributes`
|
92
|
+
|
93
|
+
`Model` forward query methods to `Query`, forward write to `@configuration.connection`
|
94
|
+
|
95
|
+
`Attributes` define and format the model attributes
|
96
|
+
```
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
101
|
+
|
102
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/influx_orm.
|
107
|
+
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
112
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "influx_orm"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/model.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'influx_orm'
|
2
|
+
|
3
|
+
InfluxORM.setup(
|
4
|
+
connection: {
|
5
|
+
database: 'test1'
|
6
|
+
}
|
7
|
+
)
|
8
|
+
InfluxORM.configuration.connection.db.delete_database('test1')
|
9
|
+
InfluxORM.configuration.connection.db.create_database('test1')
|
10
|
+
|
11
|
+
class Book
|
12
|
+
include InfluxORM
|
13
|
+
|
14
|
+
influx_tag :book_id
|
15
|
+
influx_tag :category
|
16
|
+
influx_value :price
|
17
|
+
end
|
18
|
+
|
19
|
+
Book.insert(book_id: 1, category: 'A', price: 1.3)
|
20
|
+
Book.import([
|
21
|
+
{book_id: 1, category: 'A', price: 1.3, timestamp: 111},
|
22
|
+
{book_id: 1, category: 'A', price: 1.5, timestamp: 122},
|
23
|
+
{book_id: 2, category: 'B', price: 1.5, timestamp: 132},
|
24
|
+
{book_id: 3, category: 'C', price: 2.5, timestamp: 132}
|
25
|
+
])
|
26
|
+
|
27
|
+
puts Book.select(mean: '*').where(time: {gte: 'now() - 3d'}) \
|
28
|
+
.group_by('time(12h)', :category).fill(0).result
|
29
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'influx_orm'
|
2
|
+
|
3
|
+
C1 = InfluxORM.setup(
|
4
|
+
connection: {
|
5
|
+
database: 'test1'
|
6
|
+
}
|
7
|
+
)
|
8
|
+
C1.connection.db.delete_database('test1')
|
9
|
+
C1.connection.db.create_database('test1')
|
10
|
+
M1 = C1.module
|
11
|
+
|
12
|
+
C2 = InfluxORM.setup(
|
13
|
+
connection: {
|
14
|
+
database: 'test2'
|
15
|
+
}
|
16
|
+
)
|
17
|
+
C2.connection.db.delete_database('test2')
|
18
|
+
C2.connection.db.create_database('test2')
|
19
|
+
M2 = C2.module
|
20
|
+
|
21
|
+
class Book
|
22
|
+
include M1
|
23
|
+
|
24
|
+
influx_tag :book_id
|
25
|
+
influx_tag :category
|
26
|
+
influx_value :price, :float
|
27
|
+
end
|
28
|
+
|
29
|
+
class Host
|
30
|
+
include M2
|
31
|
+
|
32
|
+
influx_tag :ip
|
33
|
+
influx_tag :name
|
34
|
+
influx_value :load, :float
|
35
|
+
end
|
36
|
+
|
37
|
+
Book.insert(book_id: 1, category: 'A', price: 1.3)
|
38
|
+
Host.import([
|
39
|
+
{ip: '192.168.1.1', name: 'A', load: 1.3, timestamp: 111},
|
40
|
+
{ip: '192.168.1.2', name: 'A', load: 1.5, timestamp: 122},
|
41
|
+
{ip: '192.168.1.3', name: 'B', load: 1.5, timestamp: 132},
|
42
|
+
{ip: '192.168.1.1', name: 'C', load: 2.5, timestamp: 132}
|
43
|
+
])
|
44
|
+
|
45
|
+
puts '----- Book result --------'
|
46
|
+
puts Book.select(mean: '*').where(time: {gte: 'now() - 3m'}) \
|
47
|
+
.group_by('time(1m)', :category).fill(0).result
|
48
|
+
|
49
|
+
puts '----- Host result --------'
|
50
|
+
puts Host.select(mean: '*').where(time: {gte: 'now() - 30m'}) \
|
51
|
+
.group_by('time(10m)', :category).fill(0).result
|
52
|
+
|
data/influx_orm.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'influx_orm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "influx_orm"
|
8
|
+
spec.version = InfluxORM::VERSION
|
9
|
+
spec.authors = ["jiangzhi.xie"]
|
10
|
+
spec.email = ["xiejiangzhi@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple InfluxDB ORM}
|
13
|
+
spec.description = %q{A simple InfluxDB ORM}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "influxdb", "~> 0.3.14"
|
25
|
+
spec.add_dependency "activesupport", ">= 3.0"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
|
31
|
+
spec.add_development_dependency "pry"
|
32
|
+
end
|
33
|
+
|
data/lib/influx_orm/:noh
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module InfluxORM
|
2
|
+
class Connection
|
3
|
+
attr_reader :config, :database, :client_config, :configuration
|
4
|
+
|
5
|
+
def initialize(options, configuration)
|
6
|
+
@config = options.with_indifferent_access
|
7
|
+
@configuration = configuration
|
8
|
+
end
|
9
|
+
|
10
|
+
def db
|
11
|
+
@db ||= InfluxDB::Client.new(config)
|
12
|
+
end
|
13
|
+
|
14
|
+
def query(sql)
|
15
|
+
log(sql) { db.query(sql) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def insert(table_name, point)
|
19
|
+
log("INSERT to #{table_name}: #{point.to_sql}") { db.write_point(table_name, point) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def import(data)
|
23
|
+
db.write_points(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def log(log, &block)
|
29
|
+
t = Time.now
|
30
|
+
block.call
|
31
|
+
ensure
|
32
|
+
c = (Time.now - t) * 1000
|
33
|
+
configuration.logger.info("[InfluxORM] (%.3f ms) %s" % [c, log])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module InfluxORM::Attributes
|
2
|
+
ATTR_TYPES = %w{int float string boolean}.map(&:to_sym)
|
3
|
+
|
4
|
+
def self.included(cls)
|
5
|
+
cls.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def influx_attrs
|
10
|
+
@influx_attrs ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def influx_tag(name)
|
14
|
+
influx_attrs[name.to_sym] = [:tags, :string]
|
15
|
+
end
|
16
|
+
|
17
|
+
def influx_value(name, type = :int)
|
18
|
+
raise InfluxORM::Error.new("Invalid type '#{type}'") unless ATTR_TYPES.include?(type)
|
19
|
+
influx_attrs[name.to_sym] = [:values, type]
|
20
|
+
end
|
21
|
+
|
22
|
+
def attrs_to_point(hash)
|
23
|
+
point = {tags: {}, values: {}}
|
24
|
+
|
25
|
+
hash.each do |k, v|
|
26
|
+
next if k == :timestamp
|
27
|
+
|
28
|
+
if k.to_sym == :time
|
29
|
+
point[:timestamp] = format_timestamp(v)
|
30
|
+
next
|
31
|
+
end
|
32
|
+
|
33
|
+
col_type, data_type = influx_attrs[k.to_sym]
|
34
|
+
raise InfluxORM::Error.new("Invalid col_type '#{col_type}' of '#{k}'") unless col_type
|
35
|
+
point[col_type][k] = convert_val(data_type, v)
|
36
|
+
end
|
37
|
+
|
38
|
+
point[:timestamp] ||= format_timestamp(Time.now)
|
39
|
+
point
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def convert_val(data_type, val)
|
46
|
+
case data_type.to_sym
|
47
|
+
when :int then val.to_i
|
48
|
+
when :float then val.to_f
|
49
|
+
when :string then val.to_s
|
50
|
+
when :boolean then val ? true : false
|
51
|
+
else
|
52
|
+
raise InfluxORM::Error.new("Invalid data_type '#{data_type}'")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def format_timestamp(ts)
|
57
|
+
case ts
|
58
|
+
when Time, DateTime then ts.to_i
|
59
|
+
when Date then ts.to_time.to_i
|
60
|
+
when Numeric then ts.to_i
|
61
|
+
else
|
62
|
+
raise InfluxORM::Error.new("Invalid timestamp value: '#{ts.inspect}'")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module InfluxORM
|
4
|
+
class Configuration
|
5
|
+
attr_reader :options
|
6
|
+
attr_accessor :logger
|
7
|
+
|
8
|
+
def initialize(options)
|
9
|
+
@options = options.deep_symbolize_keys
|
10
|
+
end
|
11
|
+
|
12
|
+
def connection
|
13
|
+
@connection ||= Connection.new(@options[:connection], self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def module
|
17
|
+
@module ||= InitModuleGenerator.new(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
def logger
|
21
|
+
@logger ||= Logger.new(STDOUT)
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def new_module
|
28
|
+
m = self
|
29
|
+
Module.new do
|
30
|
+
@configuration = m
|
31
|
+
|
32
|
+
def self.configuration
|
33
|
+
@configuration
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.included(cls)
|
37
|
+
include InfluxORM::ModuleHelper
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module InfluxORM
|
2
|
+
class Connection
|
3
|
+
attr_reader :config, :database, :client_config, :configuration
|
4
|
+
|
5
|
+
def initialize(options, configuration)
|
6
|
+
@config = options.with_indifferent_access
|
7
|
+
@configuration = configuration
|
8
|
+
end
|
9
|
+
|
10
|
+
def db
|
11
|
+
@db ||= InfluxDB::Client.new(config)
|
12
|
+
end
|
13
|
+
|
14
|
+
def query(sql)
|
15
|
+
log(sql) { db.query(sql) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def insert(table_name, point)
|
19
|
+
log("INSERT to #{table_name}: #{point}") { db.write_point(table_name, point) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def import(data)
|
23
|
+
log("IMPORT #{data}") { db.write_points(data) }
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def log(log, &block)
|
29
|
+
t = Time.now
|
30
|
+
block.call
|
31
|
+
ensure
|
32
|
+
c = (Time.now - t) * 1000
|
33
|
+
configuration.logger.info("[InfluxORM] (%.3f ms) %s" % [c, log])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module InfluxORM
|
2
|
+
module InitModuleGenerator
|
3
|
+
def self.new(configuration)
|
4
|
+
Module.new do
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
extend ModuleClassMethods
|
7
|
+
|
8
|
+
@configuration = configuration
|
9
|
+
|
10
|
+
included do |cls|
|
11
|
+
@configuration = configuration
|
12
|
+
extend ORMClassMethods
|
13
|
+
|
14
|
+
include Model
|
15
|
+
include Attributes
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ORMClassMethods
|
21
|
+
def configuration
|
22
|
+
@configuration
|
23
|
+
end
|
24
|
+
|
25
|
+
def connection
|
26
|
+
configuration.connection
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module ModuleClassMethods
|
31
|
+
def configuration
|
32
|
+
@configuration
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# InfluxORM::Model
|
2
|
+
#
|
3
|
+
# dependent InfluxORM::Query
|
4
|
+
# dependent class method `attrs_to_point` `connection`
|
5
|
+
#
|
6
|
+
module InfluxORM::Model
|
7
|
+
def self.included(cls)
|
8
|
+
cls.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def table_name
|
13
|
+
@table_name ||= name.gsub('::', '_').tableize
|
14
|
+
end
|
15
|
+
|
16
|
+
%w{
|
17
|
+
count select where
|
18
|
+
group_by fill order_by
|
19
|
+
limit slimit offset soffset
|
20
|
+
}.each do |mname|
|
21
|
+
define_method mname do |*args|
|
22
|
+
query.send(mname, *args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def insert(point_attrs)
|
27
|
+
connection.insert(table_name, attrs_to_point(point_attrs))
|
28
|
+
end
|
29
|
+
|
30
|
+
# dependent class method: attrs_to_point
|
31
|
+
def import(points_attrs)
|
32
|
+
points = points_attrs.map do |point_attrs|
|
33
|
+
attrs_to_point(point_attrs).merge!({series: table_name})
|
34
|
+
end
|
35
|
+
connection.import(points)
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def query
|
42
|
+
InfluxORM::Query.new(self)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,165 @@
|
|
1
|
+
module InfluxORM
|
2
|
+
class Query
|
3
|
+
attr_reader :model
|
4
|
+
|
5
|
+
def initialize(model)
|
6
|
+
@model = model
|
7
|
+
|
8
|
+
@select = "*"
|
9
|
+
@where_conds = []
|
10
|
+
@or_conds = []
|
11
|
+
@group = []
|
12
|
+
@fill = nil
|
13
|
+
@order = nil
|
14
|
+
@limit = nil
|
15
|
+
@slimit = nil
|
16
|
+
@offset = nil
|
17
|
+
@soffset = nil
|
18
|
+
|
19
|
+
@result = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def count
|
23
|
+
r = select("count(*)").result
|
24
|
+
return 0 if r.empty?
|
25
|
+
row = r.first['values'].first
|
26
|
+
row[row.except('time').keys.first]
|
27
|
+
end
|
28
|
+
|
29
|
+
def select(s)
|
30
|
+
@select = s
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def where(conds = {})
|
35
|
+
@where_conds << conds if conds.present?
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def or(conds = {})
|
40
|
+
@or_conds << conds if conds.present?
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def group_by(*group)
|
45
|
+
@group = group
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def fill(val)
|
50
|
+
@fill = val
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def order_by(order)
|
55
|
+
@order = order
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def limit(n)
|
60
|
+
@limit = n
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def slimit(n)
|
65
|
+
@slimit = n
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def offset(n)
|
70
|
+
@offset = n
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def soffset(n)
|
75
|
+
@soffset = n
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_sql
|
80
|
+
sql = "SELECT #{select_to_s} FROM #{model.table_name}"
|
81
|
+
if @where_conds.present?
|
82
|
+
sql += " WHERE #{format_conds(@where_conds, :and)}"
|
83
|
+
sql += " OR #{format_conds(@or_conds, :or)}" if @or_conds.present?
|
84
|
+
elsif @or_conds.present?
|
85
|
+
sql += " WHERE #{format_conds(@or_conds, :or)}"
|
86
|
+
end
|
87
|
+
sql += " GROUP BY #{@group.join(', ')}" if @group.present?
|
88
|
+
sql += " fill(#{@fill})" if @fill
|
89
|
+
sql += " ORDER BY #{order_to_s}" if @order
|
90
|
+
sql += " LIMIT #{@limit}" if @limit
|
91
|
+
sql += " SLIMIT #{@slimit}" if @slimit
|
92
|
+
sql += " OFFSET #{@offset}" if @offset
|
93
|
+
sql += " SOFFSET #{@soffset}" if @soffset
|
94
|
+
sql
|
95
|
+
end
|
96
|
+
|
97
|
+
def result
|
98
|
+
@result ||= model.connection.query(to_sql)
|
99
|
+
end
|
100
|
+
|
101
|
+
def reload
|
102
|
+
@result = nil
|
103
|
+
result
|
104
|
+
end
|
105
|
+
|
106
|
+
# conds: [{col_name: 'val'}, 'col_name = 1 AND c2 = 2']
|
107
|
+
# relation: :and :or
|
108
|
+
#
|
109
|
+
def format_conds(conds, relation)
|
110
|
+
conds_strs = conds.map do |sub_cond|
|
111
|
+
next sub_cond if sub_cond.is_a?(String)
|
112
|
+
|
113
|
+
sub_cond.map do |k, v|
|
114
|
+
if v.is_a?(Hash)
|
115
|
+
compare_cond_to_sql(k, v)
|
116
|
+
else
|
117
|
+
case v
|
118
|
+
when Numeric, true, false then "#{k} = #{v}"
|
119
|
+
else "#{k} = '#{v}'"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end.join(' AND ')
|
123
|
+
end
|
124
|
+
|
125
|
+
relation_str = case relation.to_sym
|
126
|
+
when :and then ' AND '
|
127
|
+
when :or then ' OR '
|
128
|
+
else
|
129
|
+
raise InfluxORM::Error.new("Invalid relation value '#{relation}'")
|
130
|
+
end
|
131
|
+
|
132
|
+
conds_strs.map {|str| "(#{str})" }.join(relation_str)
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def select_to_s
|
140
|
+
return @select if @select.is_a?(String)
|
141
|
+
@select.map { |k, v| "#{k}(#{v})" }.join(', ')
|
142
|
+
end
|
143
|
+
|
144
|
+
def compare_cond_to_sql(name, hash)
|
145
|
+
hash.map do |k, v|
|
146
|
+
case k.to_sym
|
147
|
+
when :gt then "#{name} > #{v}"
|
148
|
+
when :gte then "#{name} >= #{v}"
|
149
|
+
when :lt then "#{name} < #{v}"
|
150
|
+
when :lte then "#{name} <= #{v}"
|
151
|
+
else
|
152
|
+
raise "Invalid compare '#{k}'"
|
153
|
+
end
|
154
|
+
end.join(' AND ')
|
155
|
+
end
|
156
|
+
|
157
|
+
def order_to_s
|
158
|
+
return @order if @order.is_a?(String)
|
159
|
+
@order.map do |k, v|
|
160
|
+
"#{k} #{v}"
|
161
|
+
end.join(', ')
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
data/lib/influx_orm.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "influx_orm/version"
|
2
|
+
|
3
|
+
require 'influxdb'
|
4
|
+
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_support/core_ext'
|
7
|
+
|
8
|
+
module InfluxORM
|
9
|
+
extend ActiveSupport::Autoload
|
10
|
+
|
11
|
+
autoload :Model
|
12
|
+
autoload :Query
|
13
|
+
autoload :Connection
|
14
|
+
autoload :Attributes
|
15
|
+
autoload :Configuration
|
16
|
+
autoload :InitModuleGenerator
|
17
|
+
|
18
|
+
autoload :Error
|
19
|
+
|
20
|
+
|
21
|
+
class << self
|
22
|
+
attr_reader :configuration
|
23
|
+
|
24
|
+
def setup(options)
|
25
|
+
@configuration = Configuration.new(options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def included(cls)
|
29
|
+
raise Error.new("Please setup with 'InfluxORM.setup' before include") unless configuration
|
30
|
+
cls.include(configuration.module)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
InfluxOrm = InfluxORM
|
36
|
+
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: influx_orm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jiangzhi.xie
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: influxdb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.14
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.14
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: A simple InfluxDB ORM
|
98
|
+
email:
|
99
|
+
- xiejiangzhi@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- examples/model.rb
|
114
|
+
- examples/multiple_config.rb
|
115
|
+
- influx_orm.gemspec
|
116
|
+
- lib/influx_orm.rb
|
117
|
+
- lib/influx_orm/:noh
|
118
|
+
- lib/influx_orm/attributes.rb
|
119
|
+
- lib/influx_orm/configuration.rb
|
120
|
+
- lib/influx_orm/connection.rb
|
121
|
+
- lib/influx_orm/error.rb
|
122
|
+
- lib/influx_orm/init_module_generator.rb
|
123
|
+
- lib/influx_orm/model.rb
|
124
|
+
- lib/influx_orm/query.rb
|
125
|
+
- lib/influx_orm/version.rb
|
126
|
+
homepage: ''
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.6.11
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: A simple InfluxDB ORM
|
150
|
+
test_files: []
|