quandl_cassandra_models 0.3.11 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69f5626f4f3ae37d62c6c4eb1eb005b2f9e6f385
4
- data.tar.gz: 642f964098fbe5ee0b5482631ab0eff51f7bb612
3
+ metadata.gz: 19c6f57810b76a53543512153c5b92509b37067e
4
+ data.tar.gz: ba82d7c28e4e048f1abd221765d779a5d981fe4a
5
5
  SHA512:
6
- metadata.gz: 09cddb90fbc340b9cd008ae4c79303e92e70bd32ad1e150dc158507f943e73877de14faa9d8f171440de58a3d76bce7f0b99118f35c745c5caa821d1ddbeea18
7
- data.tar.gz: 17b32587b0e906d50439d73a79a3bba6db4af34edea62fbc3d36f585a0edf43d10baa64448526d13439adea088857d3acd4400d8405b6c06dae71d82996c02b8
6
+ metadata.gz: 9d39a799aeb081836cd8debf0e0c033fc2f06ee9b9433f0c7e9ced7307a06ec67a30d3490e7e27172486e8048a15effe8119f37a4b505eff7e7755afb3c17d84
7
+ data.tar.gz: b7417e67ee39584d3d34fc8b14e6524929827ef28358950f0d0692c44ae136871399299faed4168bda9ff08378977524360cb7c7141080d7166b4d25cdd5b72b
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source "https://rubygems.org"
2
2
  gemspec
3
3
 
4
- gem 'cassandra_migrations','~> 0.3', git: 'git@github.com:blakehilscher/cassandra_migrations.git'
4
+ gem 'cassandra_migrations','~> 0.3', git: 'git@github.com:blakehilscher/cassandra_migrations.git'
5
+
6
+ if ENV['BUNDLE_LOCAL_GEMS'] == "true" && ENV['BUNDLE_LOCAL_DIR']
7
+ gem 'quandl_cassandra', path: "#{ENV['BUNDLE_LOCAL_DIR']}/quandl/cassandra"
8
+ end
data/UPGRADE.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.3.12
2
+
3
+ * refactor read to use define_attributes for trim_start, trim_end, collapse WIKI-28 passes
4
+ * add trim_spec, type_spec
5
+
6
+
1
7
  ## 0.3.11
2
8
 
3
9
  * stub Quandl::Fabricate::Data.rand to return spec/fixtures/data.csv
@@ -11,7 +11,7 @@ class Quandl::Cassandra::Models::Column::Read::Data < Quandl::Cassandra::Models:
11
11
  data.column_ids = column_ids unless attributes[:column].present?
12
12
  data.column_frequencies = attributes[:column_frequencies] unless attributes[:column].present?
13
13
  # post process
14
- data = collapse(data)
14
+ data = data.collapse( collapse ) if collapse?
15
15
  data = transform(data)
16
16
  data = trim(data)
17
17
  data = sort( data )
@@ -32,11 +32,6 @@ class Quandl::Cassandra::Models::Column::Read::Data < Quandl::Cassandra::Models:
32
32
  data = data.sort_order( attributes[:order] )
33
33
  end
34
34
 
35
- def collapse(data)
36
- data = data.collapse( attributes[:collapse] ) if attributes[:collapse]
37
- data
38
- end
39
-
40
35
  def transform(data)
41
36
  data = data.transform( attributes[:transform] ) if attributes[:transform]
42
37
  data
@@ -0,0 +1,9 @@
1
+ class Quandl::Cassandra::Models::Column::Read::Limit < Quandl::Cassandra::Models::Column::Read
2
+
3
+ def perform
4
+ return unless attributes[:limit].is_a?(Integer)
5
+ # given limit, it must be greater than or equal to 1
6
+ attributes[:limit] = 1 if attributes[:limit] <= 0
7
+ end
8
+
9
+ end
@@ -9,15 +9,15 @@ class Quandl::Cassandra::Models::Column::Read::Offset < Quandl::Cassandra::Model
9
9
  end
10
10
 
11
11
  def required_attributes_present?
12
- column_ids.present? && trim_start.is_a?(Date) && trim_end.is_a?(Date)
12
+ column_ids.present? && column_collapses.present? && columns_trim_start.is_a?(Date) && columns_trim_end.is_a?(Date)
13
13
  end
14
14
 
15
15
  def apply_offset_with_transform
16
16
  # rdiff_from needs the data from the current to offset
17
17
  if attributes[:row].present? && attributes[:transform] == :rdiff_from
18
18
  # limit the results by trim
19
- attributes[:trim_start] = trim_start.occurrences_of_frequency_ago( attributes[:row], collapse_with_frequency ).end_of_frequency(collapse_with_frequency).jd if order == :desc
20
- attributes[:trim_end] = trim_end.occurrences_of_frequency_ahead( attributes[:row], collapse_with_frequency ).end_of_frequency(collapse_with_frequency).jd if order == :asc
19
+ self.trim_start = columns_trim_start.occurrences_of_frequency_ago( attributes[:row], collapse_with_frequency ).end_of_frequency(collapse_with_frequency) if order == :desc
20
+ self.trim_end = columns_trim_end.occurrences_of_frequency_ahead( attributes[:row], collapse_with_frequency ).end_of_frequency(collapse_with_frequency) if order == :asc
21
21
  # the query should not limit or offset the data
22
22
  attributes[:limit] = nil
23
23
  attributes[:offset] = nil
@@ -31,27 +31,27 @@ class Quandl::Cassandra::Models::Column::Read::Offset < Quandl::Cassandra::Model
31
31
 
32
32
  def apply_offset_asc
33
33
  # calculate ranges
34
- offset_start = trim_end.occurrences_of_frequency_ahead( offset, collapse_with_frequency ).start_of_frequency(collapse_with_frequency)
34
+ offset_start = columns_trim_end.occurrences_of_frequency_ahead( offset, collapse_with_frequency ).start_of_frequency(collapse_with_frequency)
35
35
  offset_end = offset_start.occurrences_of_frequency_ahead( accuracy_with_limit, collapse_with_frequency ).end_of_frequency( collapse_with_frequency ) if limit
36
36
  # set trims
37
- attributes[:trim_start] = offset_start.jd
38
- attributes[:trim_end] = offset_end.jd if limit
37
+ self.trim_start = offset_start
38
+ self.trim_end = offset_end if limit
39
39
  end
40
40
 
41
41
  def apply_offset_desc
42
42
  # calculate ranges
43
- offset_start = trim_start.occurrences_of_frequency_ago( offset, collapse_with_frequency ).end_of_frequency(collapse_with_frequency)
43
+ offset_start = columns_trim_start.occurrences_of_frequency_ago( offset, collapse_with_frequency ).end_of_frequency(collapse_with_frequency)
44
44
  offset_end = offset_start.occurrences_of_frequency_ago( accuracy_with_limit, collapse_with_frequency ).start_of_frequency( collapse_with_frequency ) if limit
45
45
  # set trims
46
- attributes[:trim_start] = offset_end.jd if limit
47
- attributes[:trim_end] = offset_start.jd
46
+ self.trim_start = offset_end if limit
47
+ self.trim_end = offset_start
48
48
  end
49
49
 
50
- def trim_start
50
+ def columns_trim_start
51
51
  @trim_start ||= Quandl::Cassandra::Column.find_max_time_by_ids(column_ids, column_collapses, "DESC")
52
52
  end
53
53
 
54
- def trim_end
54
+ def columns_trim_end
55
55
  @trim_end ||= Quandl::Cassandra::Column.find_max_time_by_ids(column_ids, column_collapses, "ASC")
56
56
  end
57
57
 
@@ -66,7 +66,7 @@ class Quandl::Cassandra::Models::Column::Read::Offset < Quandl::Cassandra::Model
66
66
  accuracy = attributes[:accuracy]
67
67
  # otherwise guess at the accuracy by collapse
68
68
  if accuracy.blank?
69
- accuracy = case frequency
69
+ accuracy = case frequency.to_sym
70
70
  when :daily then 0
71
71
  when :weekly then 1
72
72
  when :monthly then 1
@@ -99,8 +99,4 @@ class Quandl::Cassandra::Models::Column::Read::Offset < Quandl::Cassandra::Model
99
99
  attributes[:frequency]
100
100
  end
101
101
 
102
- def collapse
103
- attributes[:collapse]
104
- end
105
-
106
102
  end
@@ -48,8 +48,8 @@ class Quandl::Cassandra::Models::Column::Read::SelectColumns < Quandl::Cassandra
48
48
  columns = count? ? "COUNT(*)" : "time,value"
49
49
  cql = "SELECT #{columns} FROM columns WHERE"
50
50
  # cql += "ORDER"
51
- cql += " time >= #{attributes[:trim_start]} AND " if attributes[:trim_start]
52
- cql += " time <= #{attributes[:trim_end]} AND " if attributes[:trim_end]
51
+ cql += " time >= #{trim_start.jd} AND " if trim_start?
52
+ cql += " time <= #{trim_end.jd} AND " if trim_end?
53
53
  cql += " id = ? AND type = ?"
54
54
  cql += " ORDER BY type #{order}"
55
55
  cql += " LIMIT #{attributes[:limit]}" if attributes[:limit]
@@ -23,8 +23,8 @@ class Quandl::Cassandra::Models::Column::Read::Transform < Quandl::Cassandra::Mo
23
23
  # limit requires limit
24
24
  attributes[:limit] = attributes[:limit] + 1 if attributes[:limit]
25
25
  # trims should be increased by one
26
- attributes[:trim_start] = trim_start.occurrences_of_frequency_ago( 1, attributes[:collapse] ).jd if trim_start.present? && order == :desc
27
- attributes[:trim_end] = trim_end.occurrences_of_frequency_ahead( 1, attributes[:collapse] ).jd if trim_end.present? && order == :asc
26
+ self.trim_start = trim_start.occurrences_of_frequency_ago( 1, collapse ) if trim_start? && order == :desc
27
+ self.trim_end = trim_end.occurrences_of_frequency_ahead( 1, collapse ) if trim_end? && order == :asc
28
28
  end
29
29
 
30
30
  def transform?(*keys)
@@ -34,14 +34,6 @@ class Quandl::Cassandra::Models::Column::Read::Transform < Quandl::Cassandra::Mo
34
34
  false
35
35
  end
36
36
 
37
- def trim_end
38
- @trim_end ||= attributes[:trim_end].present? ? Date.jd( attributes[:trim_end] ) : nil
39
- end
40
-
41
- def trim_start
42
- @trim_start ||= attributes[:trim_start].present? ? Date.jd( attributes[:trim_start] ) : nil
43
- end
44
-
45
37
  def limit
46
38
  attributes[:limit]
47
39
  end
@@ -1,14 +1,22 @@
1
1
  class Quandl::Cassandra::Models::Column::Read::Trim < Quandl::Cassandra::Models::Column::Read
2
2
 
3
3
  def perform
4
- # if both are provided
5
- if attributes[:trim_end].is_a?(Integer) && attributes[:trim_start].is_a?(Integer)
6
- # if trim_start exceeds trim_end
7
- if attributes[:trim_start] > attributes[:trim_end]
8
- # then set trim_end to trim_start, since they are requesting nothing
9
- attributes[:trim_end] = attributes[:trim_start]
10
- end
11
- end
4
+ trim_start_should_begin_at_start_of_frequency if trim_start? && collapse?
5
+ trim_end_should_finish_at_end_of_frequency if trim_end? && collapse?
6
+ trim_start_should_not_exceed_trim_end if trim_start? && trim_end?
7
+ end
8
+
9
+ def trim_start_should_begin_at_start_of_frequency
10
+ self.trim_start = trim_start.start_of_frequency(collapse)
11
+ end
12
+
13
+ def trim_end_should_finish_at_end_of_frequency
14
+ self.trim_end = trim_end.end_of_frequency(collapse)
15
+ end
16
+
17
+ def trim_start_should_not_exceed_trim_end
18
+ # must not exceed trim_start
19
+ self.trim_end = trim_start if trim_start > trim_end
12
20
  end
13
21
 
14
22
  end
@@ -11,15 +11,35 @@ class Quandl::Cassandra::Models::Column::Read::Type < Quandl::Cassandra::Models:
11
11
 
12
12
  def enforce_type(key, value)
13
13
  case key
14
- when :limit, :column, :trim_start, :trim_end, :offset, :accuracy, :row
15
- return value.try(:to_i)
16
- when :collapse, :transform, :frequency
17
- return value.try(:to_sym)
18
- when :order
19
- return value.try(:to_sym) == :asc ? :asc : :desc
14
+ when *date_types then parse_date(value)
15
+ when *integer_types then value.try(:to_i)
16
+ when *sym_types then value.try(:to_sym)
17
+ when :order then value.try(:to_sym) == :asc ? :asc : :desc
20
18
  else
21
19
  value
22
20
  end
23
21
  end
24
22
 
23
+ def sym_types
24
+ [:collapse, :transform, :frequency]
25
+ end
26
+
27
+ def date_types
28
+ [:trim_start, :trim_end]
29
+ end
30
+
31
+ def integer_types
32
+ [:limit, :column, :offset, :accuracy, :row]
33
+ end
34
+
35
+ def parse_date(value)
36
+ date = Date.jd(value.to_i) if value.kind_of?(String) && value.numeric?
37
+ date = Date.jd(value) if value.is_a?(Integer)
38
+ date = Date.parse(value) if value.is_a?(String) && value =~ /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/
39
+ date = value if value.is_a?(Date)
40
+ date
41
+ rescue
42
+ nil
43
+ end
44
+
25
45
  end
@@ -9,12 +9,14 @@ class Quandl::Cassandra::Models::Column::Read < Quandl::Strategy::Strategize
9
9
  require_relative 'read/transform'
10
10
  require_relative 'read/trim'
11
11
  require_relative 'read/type'
12
+ require_relative 'read/limit'
12
13
 
13
- define_attributes :id
14
+ define_attributes :id, :trim_start, :trim_end, :collapse
14
15
 
15
16
  def self.perform(attributes)
16
17
  strategy = Quandl::Strategy.new( attributes ) do |c|
17
18
  c.use Quandl::Cassandra::Models::Column::Read::Type
19
+ c.use Quandl::Cassandra::Models::Column::Read::Limit
18
20
  c.use Quandl::Cassandra::Models::Column::Read::Row
19
21
  c.use Quandl::Cassandra::Models::Column::Read::Column
20
22
  c.use Quandl::Cassandra::Models::Column::Read::Collapse
@@ -26,7 +28,7 @@ class Quandl::Cassandra::Models::Column::Read < Quandl::Strategy::Strategize
26
28
  end
27
29
  strategy.perform
28
30
  rescue => e
29
- Quandl::Logger.error("#{e} #{strategy.attributes}")
31
+ Quandl::Logger.error("#{e} #{attributes}")
30
32
  raise
31
33
  end
32
34
 
@@ -1,7 +1,7 @@
1
1
  module Quandl
2
2
  module Cassandra
3
3
  module Models
4
- VERSION = '0.3.11'
4
+ VERSION = '0.3.12'
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Cassandra::Models::Column::Read::Trim do
5
+ # column_ids.present? && trim_start.is_a?(Date) && trim_end.is_a?(Date)
6
+
7
+ let(:trim_start){ Date.parse("2010-02-13") }
8
+ let(:trim_end){ Date.parse("2013-10-21") }
9
+ let(:collapse){ 'daily' }
10
+
11
+ let(:strategy){ Quandl::Cassandra::Models::Column::Read::Trim.new(
12
+ trim_start: trim_start,
13
+ trim_end: trim_end,
14
+ collapse: collapse,
15
+ )}
16
+
17
+ [:daily, :weekly, :monthly, :quarterly, :annual].each do |collapse|
18
+ context collapse do
19
+ let(:collapse){ collapse }
20
+
21
+ describe "#attributes" do
22
+ subject{ OpenStruct.new(strategy.attributes) }
23
+ its(:trim_start){ should eq Date.parse("2010-02-13").start_of_frequency(collapse) }
24
+ its(:trim_end){ should eq Date.parse("2013-10-21").end_of_frequency(collapse) }
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Cassandra::Models::Column::Read::Type do
5
+
6
+ let(:strategy){
7
+ Quandl::Cassandra::Models::Column::Read::Type.new({
8
+ collapse: 'monthly',
9
+ transform: 'rdiff',
10
+ frequency: 'daily',
11
+ trim_start: Date.today,
12
+ trim_end: Date.today.jd.to_s,
13
+ limit: '10',
14
+ row: '2',
15
+ })
16
+ }
17
+
18
+ describe "#attributes" do
19
+ subject{ OpenStruct.new(strategy.attributes) }
20
+ its(:collapse){ should eq :monthly }
21
+ its(:transform){ should eq :rdiff }
22
+ its(:frequency){ should eq :daily }
23
+ its(:trim_start){ should eq Date.today }
24
+ its(:trim_end){ should eq Date.today }
25
+ its(:limit){ should eq 10 }
26
+ its(:row){ should eq 2 }
27
+
28
+ [ Date.today, Date.today.jd.to_s, Date.today.to_s, Date.today.jd ].each do |date_type|
29
+ context "#trim_start #{date_type.class}" do
30
+ let(:strategy){ Quandl::Cassandra::Models::Column::Read::Type.new({ trim_start: date_type }) }
31
+ its(:trim_start){ should eq Date.today }
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_cassandra_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.11
4
+ version: 0.3.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Hilscher
@@ -145,6 +145,7 @@ files:
145
145
  - lib/quandl/cassandra/models/column/read/collapse.rb
146
146
  - lib/quandl/cassandra/models/column/read/column.rb
147
147
  - lib/quandl/cassandra/models/column/read/data.rb
148
+ - lib/quandl/cassandra/models/column/read/limit.rb
148
149
  - lib/quandl/cassandra/models/column/read/offset.rb
149
150
  - lib/quandl/cassandra/models/column/read/row.rb
150
151
  - lib/quandl/cassandra/models/column/read/select_columns.rb
@@ -173,6 +174,8 @@ files:
173
174
  - spec/expectations/time.rb
174
175
  - spec/factories/dataset.rb
175
176
  - spec/fixtures/data.csv
177
+ - spec/lib/quandl/cassandra/models/column/read/trim_spec.rb
178
+ - spec/lib/quandl/cassandra/models/column/read/type_spec.rb
176
179
  - spec/lib/quandl/cassandra/models/column/read_spec.rb
177
180
  - spec/lib/quandl/cassandra/models/column/write/group_data_by_frequency_spec.rb
178
181
  - spec/lib/quandl/cassandra/models/column/write_spec.rb
@@ -223,6 +226,8 @@ test_files:
223
226
  - spec/expectations/time.rb
224
227
  - spec/factories/dataset.rb
225
228
  - spec/fixtures/data.csv
229
+ - spec/lib/quandl/cassandra/models/column/read/trim_spec.rb
230
+ - spec/lib/quandl/cassandra/models/column/read/type_spec.rb
226
231
  - spec/lib/quandl/cassandra/models/column/read_spec.rb
227
232
  - spec/lib/quandl/cassandra/models/column/write/group_data_by_frequency_spec.rb
228
233
  - spec/lib/quandl/cassandra/models/column/write_spec.rb