Class: ForestLiana::PieStatGetter
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from StatGetter
#initialize
Instance Attribute Details
#record ⇒ Object
Returns the value of attribute record.
3
4
5
|
# File 'app/services/forest_liana/pie_stat_getter.rb', line 3
def record
@record
end
|
Instance Method Details
#group_by_field ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'app/services/forest_liana/pie_stat_getter.rb', line 46
def group_by_field
if @params[:group_by_field].include? ':'
association, field = @params[:group_by_field].split ':'
resource = @resource.reflect_on_association(association.to_sym)
"#{resource.table_name}.#{field}"
else
"#{@resource.table_name}.#{@params[:group_by_field]}"
end
end
|
#order ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'app/services/forest_liana/pie_stat_getter.rb', line 56
def order
order = 'DESC'
if @params[:aggregate].downcase == 'sum'
field = @params[:aggregate_field].downcase
else
field = Rails::VERSION::MAJOR >= 5 || includes.size > 0 ? 'id' : 'all'
end
"#{@params[:aggregate].downcase}_#{field} #{order}"
end
|
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/services/forest_liana/pie_stat_getter.rb', line 5
def perform
if @params[:group_by_field]
timezone_offset = @params[:timezone].to_i
value = @resource
conditions = []
filter_operator = ''
if @params[:filterType] && @params[:filters]
filter_operator = " #{@params[:filterType]} ".upcase
@params[:filters].try(:each) do |filter|
operator, filter_value = OperatorValueParser.parse(filter[:value])
conditions << OperatorValueParser.get_condition(filter[:field],
operator, filter_value, @resource, @params[:timezone])
end
end
value = value
.unscoped
.eager_load(includes)
.where(conditions.join(filter_operator))
.group(group_by_field)
.order(order)
.send(@params[:aggregate].downcase, @params[:aggregate_field])
.map do |key, value|
if @resource.respond_to?(:defined_enums) &&
@resource.defined_enums.has_key?(@params[:group_by_field])
key = @resource.defined_enums[@params[:group_by_field]].invert[key]
elsif @resource.columns_hash[@params[:group_by_field]] &&
@resource.columns_hash[@params[:group_by_field]].type == :datetime
key = (key + timezone_offset.hours).strftime('%d/%m/%Y %T')
end
{ key: key, value: value }
end
@record = Model::Stat.new(value: value)
end
end
|