custom_sort 0.4.02 → 0.4.07
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/custom_sort.rb +7 -3
- data/lib/custom_sort/magic_new.rb +11 -2
- data/lib/custom_sort/relation_builder.rb +51 -20
- data/lib/custom_sort/version.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 722f4ad4bb8ff604ac7b481e2f51153f952235e341ad87975d39ace75653fa5d
|
4
|
+
data.tar.gz: eac40061c9d950bb0a83ea72fe88e474df1b3b919952da6b816402b8214f6db9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20aa137fc8c67df5cff97ffc469c281d11ab2b5e80cc015dd7cd7cdb444087c4e42ab609d28e336470417830e4c79f513f4558c1daa8498e14b162a8ebe1d1ca
|
7
|
+
data.tar.gz: c4abd3aeb40c5fbd0a2c53fde15491be7c200cfcf5427a0a22fd028017bb56e6e6b755460886157234c85dc5d0de888dbbeb9b0f800528aaac2a158082c3d28d
|
data/lib/custom_sort.rb
CHANGED
@@ -14,7 +14,11 @@ module CustomSort
|
|
14
14
|
class Error < StandardError; end
|
15
15
|
# Your code goes here...
|
16
16
|
|
17
|
-
|
17
|
+
# currently, :labeled requires a vote_scope/label as well, the other queries ignore vote_scope
|
18
|
+
FIELDS = [:labeled,:blended,:quality,:interesting,:learned,:votes,:nofeedback,:nojoin] # [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :minute_of_hour, :day_of_month, :day_of_year, :month_of_year]
|
19
|
+
|
20
|
+
LABELS = [:quality,:interesting,:fun,:funny,:learnedfrom,:spam,:ad,:clickbait,:english]
|
21
|
+
|
18
22
|
METHODS = FIELDS.map { |v| :"customsort_by_#{v}" } + [:customsort_by_field]
|
19
23
|
LAMBDA_VALUES = [-0.02445,-0.0489,-0.0990, nil]
|
20
24
|
|
@@ -26,8 +30,8 @@ module CustomSort
|
|
26
30
|
def initialize
|
27
31
|
|
28
32
|
end
|
29
|
-
def self.time_decay table_name
|
30
|
-
"EXTRACT(EPOCH FROM (NOW()::timestamp - #{table_name}
|
33
|
+
def self.time_decay table_name, time_column = "created_at"
|
34
|
+
"EXTRACT(EPOCH FROM (NOW()::timestamp - #{table_name}.#{time_column}::timestamp))/(24*60*60)"
|
31
35
|
end
|
32
36
|
|
33
37
|
def self.time_decay_adjusted table_name, lambda_key
|
@@ -2,13 +2,19 @@ require "i18n"
|
|
2
2
|
|
3
3
|
module CustomSort
|
4
4
|
class MagicNew
|
5
|
-
attr_accessor :query_name, :recency_key, :options
|
5
|
+
attr_accessor :query_name, :label, :sort_order, :recency_key, :recent, :options
|
6
6
|
|
7
7
|
def initialize(query_name:, **options)
|
8
8
|
@query_name = query_name
|
9
9
|
@options = options
|
10
10
|
|
11
|
+
@recent = options[:recent] == false ? false : true
|
11
12
|
@recency_key = options[:recency_key] ? options[:recency_key].to_i : 2
|
13
|
+
@label = options[:label] ? options[:label] : nil
|
14
|
+
@label = @label.nil? ? CustomSort::LABELS[0].to_s : @label
|
15
|
+
|
16
|
+
@sort_order = options[:sort_order] ? (["asc","desc"].include?(options[:sort_order].downcase) ? options[:sort_order] : "desc") : "desc"
|
17
|
+
|
12
18
|
end
|
13
19
|
|
14
20
|
def self.validate_period(period, permit)
|
@@ -46,7 +52,10 @@ module CustomSort
|
|
46
52
|
relation,
|
47
53
|
column: field,
|
48
54
|
query_name: magic.query_name,
|
49
|
-
|
55
|
+
recent: magic.recent,
|
56
|
+
recency_key: magic.recency_key,
|
57
|
+
label: magic.label,
|
58
|
+
sort_order: magic.sort_order
|
50
59
|
).generate
|
51
60
|
|
52
61
|
relation
|
@@ -1,18 +1,21 @@
|
|
1
1
|
module CustomSort
|
2
2
|
class RelationBuilder
|
3
|
-
attr_reader :query_name, :column, :recency_key #, :period
|
3
|
+
attr_reader :query_name, :column, :recency_key, :label #, :period
|
4
4
|
#, :period, :day_start, :week_start, :n_seconds
|
5
5
|
|
6
6
|
#def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:)
|
7
|
-
def initialize(relation, column:, query_name:, recency_key:)
|
7
|
+
def initialize(relation, column:, query_name:, recency_key:, label: nil, sort_order: "DESC", recent: true)
|
8
8
|
@relation = relation
|
9
9
|
@column = resolve_column(relation, column) unless column.nil?
|
10
10
|
@query_name = query_name
|
11
11
|
@table_name = resolve_table(relation) #"items"
|
12
12
|
@class_name = resolve_class(relation)
|
13
13
|
|
14
|
-
@
|
14
|
+
@recent = recent == false ? recent : true
|
15
15
|
@lambda_key = recency_key #2
|
16
|
+
@sort_order = sort_order
|
17
|
+
|
18
|
+
@label = label #label.nil? ? CustomSort::LABELS[0].to_s : label
|
16
19
|
|
17
20
|
#if relation.default_timezone == :local
|
18
21
|
# raise CustomSort::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
|
@@ -24,17 +27,38 @@ module CustomSort
|
|
24
27
|
|
25
28
|
case query_name
|
26
29
|
when "nojoin"
|
27
|
-
@relation.where(*where_clause).order(order_clause)
|
30
|
+
@relation.select(net_score_col).select(time_decay_col).where(*where_clause).order(order_clause)
|
28
31
|
else
|
29
|
-
@relation.joins(join_clause).where(*where_clause).order(order_clause)
|
32
|
+
@relation.select(net_score_col).select(time_decay_col).joins(join_clause).where(*where_clause).order(order_clause)
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
33
36
|
private
|
37
|
+
def default_sort_col
|
38
|
+
"created_at"
|
39
|
+
end
|
40
|
+
def sort_order
|
41
|
+
["asc","desc"].include?(@sort_order.downcase) ? @sort_order : "desc"
|
42
|
+
end
|
43
|
+
|
44
|
+
def net_score_col
|
45
|
+
"#{net_score} as net_score"
|
46
|
+
end
|
47
|
+
def net_score
|
48
|
+
case query_name
|
49
|
+
when "nofeedback","nojoin"
|
50
|
+
time_decay
|
51
|
+
else
|
52
|
+
sort_col
|
53
|
+
end
|
54
|
+
end
|
34
55
|
|
56
|
+
def time_decay_col
|
57
|
+
"#{time_decay} as time_decay"
|
58
|
+
end
|
35
59
|
|
36
60
|
def time_decay
|
37
|
-
CustomSort::SortFields.time_decay_adjusted(@table_name,@lambda_key)
|
61
|
+
@recent ? CustomSort::SortFields.time_decay_adjusted(@table_name,@lambda_key) : 1
|
38
62
|
end
|
39
63
|
|
40
64
|
def quality_min
|
@@ -67,6 +91,10 @@ module CustomSort
|
|
67
91
|
end
|
68
92
|
"(#{cols.join(" + ")})"
|
69
93
|
end
|
94
|
+
def labeled_col
|
95
|
+
"(cached_weighted_#{@label}_average * cached_weighted_#{@label}_total)"
|
96
|
+
end
|
97
|
+
|
70
98
|
def blended_where vote_filter_col
|
71
99
|
cols = []
|
72
100
|
CustomSort.blended_columns.each do |col|
|
@@ -77,28 +105,29 @@ module CustomSort
|
|
77
105
|
["#{vote_filter_col} = ? AND #{cols.join(" AND ")}",@class_name]
|
78
106
|
end
|
79
107
|
|
108
|
+
def labeled_where vote_filter_col
|
109
|
+
["#{vote_filter_col} = ?",@class_name]
|
110
|
+
end
|
111
|
+
|
80
112
|
def data_table
|
81
113
|
CustomSort.data_table
|
82
114
|
end
|
83
115
|
|
84
|
-
def
|
116
|
+
def sort_col
|
85
117
|
case query_name
|
86
|
-
when "quality"
|
87
|
-
|
88
|
-
when "interesting"
|
89
|
-
sort_col = interesting_col
|
90
|
-
when "learned"
|
91
|
-
sort_col = learned_col
|
92
|
-
when "blended"
|
93
|
-
sort_col = blended_col
|
118
|
+
when "quality", "interesting","learned","blended","labeled"
|
119
|
+
"(#{time_decay}*#{send(query_name+"_col")})"
|
94
120
|
when "blended_recent"
|
95
|
-
|
121
|
+
"(#{time_decay}*#{blended_col})"
|
96
122
|
when "nofeedback","nojoin"
|
97
|
-
|
123
|
+
default_sort_col
|
98
124
|
else
|
99
|
-
|
125
|
+
votes_col
|
100
126
|
end
|
101
|
-
|
127
|
+
end
|
128
|
+
|
129
|
+
def order_clause
|
130
|
+
sort_col + " " + sort_order
|
102
131
|
end
|
103
132
|
|
104
133
|
def join_clause
|
@@ -117,8 +146,10 @@ module CustomSort
|
|
117
146
|
vote_filter_col = "#{data_table}.resource_type"
|
118
147
|
#base_query = ["vote_caches.resource_type = ?",@class_name]
|
119
148
|
case query_name
|
120
|
-
when "blended"
|
149
|
+
when "blended"
|
121
150
|
query = blended_where vote_filter_col
|
151
|
+
when "weighted"
|
152
|
+
query = weighted_where vote_filter_col
|
122
153
|
when "interesting"
|
123
154
|
query = ["#{vote_filter_col} = ? AND #{interesting_col} > ?",@class_name, interesting_min]
|
124
155
|
when "learned"
|
data/lib/custom_sort/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module CustomSort
|
2
|
-
VERSION = "0.4.
|
3
|
-
end
|
2
|
+
VERSION = "0.4.07"
|
3
|
+
end
|