Class: Workarea::Insights::TrendingSearches
- Inherits:
-
Base
- Object
- Base
- Workarea::Insights::TrendingSearches
show all
- Defined in:
- app/models/workarea/insights/trending_searches.rb
Class Method Summary
collapse
Methods inherited from Base
beginning_of_last_month, current, end_of_last_month, generate_daily!, generate_weekly!, #include?, #slug
#releasable?
assert_valid_config!, async, disable, enable, inline, #run_callbacks
Class Method Details
.add_improving_weeks ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'app/models/workarea/insights/trending_searches.rb', line 43
def add_improving_weeks
{
'$addFields' => {
'improving_weeks' => {
'$size' => {
'$filter' => {
'input' => '$revenue_changes',
'as' => 'revenue_change',
'cond' => { '$gt' => ['$$revenue_change', 0] }
}
}
}
}
}
end
|
.dashboards ⇒ Object
5
6
7
|
# File 'app/models/workarea/insights/trending_searches.rb', line 5
def dashboards
%w(search)
end
|
.filter_date_range ⇒ Object
21
22
23
24
25
26
27
28
29
30
|
# File 'app/models/workarea/insights/trending_searches.rb', line 21
def filter_date_range
{
'$match' => {
'reporting_on' => {
'$gte' => beginning_of_last_month,
'$lte' => end_of_last_month
}
}
}
end
|
.generate_monthly! ⇒ Object
9
10
11
12
|
# File 'app/models/workarea/insights/trending_searches.rb', line 9
def generate_monthly!
results = generate_results.map { |r| r.merge(query_id: r['_id']) }
create!(results: results) if results.present?
end
|
.generate_results ⇒ Object
14
15
16
17
18
19
|
# File 'app/models/workarea/insights/trending_searches.rb', line 14
def generate_results
Metrics::SearchByWeek
.collection
.aggregate([filter_date_range, group_by_query, add_improving_weeks, sort, limit])
.to_a
end
|
.group_by_query ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'app/models/workarea/insights/trending_searches.rb', line 32
def group_by_query
{
'$group' => {
'_id' => '$query_id',
'improving_weeks' => { '$sum' => 1 },
'revenue_changes' => { '$push' => '$revenue_change' },
'orders' => { '$sum' => '$orders' }
}
}
end
|
.limit ⇒ Object
63
64
65
|
# File 'app/models/workarea/insights/trending_searches.rb', line 63
def limit
{ '$limit' => Workarea.config.insights_searches_list_max_results }
end
|
.sort ⇒ Object
59
60
61
|
# File 'app/models/workarea/insights/trending_searches.rb', line 59
def sort
{ '$sort' => { 'improving_weeks' => -1, 'orders' => -1 } }
end
|