Skip to content

Commit

Permalink
working on 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Dec 26, 2014
1 parent 0db0695 commit 3a0cb61
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 0.2.0
- Relation scopes
- Support for fanout series

## 0.1.0
- Add `time` method to Relation to group by time with constants (`:hour`, `:day`, etc) and fill support
- Series names now properly quoted with double-quotes
- [TODO] Using regexp within `where` clause
- [TODO] `where.not(...)` support
- [TODO] Support for `where(id: [...])` (for now arrays are exploding to `where ((...) or (...) or ...)`)
2 changes: 1 addition & 1 deletion MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2014 YOURNAME
Copyright 2014 palkan

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
1 change: 1 addition & 0 deletions lib/influxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Influxer
require 'influxer/config'
require 'influxer/client'
require 'influxer/metrics/metrics'
require 'influxer/metrics/relation/time_group'
require 'influxer/metrics/relation'

module Model
Expand Down
7 changes: 6 additions & 1 deletion lib/influxer/metrics/relation.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Influxer
class Relation

include Influxer::TimeGroup
# Initialize new Relation for 'klass' (Class) metrics.
#
# Available params:
Expand Down Expand Up @@ -73,6 +73,10 @@ def to_sql
sql << "group by #{@group_values.join(",")}"
end

unless @fill_value.nil?
sql << "fill(#{@fill_value})"
end

unless @where_values.empty?
sql << "where #{@where_values.join(" and ")}"
end
Expand Down Expand Up @@ -119,6 +123,7 @@ def reset
@select_values = []
@group_values = []
@where_values = []
@fill_value = nil
@records = nil
@loaded = false
self
Expand Down
26 changes: 26 additions & 0 deletions lib/influxer/metrics/relation/time_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Influxer
module TimeGroup
TIME_ALIASES = {
hour: '1h',
minute: '1m',
second: '1s',
ms: '1u',
week: '1w',
day: '1d',
month: '30d'
}

def time(val, options={})
if val.is_a?(Symbol)
group("time(#{ TIME_ALIASES[val] || ('1'+val.to_s) })")
else
group("time(#{val})")
end

unless options[:fill].nil?
@fill_value = (options[:fill] == :null) ? 'null' : options[:fill].to_i
end
self
end
end
end
2 changes: 1 addition & 1 deletion lib/influxer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Influxer
VERSION = "0.0.1"
VERSION = "0.1.0"
end
54 changes: 54 additions & 0 deletions spec/metrics/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,66 @@
xit "should recognize regexps as params" do
expect(rel.where(user_id: 1, dummy: /^du.*/).to_sql).to eq "select * from \"dummy\" where (user_id=1) and (dummy=~/^du.*/)"
end

xit "should handle negation" do
expect(rel.where.not(user_id: 1, dummy: /^du.*/).to_sql).to eq "select * from \"dummy\" where (user_id!=1) and (dummy!~/^du.*/)"
end

xit "should handle arrays" do
expect(rel.where(user_id: [1,2,3]).to_sql).to eq "select * from \"dummy\" where ((user_id=1) or (user_id=2) or (user_id=3))"
end
end

describe "group" do
it "should generate valid groups" do
expect(rel.group(:user_id, "time(1m) fill(0)").to_sql).to eq "select * from \"dummy\" group by user_id,time(1m) fill(0)"
end

describe "group by time predefined values" do
it "should group by hour" do
expect(rel.time(:hour).to_sql).to eq "select * from \"dummy\" group by time(1h)"
end

it "should group by minute" do
expect(rel.time(:minute).to_sql).to eq "select * from \"dummy\" group by time(1m)"
end

it "should group by second" do
expect(rel.time(:second).to_sql).to eq "select * from \"dummy\" group by time(1s)"
end

it "should group by millisecond" do
expect(rel.time(:ms).to_sql).to eq "select * from \"dummy\" group by time(1u)"
end

it "should group by day" do
expect(rel.time(:day).to_sql).to eq "select * from \"dummy\" group by time(1d)"
end

it "should group by week" do
expect(rel.time(:week).to_sql).to eq "select * from \"dummy\" group by time(1w)"
end

it "should group by month" do
expect(rel.time(:month).to_sql).to eq "select * from \"dummy\" group by time(30d)"
end

it "should group by hour and fill" do
expect(rel.time(:month, fill: 0).to_sql).to eq "select * from \"dummy\" group by time(30d) fill(0)"
end
end

it "should group by time with string value" do
expect(rel.time("4d").to_sql).to eq "select * from \"dummy\" group by time(4d)"
end

it "should group by time with string value and fill null" do
expect(rel.time("4d", fill: :null).to_sql).to eq "select * from \"dummy\" group by time(4d) fill(null)"
end

it "should group by time and other fields with fill null" do
expect(rel.time("4d", fill: 0).group(:dummy_id).to_sql).to eq "select * from \"dummy\" group by time(4d),dummy_id fill(0)"
end
end

describe "limit" do
Expand Down

0 comments on commit 3a0cb61

Please sign in to comment.