Sha256: 871a6c484f8646f52824cfc617c0a99bd1a44c4ed26b2090f8329898e3478db4

Contents?: true

Size: 1.66 KB

Versions: 49

Compression:

Stored size: 1.66 KB

Contents

module Workarea
  module Reports
    class Searches
      include Report

      self.reporting_class = Metrics::SearchByDay
      self.sort_fields = %w(searches query_string total_results orders units_sold discounts revenue)

      def aggregation
        [filter_date_range, project_used_fields, group_by_query_id]
      end

      def filter_date_range
        {
          '$match' => {
            'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc },
            'total_results' => total_results_query
          }
        }
      end

      def project_used_fields
        {
          '$project' => {
            'query_id' => 1,
            'query_string' => 1,
            'total_results' => 1,
            'searches' => 1,
            'orders' => 1,
            'units_sold' => 1,
            'discounts' => 1,
            'revenue' => 1
          }
        }
      end

      def group_by_query_id
        {
          '$group' => {
            '_id' => '$query_id',
            'query_string' => { '$first' => '$query_string' },
            'total_results' => { '$last' => '$total_results' },
            'searches' => { '$sum' => '$searches' },
            'orders' => { '$sum' => '$orders' },
            'units_sold' => { '$sum' => '$units_sold' },
            'discounts' => { '$sum' => '$discounts' },
            'revenue' => { '$sum' => '$revenue' }
          }
        }
      end

      private

      def total_results_query
        if params[:results_filter] == 'without_results'
          { '$eq' => 0 }
        elsif params[:results_filter] == 'with_results'
          { '$gt' => 0 }
        else
          { '$gte' => 0 }
        end
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
workarea-core-3.5.15 app/queries/workarea/reports/searches.rb
workarea-core-3.4.36 app/queries/workarea/reports/searches.rb
workarea-core-3.5.14 app/queries/workarea/reports/searches.rb
workarea-core-3.4.35 app/queries/workarea/reports/searches.rb
workarea-core-3.5.13 app/queries/workarea/reports/searches.rb
workarea-core-3.4.34 app/queries/workarea/reports/searches.rb
workarea-core-3.5.12 app/queries/workarea/reports/searches.rb
workarea-core-3.4.33 app/queries/workarea/reports/searches.rb
workarea-core-3.5.11 app/queries/workarea/reports/searches.rb
workarea-core-3.5.10 app/queries/workarea/reports/searches.rb
workarea-core-3.4.32 app/queries/workarea/reports/searches.rb
workarea-core-3.5.9 app/queries/workarea/reports/searches.rb
workarea-core-3.4.31 app/queries/workarea/reports/searches.rb
workarea-core-3.5.8 app/queries/workarea/reports/searches.rb
workarea-core-3.4.30 app/queries/workarea/reports/searches.rb
workarea-core-3.5.7 app/queries/workarea/reports/searches.rb
workarea-core-3.4.29 app/queries/workarea/reports/searches.rb
workarea-core-3.5.6 app/queries/workarea/reports/searches.rb
workarea-core-3.4.28 app/queries/workarea/reports/searches.rb
workarea-core-3.5.5 app/queries/workarea/reports/searches.rb