Sha256: 01f8bf181436b92d95c33d6c700f75e514551a75752215144766f91b58650483

Contents?: true

Size: 1.72 KB

Versions: 23

Compression:

Stored size: 1.72 KB

Contents

module Workarea
  module Insights
    class TrendingSearches < Base
      class << self
        def dashboards
          %w(search)
        end

        def generate_monthly!
          results = generate_results.map { |r| r.merge(query_id: r['_id']) }
          create!(results: results) if results.present?
        end

        def generate_results
          Metrics::SearchByWeek
            .collection
            .aggregate([filter_date_range, group_by_query, add_improving_weeks, sort, limit])
            .to_a
        end

        def filter_date_range
          {
            '$match' => {
              'reporting_on' => {
                '$gte' => beginning_of_last_month.utc,
                '$lte' => end_of_last_month.utc
              }
            }
          }
        end

        def group_by_query
          {
            '$group' => {
              '_id' => '$query_id',
              'improving_weeks' => { '$sum' => 1 },
              'revenue_changes' => { '$push' => '$revenue_change' },
              'orders' => { '$sum' => '$orders' }
            }
          }
        end

        def add_improving_weeks
          {
            '$addFields' => {
              'improving_weeks' => {
                '$size' => {
                  '$filter' => {
                    'input' => '$revenue_changes',
                    'as' => 'revenue_change',
                    'cond' => { '$gt' => ['$$revenue_change', 0] }
                  }
                }
              }
            }
          }
        end

        def sort
          { '$sort' => { 'improving_weeks' => -1, 'orders' => -1 } }
        end

        def limit
          { '$limit' => Workarea.config.insights_searches_list_max_results }
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.44 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.43 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.42 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.41 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.40 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.39 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.38 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.37 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.36 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.35 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.34 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.33 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.32 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.31 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.30 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.29 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.28 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.27 app/models/workarea/insights/trending_searches.rb
workarea-core-3.4.26 app/models/workarea/insights/trending_searches.rb