olap_report 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 900f924f26b8abfb68f0d90a2b9172827ed68ab7
4
- data.tar.gz: 48c5a6579789c1be63d89ce6cc092fb0fcd9bcb1
3
+ metadata.gz: 60b96d699169e6839734460bc330e7cd9849f60b
4
+ data.tar.gz: 584b15684ff4d8524e5cafb0ad50f30300e3fea4
5
5
  SHA512:
6
- metadata.gz: f46013fd4b1fba0fb6c067d19882650d85da979903dfdee1dfa432363cbd1c40d59d5644d33e8da3f5616a88c753de0b3ff36922303091ee4616fe1d43301db5
7
- data.tar.gz: a295c6c5d371cf0f15303c4817f04db68051afa8c667f609244d128ae6781325bf55a55ad62aed7f7922efe9cf5ee790efa95b75e69e5f741b519fe06f01ae7b
6
+ metadata.gz: 3bde07565fb5f4dd0fab4338dc356738c33c4f57a2c0da5c8e44af28a0b585dd8ff1af0bf50b8038653d47c2f5dc1156db1d2b1e4bb9fab0facfd5689a16c813
7
+ data.tar.gz: 3f8234d53eea77ccb5f932b55954b2b67f7b5e76562db9d5bc0043755a95e4754a0e13c1ac326ad25b76544bc193c91cb63aa0b9ba4ca455f7644c494d8a9115
@@ -51,7 +51,7 @@ module OlapReport
51
51
  # @param [Symbol] name
52
52
  # @return [OlapReport::Cube::Dimension]
53
53
  def dimension(name)
54
- dimensions.find{|d| d.name == name}
54
+ dimensions.find{|d| d.name == name} || raise(KeyError, "Dimension #{name} not found")
55
55
  end
56
56
 
57
57
  # Get dimension by name
@@ -32,7 +32,19 @@ module OlapReport
32
32
  # @param [Symbol] level_name
33
33
  # @return [OlapReport::Cube::Level]
34
34
  def [](level_name)
35
- levels.find{|l| l.name == level_name}
35
+ levels.find{|l| l.name == level_name} || raise(KeyError, "Level '#{level_name}' not found for dimension '#{self.name}'")
36
+ end
37
+
38
+ def level_index(level_name)
39
+ levels.map(&:name).index(level_name)
40
+ end
41
+
42
+ def prev_level(level_name)
43
+ levels[level_index(level_name) - 1]
44
+ end
45
+
46
+ def next_level(level_name)
47
+ levels[level_index(level_name) + 1]
36
48
  end
37
49
  end
38
50
  end
@@ -15,8 +15,33 @@ module OlapReport
15
15
  end
16
16
  end
17
17
 
18
+ # Fetches details for specified levels/dimensions
19
+ # @param [Hash] options - projection options
20
+ # :dimensions - dimensions to query in
21
+ # :measures - measures to calculate for specified dimensions
22
+ #
23
+ # Fact.drilldown(user: :category) # drill 1 level down (group_id)
24
+ # Fact.drilldown(user: {category: :user_id}) # drill down to specified level (user_id)
18
25
  def drilldown(options)
19
- raise "Not implemented yet"
26
+ dimensions, measures = options.values_at(:dimensions, :measures).map{|v| Array(v)}
27
+
28
+ joins, dims = {}, {}
29
+ dimensions.each do |k,v|
30
+ dim = dimension(k)
31
+
32
+ case v
33
+ when Hash
34
+ raise ArgumentError, "Level for '#{k}' dimension is invalid" if v.size != 1
35
+ lvl, details_lvl = *v.first
36
+ dims[k] = details_lvl
37
+ joins.merge! dim[lvl].joins
38
+ when Symbol
39
+ dims[k] = dim.prev_level(v).name
40
+ joins.merge! dim[v].joins
41
+ end
42
+ end
43
+
44
+ Fact.joins(joins).slice(dimensions: dims, measures: measures, skip_aggregated: true)
20
45
  end
21
46
 
22
47
  def rollup(options)
@@ -1,3 +1,3 @@
1
1
  module OlapReport
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -24,7 +24,7 @@ describe OlapReport::Cube::Aggregation do
24
24
  Fact.define_aggregation user: :user_id
25
25
  Fact.instance_variable_get(:@aggregations).size.should == 1
26
26
 
27
- Fact.define_aggregation user: :group_id, date: :created_at
27
+ Fact.define_aggregation user: :group_id, date: :month
28
28
  Fact.instance_variable_get(:@aggregations).size.should == 2
29
29
  end
30
30
 
@@ -49,4 +49,22 @@ describe OlapReport::Cube::QueryMethods do
49
49
  end
50
50
  end
51
51
  end
52
+
53
+ describe "::drilldown" do
54
+ it "should fetch level details down the hierarchy 1 level" do
55
+ expected = Fact.select('"users"."group_id" AS "group_id", COUNT("facts"."score") AS "score_count"').
56
+ joins(user: :group).
57
+ group('"users"."group_id"')
58
+
59
+ Fact.drilldown(dimensions: {user: :category}, measures: :score_count).should == expected
60
+ end
61
+
62
+ it "should fetch level details down the hierarchy to the specified level" do
63
+ expected = Fact.select('"facts"."user_id" AS "user_id", COUNT("facts"."score") AS "score_count"').
64
+ joins(user: :group).
65
+ group('"facts"."user_id"')
66
+
67
+ Fact.drilldown(dimensions: {user: {category: :user_id}}, measures: :score_count).should == expected
68
+ end
69
+ end
52
70
  end
@@ -42,8 +42,8 @@ describe OlapReport::Cube do
42
42
  end
43
43
  end
44
44
 
45
- it "should return nil if no dimension with specified name exists" do
46
- Fact.dimension(:new_dimension).should be_nil
45
+ it "should raise KeyError if no dimension with specified name exists" do
46
+ expect { Fact.dimension(:new_dimension) }.to raise_error(KeyError)
47
47
  end
48
48
  end
49
49
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olap_report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hck
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-29 00:00:00.000000000 Z
13
+ date: 2013-04-01 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Olap-like queries & aggregations for activerecord models using defined
16
16
  hierarchies & measures