influxdb-arel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +350 -0
  8. data/Rakefile +6 -0
  9. data/influxdb-arel.gemspec +24 -0
  10. data/lib/influxdb-arel.rb +1 -0
  11. data/lib/influxdb.rb +4 -0
  12. data/lib/influxdb/arel.rb +38 -0
  13. data/lib/influxdb/arel/alias_predication.rb +9 -0
  14. data/lib/influxdb/arel/attributes.rb +1 -0
  15. data/lib/influxdb/arel/attributes/attribute.rb +74 -0
  16. data/lib/influxdb/arel/core_extensions.rb +49 -0
  17. data/lib/influxdb/arel/expressions.rb +73 -0
  18. data/lib/influxdb/arel/math.rb +21 -0
  19. data/lib/influxdb/arel/nodes.rb +27 -0
  20. data/lib/influxdb/arel/nodes/and.rb +32 -0
  21. data/lib/influxdb/arel/nodes/binary.rb +47 -0
  22. data/lib/influxdb/arel/nodes/duration.rb +30 -0
  23. data/lib/influxdb/arel/nodes/equality.rb +11 -0
  24. data/lib/influxdb/arel/nodes/function.rb +47 -0
  25. data/lib/influxdb/arel/nodes/grouping.rb +11 -0
  26. data/lib/influxdb/arel/nodes/in.rb +8 -0
  27. data/lib/influxdb/arel/nodes/infix_operation.rb +51 -0
  28. data/lib/influxdb/arel/nodes/node.rb +19 -0
  29. data/lib/influxdb/arel/nodes/now.rb +15 -0
  30. data/lib/influxdb/arel/nodes/select_statement.rb +59 -0
  31. data/lib/influxdb/arel/nodes/sql_literal.rb +23 -0
  32. data/lib/influxdb/arel/nodes/table_alias.rb +23 -0
  33. data/lib/influxdb/arel/nodes/time.rb +13 -0
  34. data/lib/influxdb/arel/nodes/unary.rb +35 -0
  35. data/lib/influxdb/arel/predications.rb +137 -0
  36. data/lib/influxdb/arel/select_manager.rb +129 -0
  37. data/lib/influxdb/arel/table.rb +219 -0
  38. data/lib/influxdb/arel/tree_manager.rb +30 -0
  39. data/lib/influxdb/arel/version.rb +5 -0
  40. data/lib/influxdb/arel/visitor.rb +287 -0
  41. data/spec/lib/influxdb/arel/core_extensions_spec.rb +49 -0
  42. data/spec/lib/influxdb/arel/nodes/and_spec.rb +17 -0
  43. data/spec/lib/influxdb/arel/nodes/binary_spec.rb +49 -0
  44. data/spec/lib/influxdb/arel/nodes/duration_spec.rb +72 -0
  45. data/spec/lib/influxdb/arel/nodes/equality_spec.rb +5 -0
  46. data/spec/lib/influxdb/arel/nodes/function_spec.rb +69 -0
  47. data/spec/lib/influxdb/arel/nodes/grouping_spec.rb +10 -0
  48. data/spec/lib/influxdb/arel/nodes/in_spec.rb +13 -0
  49. data/spec/lib/influxdb/arel/nodes/now_spec.rb +8 -0
  50. data/spec/lib/influxdb/arel/nodes/sql_literal_spec.rb +28 -0
  51. data/spec/lib/influxdb/arel/nodes/table_alias_spec.rb +36 -0
  52. data/spec/lib/influxdb/arel/nodes/time_spec.rb +5 -0
  53. data/spec/lib/influxdb/arel/nodes/unary_spec.rb +25 -0
  54. data/spec/lib/influxdb/arel/select_manager_spec.rb +459 -0
  55. data/spec/lib/influxdb/arel/table_spec.rb +193 -0
  56. data/spec/lib/influxdb/arel_spec.rb +11 -0
  57. data/spec/spec_helper.rb +20 -0
  58. data/spec/support/examples/binary_node.rb +10 -0
  59. data/spec/support/examples/function_node.rb +14 -0
  60. data/spec/support/examples/node_as.rb +8 -0
  61. data/spec/support/examples/node_expressions.rb +145 -0
  62. data/spec/support/examples/node_math.rb +29 -0
  63. data/spec/support/examples/node_predications.rb +248 -0
  64. data/spec/support/examples/node_to_sql.rb +5 -0
  65. data/spec/support/examples/unary_node.rb +10 -0
  66. data/spec/support/fabrics.rb +21 -0
  67. metadata +177 -0
@@ -0,0 +1 @@
1
+ require 'influxdb'
@@ -0,0 +1,4 @@
1
+ require 'influxdb/arel'
2
+
3
+ module Influxdb
4
+ end
@@ -0,0 +1,38 @@
1
+ require "influxdb/arel/version"
2
+
3
+ require 'influxdb/arel/expressions'
4
+ require 'influxdb/arel/predications'
5
+ require 'influxdb/arel/math'
6
+ require 'influxdb/arel/alias_predication'
7
+ require 'influxdb/arel/table'
8
+ require 'influxdb/arel/attributes'
9
+
10
+ require 'influxdb/arel/visitor'
11
+
12
+ require 'influxdb/arel/tree_manager'
13
+ require 'influxdb/arel/select_manager'
14
+ # require 'influxdb/arel/delete_manager'
15
+ require 'influxdb/arel/nodes'
16
+
17
+ module Influxdb
18
+ module Arel
19
+ extend self
20
+
21
+ def sql(raw_sql)
22
+ Nodes::SqlLiteral.new(raw_sql)
23
+ end
24
+
25
+ def star
26
+ sql('*')
27
+ end
28
+
29
+ def now
30
+ Influxdb::Arel::Nodes::Now.new
31
+ end
32
+
33
+ def time(duration)
34
+ duration = sql(duration) if String === duration
35
+ Influxdb::Arel::Nodes::Time.new(duration)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ module Influxdb
2
+ module Arel
3
+ module AliasPredication
4
+ def as(other)
5
+ Nodes::As.new(self, Arel.sql(other.to_s))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'influxdb/arel/attributes/attribute'
@@ -0,0 +1,74 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Attributes
4
+ class Attribute < Struct.new(:relation, :name)
5
+ include Expressions
6
+ include Predications
7
+ include AliasPredication
8
+ include Math
9
+
10
+ def self.encode(value)
11
+ "'#{value.to_s}'"
12
+ end
13
+ end
14
+
15
+ class Time < Attribute
16
+ def self.encode(value)
17
+ value.to_i * 1_000_000
18
+ end
19
+ end
20
+
21
+ class Date < Time; end
22
+ class DateTime < Time; end
23
+
24
+ class BigDecimal < Attribute
25
+ def self.encode(value)
26
+ value.to_s('F')
27
+ end
28
+ end
29
+
30
+ class Boolean < Attribute
31
+ def self.encode(value)
32
+ value.inspect
33
+ end
34
+ end
35
+
36
+ class FalseClass < Boolean; end
37
+ class TrueClass < Boolean; end
38
+
39
+ class Float < Attribute
40
+ def self.encode(value)
41
+ value
42
+ end
43
+ end
44
+
45
+ class Integer < Attribute
46
+ def self.encode(value)
47
+ value
48
+ end
49
+ end
50
+
51
+ class Regexp < Attribute
52
+ def self.encode(value)
53
+ value.inspect
54
+ end
55
+ end
56
+
57
+ class Hash < Attribute
58
+ def self.encode(value)
59
+ value = value.to_json if value.respond_to?(:to_json)
60
+ super(value)
61
+ end
62
+ end
63
+
64
+ class NilClass < Attribute
65
+ def self.encode(value)
66
+ 'null'
67
+ end
68
+ end
69
+ end
70
+
71
+ Attribute = Attributes::Attribute
72
+ end
73
+ end
74
+
@@ -0,0 +1,49 @@
1
+ class Integer
2
+ def u
3
+ Influxdb::Arel::Nodes::Duration.new(self, 'u')
4
+ end
5
+
6
+ def s
7
+ Influxdb::Arel::Nodes::Duration.new(self, 's')
8
+ end
9
+
10
+ def m
11
+ Influxdb::Arel::Nodes::Duration.new(self, 'm')
12
+ end
13
+
14
+ def h
15
+ Influxdb::Arel::Nodes::Duration.new(self, 'h')
16
+ end
17
+
18
+ def d
19
+ Influxdb::Arel::Nodes::Duration.new(self, 'd')
20
+ end
21
+
22
+ def w
23
+ Influxdb::Arel::Nodes::Duration.new(self, 'w')
24
+ end
25
+ end
26
+
27
+ class String
28
+ def to_influxdb_arel
29
+ Influxdb::Arel::Nodes::SqlLiteral.new(self)
30
+ end
31
+
32
+ alias :to_arel :to_influxdb_arel unless method_defined?(:to_arel)
33
+
34
+ def as(other)
35
+ Influxdb::Arel::Nodes::As.new(to_influxdb_arel, other.to_influxdb_arel)
36
+ end
37
+ end
38
+
39
+ class Symbol
40
+ def to_influxdb_arel
41
+ Influxdb::Arel::Table.new(self.to_s)
42
+ end
43
+
44
+ alias :to_arel :to_influxdb_arel unless method_defined?(:to_arel)
45
+
46
+ def as(other)
47
+ to_influxdb_arel.as(other)
48
+ end
49
+ end
@@ -0,0 +1,73 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Expressions
4
+ def count
5
+ Nodes::Count.new([self])
6
+ end
7
+
8
+ def sum
9
+ Nodes::Sum.new([self])
10
+ end
11
+
12
+ def max
13
+ Nodes::Max.new([self])
14
+ end
15
+
16
+ def min
17
+ Nodes::Min.new([self])
18
+ end
19
+
20
+ def mean
21
+ Nodes::Mean.new([self])
22
+ end
23
+
24
+ def mode
25
+ Nodes::Mode.new([self])
26
+ end
27
+
28
+ def median
29
+ Nodes::Median.new([self])
30
+ end
31
+
32
+ def distinct
33
+ Nodes::Distinct.new([self])
34
+ end
35
+
36
+ def percentile(nth)
37
+ Nodes::Percentile.new([self, nth])
38
+ end
39
+
40
+ def histogram(bucket_size = nil)
41
+ Nodes::Histogram.new([self, bucket_size || 1.0])
42
+ end
43
+
44
+ def derivative
45
+ Nodes::Derivative.new([self])
46
+ end
47
+
48
+ def stddev
49
+ Nodes::Stddev.new([self])
50
+ end
51
+
52
+ def first
53
+ Nodes::First.new([self])
54
+ end
55
+
56
+ def last
57
+ Nodes::Last.new([self])
58
+ end
59
+
60
+ def difference
61
+ Nodes::Difference.new([self])
62
+ end
63
+
64
+ def top(size)
65
+ Nodes::Top.new([self, size])
66
+ end
67
+
68
+ def bottom(size)
69
+ Nodes::Bottom.new([self, size])
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,21 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Math
4
+ def *(other)
5
+ Nodes::Multiplication.new(self, other)
6
+ end
7
+
8
+ def +(other)
9
+ Nodes::Grouping.new(Nodes::Addition.new(self, other))
10
+ end
11
+
12
+ def -(other)
13
+ Nodes::Grouping.new(Nodes::Subtraction.new(self, other))
14
+ end
15
+
16
+ def /(other)
17
+ Nodes::Division.new(self, other)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'influxdb/arel/nodes/node'
2
+ require 'influxdb/arel/nodes/now'
3
+ require 'influxdb/arel/nodes/select_statement'
4
+ # require 'influxdb/arel/nodes/update_statement'
5
+
6
+ # unary
7
+ require 'influxdb/arel/nodes/unary'
8
+ require 'influxdb/arel/nodes/grouping'
9
+ require 'influxdb/arel/nodes/time'
10
+
11
+
12
+ # binary
13
+ require 'influxdb/arel/nodes/binary'
14
+ require 'influxdb/arel/nodes/duration'
15
+ require 'influxdb/arel/nodes/equality'
16
+ require 'influxdb/arel/nodes/in'
17
+ # require 'influxdb/arel/nodes/delete_statement'
18
+ require 'influxdb/arel/nodes/table_alias'
19
+ require 'influxdb/arel/nodes/infix_operation'
20
+
21
+ # nary
22
+ require 'influxdb/arel/nodes/and'
23
+
24
+
25
+ require 'influxdb/arel/nodes/function'
26
+ # require 'influxdb/arel/nodes/named_function'
27
+ require 'influxdb/arel/nodes/sql_literal'
@@ -0,0 +1,32 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class And < Node
5
+ attr_reader :children
6
+
7
+ def initialize(children)
8
+ super()
9
+ @children = children
10
+ end
11
+
12
+ def left
13
+ children.first
14
+ end
15
+
16
+ def right
17
+ children[1]
18
+ end
19
+
20
+ def hash
21
+ children.hash
22
+ end
23
+
24
+ def eql?(other)
25
+ self.class == other.class && children == other.children
26
+ end
27
+
28
+ alias :== :eql?
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Binary < Node
5
+ attr_accessor :left, :right
6
+
7
+ def initialize(left, right)
8
+ super()
9
+ self.left = left
10
+ self.right = right
11
+ end
12
+
13
+ def initialize_copy(other)
14
+ super
15
+ self.left = left.clone if left
16
+ self.right = right.clone if right
17
+ end
18
+
19
+ def hash
20
+ [left, right].hash
21
+ end
22
+
23
+ def eql?(other)
24
+ self.class == other.class && left == other.left && right == other.right
25
+ end
26
+
27
+ alias :== :eql?
28
+ end
29
+
30
+ %w{
31
+ As
32
+ DoesNotMatch
33
+ GreaterThan
34
+ GreaterThanOrEqual
35
+ Join
36
+ LessThan
37
+ LessThanOrEqual
38
+ Matches
39
+ Merge
40
+ NotEqual
41
+ Or
42
+ }.each do |name|
43
+ const_set(name, Class.new(Binary))
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,30 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Duration < Binary
5
+ POSSIBLE_SUFFIXES = %w{u s m h d w}.freeze
6
+
7
+ alias :value :left
8
+ alias :suffix :right
9
+
10
+ def initialize(value, suffix)
11
+ suffix = suffix.to_s
12
+ suffix = 'u' unless POSSIBLE_SUFFIXES.include?(suffix)
13
+ super(value.to_i, suffix)
14
+ end
15
+
16
+ def time
17
+ Arel.time(self)
18
+ end
19
+
20
+ def ago
21
+ Arel.now - self
22
+ end
23
+
24
+ def since
25
+ Arel.now + self
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Equality < Binary
5
+ def operator; :== end
6
+ alias :operand1 :left
7
+ alias :operand2 :right
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,47 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Function < Node
5
+ include AliasPredication
6
+ include Math
7
+
8
+ attr_accessor :expressions
9
+
10
+ def initialize(expr)
11
+ super()
12
+ self.expressions = expr
13
+ end
14
+
15
+ def hash
16
+ expressions.hash
17
+ end
18
+
19
+ def eql?(other)
20
+ self.class == other.class && expressions == other.expressions
21
+ end
22
+ end
23
+
24
+ %w{
25
+ Count
26
+ Sum
27
+ Max
28
+ Min
29
+ Mean
30
+ Mode
31
+ Median
32
+ Distinct
33
+ Percentile
34
+ Histogram
35
+ Derivative
36
+ Stddev
37
+ First
38
+ Last
39
+ Difference
40
+ Top
41
+ Bottom
42
+ }.each do |name|
43
+ const_set(name, Class.new(Function))
44
+ end
45
+ end
46
+ end
47
+ end