Sha256: 695d3b672c8e1265637a1666f8e681c8d3ea2221c105ddd31ed896ba7f6c1447

Contents?: true

Size: 1.94 KB

Versions: 13

Compression:

Stored size: 1.94 KB

Contents

module Workarea
  module Reports
    module GroupByTime
      extend ActiveSupport::Concern

      included do
        cattr_accessor :group_bys
        self.group_bys = %w(year quarter month week day day_of_week)
      end

      def time_group_id
        case group_by
        when 'year' then year_id
        when 'quarter' then quarter_id
        when 'week' then week_id
        when 'day_of_week' then day_of_week_id
        when 'day' then day_id
        else month_id
        end
      end

      def group_by
        @group_by ||= params[:group_by].presence_in(group_bys) || 'month'
      end

      private

      def day_id
        {
          'year' => { '$year' => '$reporting_on' },
          'month' => { '$month' => '$reporting_on' },
          'day' => { '$dayOfMonth' => '$reporting_on' }
        }
      end

      def week_id
        {
          'year' => { '$year' => '$reporting_on' },
          'week' => { '$isoWeek' => '$reporting_on' }
        }
      end

      def day_of_week_id
        { 'day_of_week' => { '$dayOfWeek' => '$reporting_on' } }
      end

      def month_id
        {
          'year' => { '$year' => '$reporting_on' },
          'month' => { '$month' => '$reporting_on' }
        }
      end

      def quarter_id
        {
          'year' => { '$year' => '$reporting_on' },
          'quarter' => {
            '$cond' => [
              { '$lte' => [{ '$month' => '$reporting_on' }, 3] },
              1,
              {
                '$cond' => [
                  { '$lte' => [{ '$month' => '$reporting_on' }, 6] },
                  2,
                  {
                    '$cond' => [
                      { '$lte' => [{ '$month' => '$reporting_on' }, 9] },
                      3,
                      4
                    ]
                  }
                ]
              }
            ]
          }
        }
      end

      def year_id
        { 'year' => { '$year' => '$reporting_on' } }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
workarea-core-3.4.22 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.5.0 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.21 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.20 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.19 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.18 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.17 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.16 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.15 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.14 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.13 app/queries/workarea/reports/group_by_time.rb
workarea-core-3.4.12 app/queries/workarea/reports/group_by_time.rb