Class: ForestLiana::PieStatGetter
- Inherits:
-
StatGetter
- Object
- BaseGetter
- StatGetter
- ForestLiana::PieStatGetter
- Includes:
- AggregationHelper
- Defined in:
- app/services/forest_liana/pie_stat_getter.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
Methods included from AggregationHelper
#aggregation_alias, #aggregation_sql, #resolve_field_path
Methods inherited from StatGetter
#get_resource, #initialize, #validate_params
Methods inherited from BaseGetter
#get_collection, #get_resource, #includes_for_serialization
Constructor Details
This class inherits a constructor from ForestLiana::StatGetter
Instance Attribute Details
#record ⇒ Object
Returns the value of attribute record.
4 5 6 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 4 def record @record end |
Instance Method Details
#groupByFieldName ⇒ Object
46 47 48 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 46 def groupByFieldName resolve_field_path(@params[:groupByFieldName]) end |
#perform ⇒ Object
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 6 def perform if @params[:groupByFieldName] timezone_offset = @params[:timezone].to_i resource = optimize_record_loading(@resource, get_resource) filters = ForestLiana::ScopeManager.append_scope_for_user(@params[:filter], @user, @resource.name, @params['contextVariables']) unless filters.blank? resource = FiltersParser.new(filters, resource, @params[:timezone], @params).apply_filters end aggregation_type = @params[:aggregator].downcase aggregation_field = @params[:aggregateFieldName] alias_name = aggregation_alias(aggregation_type, aggregation_field) resource = resource .group(groupByFieldName) .order(Arel.sql("#{alias_name} DESC")) .pluck(groupByFieldName, Arel.sql("#{aggregation_sql(aggregation_type, aggregation_field)} AS #{alias_name}")) result = resource.map do |key, value| # NOTICE: Display the enum name instead of an integer if it is an # "Enum" field type on old Rails version (before Rails # 5.1.3). if @resource.respond_to?(:defined_enums) && @resource.defined_enums.has_key?(@params[:groupByFieldName]) && key.is_a?(Integer) key = @resource.defined_enums[@params[:groupByFieldName]].invert[key] elsif @resource.columns_hash[@params[:groupByFieldName]] && @resource.columns_hash[@params[:groupByFieldName]].type == :datetime key = (key + timezone_offset.hours).strftime('%d/%m/%Y %T') end { key: key, value: value } end @record = Model::Stat.new(value: result) end end |