Class: Mongoid::Report::Scope

Inherits:
Struct
  • Object
show all
Defined in:
lib/mongoid/report/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context

Returns:

  • (Object)

    the current value of context



6
7
8
# File 'lib/mongoid/report/scope.rb', line 6

def context
  @context
end

#report_moduleObject

Returns the value of attribute report_module

Returns:

  • (Object)

    the current value of report_module



6
7
8
# File 'lib/mongoid/report/scope.rb', line 6

def report_module
  @report_module
end

#report_nameObject

Returns the value of attribute report_name

Returns:

  • (Object)

    the current value of report_name



6
7
8
# File 'lib/mongoid/report/scope.rb', line 6

def report_name
  @report_name
end

Instance Method Details

#allObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mongoid/report/scope.rb', line 62

def all
  self.yield unless yielded?

  aggregation_queries = compile_queries

  rows = if batches.present?
    all_in_batches(aggregation_queries)
  else
    all_inline(aggregation_queries)
  end

  # in case if we want to store rows to collection
  if output.present?
    output.do(rows)
  end

  Collection.new(context, rows, fields, columns, mapping)
end

#all_in_batches(aggregation_queries) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongoid/report/scope.rb', line 33

def all_in_batches(aggregation_queries)
  # Lets assume we have only one field for making splits for the
  # aggregation queries.
  rows = []

  threads = batches.map do |r|
    # For now we are supporting only data fields for splitting up the
    # queries.
    range_match = r.map { |time| time.to_date.mongoize }

    Thread.new do
      q =
        ['$match' => { batches.field => { '$gte' => range_match.first, '$lte' => range_match.last } }] +
        aggregation_queries

      # if groups == [batch.field]
      rows.concat(Array(collection.aggregate(q)))
    end
  end
  threads.map(&:join)

  merger = Mongoid::Report::Merger.new(groups)
  merger.do(rows)
end

#all_inline(aggregation_queries) ⇒ Object



58
59
60
# File 'lib/mongoid/report/scope.rb', line 58

def all_inline(aggregation_queries)
  Array(collection.aggregate(aggregation_queries))
end

#in(collection_name) ⇒ Object



28
29
30
31
# File 'lib/mongoid/report/scope.rb', line 28

def in(collection_name)
  input.collection_name = collection_name
  self
end

#in_batches(conditions) ⇒ Object



81
82
83
84
# File 'lib/mongoid/report/scope.rb', line 81

def in_batches(conditions)
  batches.conditions = conditions
  self
end

#out(collection_name, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/mongoid/report/scope.rb', line 22

def out(collection_name, options = {})
  output.collection_name = collection_name
  output.options = options
  self
end

#query(conditions = {}) ⇒ Object



7
8
9
10
# File 'lib/mongoid/report/scope.rb', line 7

def query(conditions = {})
  queries.concat([conditions]) unless conditions.empty?
  self
end

#yieldObject

We need to add grouping conditions when user needs it.



13
14
15
16
17
18
19
20
# File 'lib/mongoid/report/scope.rb', line 13

def yield
  return self if @yielded

  queries.concat(context.queries(report_module, report_name))
  @yielded = true

  self
end