Class: Shades::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/shades/cube.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Query

Returns a new instance of Query.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/shades/cube.rb', line 3

def initialize(opts)
  dimensions = []
  @dcs = opts[:categorizations].map do |d|
    dimensions.push(d)
    DimensionComputer.new(d, d)
  end unless opts[:categorizations].nil?
  @mrs = opts[:rollups].map do |r|
    MeasureRollup.new(r[:measure], r[:measure], r[:stat]) unless opts[:rollups].nil?
  end unless opts[:rollups].nil?
  @sorting = opts[:sorting]
  if !does_sorting?
    # got a query with no sorting parameters. but surely we should
    # return the info with some meaningful ordering
    # so, we choose to order by the first measure returned in the query, largest values firs
    @sorting = [{:key => outbound_measures.first, :asc => false}]
  end
  @pre = Processor.new(@dcs)
end

Instance Method Details

#does_rollups?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/shades/cube.rb', line 30

def does_rollups?
  !@dcs.nil? && !@dcs.empty? && !@mrs.nil? && !@mrs.empty?
end

#does_sorting?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/shades/cube.rb', line 26

def does_sorting?
  !@sorting.nil? && !@sorting.empty?
end

#lookup(e, k) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/shades/cube.rb', line 72

def lookup(e, k)
  v = e.dimension(k)
  if v.nil?
    v = e.measure(k)
  end
  natural_order(v)
end

#multicompare(a, b) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shades/cube.rb', line 57

def multicompare(a, b)
  c = 0
  @sorting.each do |s|
    v1 = lookup(a, s[:key])
    v2 = lookup(b, s[:key])
    asc = s[:asc]
    if v1 < v2
      c = if asc; -1 else 1 end
    elsif v2 < v1
      c = if asc; 1 else -1 end
    end
  end
  c
end

#natural_order(o) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/shades/cube.rb', line 80

def natural_order(o)
  begin
    return Float(o)
  rescue
  end
  o
end

#outbound_measuresObject



34
35
36
# File 'lib/shades/cube.rb', line 34

def outbound_measures
  @mrs.map { |r| r.outbound_measure }
end

#rollup_listObject



22
23
24
# File 'lib/shades/cube.rb', line 22

def rollup_list
  @mrs
end

#run(events_in) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shades/cube.rb', line 38

def run(events_in)
  if does_rollups?
    aggregator = Aggregator.new(self)
    events_in.each do |event|
      eout = @pre.send(event)
      if !eout.nil?
        aggregator.add(eout)
      end
    end
    results = aggregator.snapshot
  end
  if !@sorting.nil?
    results.sort! do |a, b|
      multicompare(a, b)
    end
  end
  results
end