Class: ForestLiana::PieStatGetter
- Inherits:
-
StatGetter
- Object
- BaseGetter
- StatGetter
- ForestLiana::PieStatGetter
- 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 inherited from StatGetter
Methods inherited from BaseGetter
#get_collection, #get_resource
Constructor Details
This class inherits a constructor from ForestLiana::StatGetter
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
47 48 49 50 51 52 53 54 55 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 47 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
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 57 def order order = 'DESC' # NOTICE: The generated alias for a count is "count_all", for a sum the # alias looks like "sum_#{aggregate_field}" 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 |
#perform ⇒ Object
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 45 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 5 def perform if @params[:group_by_field] timezone_offset = @params[:timezone].to_i 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 = get_resource() .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| # 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[:group_by_field]) && key.is_a?(Integer) 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 |