| Class | ActiveWarehouse::Cube |
| In: |
lib/active_warehouse/cube.rb
|
| Parent: | Object |
A Cube represents a collection of dimensions operating on a fact. The Cube provides a front-end for getting at the underlying data. Cubes support pluggable aggregation. The default aggregation is the NoAggregate which goes directly to the fact and dimensions to answer queries.
| pivots_on | -> | pivot_on |
| reports_on | -> | report_on |
| connect_through | [RW] | Specify the ActiveRecord class to connect through Note: this is a potential directive in a Cube subclass |
| temp_dir | [RW] | The temp directory for storing files during warehouse rebuilds |
Defaults to NoAggregate strategy.
# File lib/active_warehouse/cube.rb, line 163
163: def aggregate
164: @aggregate ||= ActiveWarehouse::Aggregate::NoAggregate.new(self)
165: end
# File lib/active_warehouse/cube.rb, line 167
167: def aggregate_class(agg_class)
168: @aggregate = agg_class.new(self)
169: end
returns the aggregate fields for this cube removing the aggregate fields that are defined in fact class that are related to hierarchical dimension, but the cube doesn‘t pivot on any hierarchical dimensions
# File lib/active_warehouse/cube.rb, line 94
94: def aggregate_fields
95: fact_class.aggregate_fields.reject {|field| !pivot_on_hierarchical_dimension? and !field.levels_from_parent.empty? }
96: end
Get the class name for the specified cube name Example: Regional Sales will become RegionalSalesCube
# File lib/active_warehouse/cube.rb, line 100
100: def class_name(name)
101: cube_name = name.to_s
102: cube_name = "#{cube_name}_cube" unless cube_name =~ /_cube$/
103: cube_name.classify
104: end
# File lib/active_warehouse/cube.rb, line 153
153: def connect_through
154: @connect_through ||= ActiveRecord::Base
155: end
Get an adapter connection
# File lib/active_warehouse/cube.rb, line 158
158: def connection
159: connect_through.connection
160: end
Get the dimension class for the specified dimension name
# File lib/active_warehouse/cube.rb, line 124
124: def dimension_class(dimension_name)
125: fact_class.dimension_relationships[dimension_name.to_sym].class_name.constantize
126: end
Get a list of dimension class instances
# File lib/active_warehouse/cube.rb, line 117
117: def dimension_classes
118: dimensions.collect do |dimension_name|
119: dimension_class(dimension_name)
120: end
121: end
Get the dimensions that this cube pivots on
# File lib/active_warehouse/cube.rb, line 66
66: def dimensions
67: @dimensions ||= fact_class.dimension_relationships.collect{|k,v| k}
68: end
Get an OrderedHash of each dimension mapped to its hierarchies which will be included in the cube
# File lib/active_warehouse/cube.rb, line 72
72: def dimensions_hierarchies
73: if @dimensions_hierarchies.nil?
74: @dimensions_hierarchies = OrderedHash.new
75: dimensions.each do |dimension|
76: @dimensions_hierarchies[dimension] = fact_class.dimension_class(dimension).hierarchies
77: end
78: end
79: @dimensions_hierarchies
80: end
Get the aggregated fact class instance
# File lib/active_warehouse/cube.rb, line 112
112: def fact_class
113: fact_class_name.constantize
114: end
Get the aggregated fact class name
# File lib/active_warehouse/cube.rb, line 107
107: def fact_class_name
108: ActiveWarehouse::Fact.class_name(@fact_name || name.sub(/Cube$/,'').underscore.to_sym)
109: end
Callback which is invoked when subclasses are created
# File lib/active_warehouse/cube.rb, line 11
11: def inherited(subclass)
12: subclasses << subclass
13: end
Get the time when the fact or any dimension referenced in this cube was last modified
# File lib/active_warehouse/cube.rb, line 135
135: def last_modified
136: lm = fact_class.last_modified
137: dimensions.each do |dimension|
138: dim = ActiveWarehouse::Dimension.class_for_name(dimension)
139: lm = dim.last_modified if dim.last_modified > lm
140: end
141: lm
142: end
returns true if this cube pivots on a hierarchical dimension.
# File lib/active_warehouse/cube.rb, line 83
83: def pivot_on_hierarchical_dimension?
84: dimension_classes.each do |dimension|
85: return true if dimension.hierarchical_dimension?
86: end
87: return false
88: end
Defines the dimensions that this cube pivots on. If the fact name and cube name are different (for example, if a PurchaseCube does not report on a PurchaseFact) then you must declare the reports_on first.
# File lib/active_warehouse/cube.rb, line 24
24: def pivots_on(*dimension_list)
25: @dimensions_hierarchies = OrderedHash.new
26: @dimensions = []
27: dimension_list.each do |dimension|
28: case dimension
29: when Symbol, String
30: dimensions << dimension.to_sym
31: dimensions_hierarchies[dimension.to_sym] = fact_class.dimension_class(dimension).hierarchies
32: when Hash
33: dimension_name = dimension.keys.first.to_sym
34: dimensions << dimension_name
35: dimensions_hierarchies[dimension_name] = [dimension[dimension_name]].flatten
36: else
37: raise ArgumentError, "Each argument to pivot_on must be a symbol, string or Hash"
38: end
39: end
40: end
Populate the data warehouse. Delegate to aggregate.populate
# File lib/active_warehouse/cube.rb, line 61
61: def populate(options={})
62: aggregate.populate
63: end
Rebuild the data warehouse.
# File lib/active_warehouse/cube.rb, line 56
56: def rebuild(options={})
57: populate(options)
58: end
Defines the fact name, without the ‘Fact’ suffix, that this cube reports on. For instance, if you have PurchaseFact, you could then call reports_on :purchase.
The default value for reports_on is to take the name of the cube, i.e. PurchaseCube, and remove the Cube suffix. The assumption is that your Cube name matches your Fact name.
# File lib/active_warehouse/cube.rb, line 50
50: def reports_on(fact_name)
51: @fact_name = fact_name
52: end
Get a list of all known subclasses
# File lib/active_warehouse/cube.rb, line 16
16: def subclasses
17: @subclasses ||= []
18: end
Get the database connection (delegates to Cube.connection class method)
# File lib/active_warehouse/cube.rb, line 198
198: def connection
199: self.class.connection
200: end
Query the cube. The column dimension, column hierarchy, row dimension and row hierarchy are all required.
The conditions value is a String that represents a SQL condition appended to the where clause. TODO: this may eventually be converted to another query language.
The cstage value represents the current column drill down stage and defaults to 0.
The rstage value represents the current row drill down stage and defaults to 0. Filters contains key/value pairs where the key is a string of ‘dimension.column’ and the value is the value to filter by. For example:
filters = {‘date.calendar_year’ => 2007, ‘product.category’ => ‘Food’} query(:date, :cy, :store, :region, 1, 0, filters)
Note that product.category refers to a dimension which is not actually visible but which is both part of the cube and is used for filtering.
# File lib/active_warehouse/cube.rb, line 193
193: def query(*args)
194: self.class.aggregate.query(*args)
195: end