2015-05-07 12:55:13 +00:00
module JamRuby
class BaseSearch < JsonStore
attr_accessor :page_count , :results , :page_number
ANY_VAL_STR = 'any'
ANY_VAL_INT = - 1
2015-05-27 02:58:04 +00:00
VAL_YES = 'yes'
VAL_NO = 'no'
2015-05-07 12:55:13 +00:00
PER_PAGE = 10
PG_SMALLINT_MAX = 32767
KEY_SKILL = 'skill_level'
KEY_GENRES = 'genres'
KEY_INSTRUMENTS = 'instruments'
KEY_GIGS = 'concert_gigs'
KEY_SORT_ORDER = 'sort_order'
2021-08-09 14:18:10 +00:00
KEY_JOINED_WITHIN = 'joined_within_days'
2021-08-31 18:25:15 +00:00
KEY_ACTIVE_WITHIN = 'active_within_days'
2015-05-07 12:55:13 +00:00
SORT_VALS = %W{ latency distance }
SORT_ORDERS = {
SORT_VALS [ 0 ] = > 'Latency to Me' ,
SORT_VALS [ 1 ] = > 'Distance to Me'
}
SKILL_VALS = [ ANY_VAL_INT , 1 , 2 ]
SKILL_LEVELS = {
SKILL_VALS [ 0 ] = > 'Any' ,
SKILL_VALS [ 1 ] = > 'Amateur' ,
SKILL_VALS [ 2 ] = > 'Pro' ,
}
2021-08-09 14:18:10 +00:00
JOINED_WITHIN_VALS = [ ANY_VAL_INT , 1 , 7 , 30 , 90 ]
JOINED_WITHIN_LABELS = {
JOINED_WITHIN_VALS [ 0 ] = > ANY_VAL_STR ,
JOINED_WITHIN_VALS [ 1 ] = > 'Within Last 1 Day' ,
JOINED_WITHIN_VALS [ 2 ] = > 'Within Last 7 Day' ,
JOINED_WITHIN_VALS [ 3 ] = > 'Within Last 30 Day' ,
JOINED_WITHIN_VALS [ 4 ] = > 'Within Last 90 Day' ,
}
2021-08-31 18:25:15 +00:00
ACTIVE_WITHIN_VALS = [ ANY_VAL_INT , 1 , 7 , 30 , 90 ]
ACTIVE_WITHIN_LABELS = {
ACTIVE_WITHIN_VALS [ 0 ] = > ANY_VAL_STR ,
ACTIVE_WITHIN_VALS [ 1 ] = > 'Within Last 1 Day' ,
ACTIVE_WITHIN_VALS [ 2 ] = > 'Within Last 7 Day' ,
ACTIVE_WITHIN_VALS [ 3 ] = > 'Within Last 30 Day' ,
ACTIVE_WITHIN_VALS [ 4 ] = > 'Within Last 90 Day' ,
}
2015-05-07 12:55:13 +00:00
GIG_COUNTS = [ ANY_VAL_INT , 0 , 1 , 2 , 3 , 4 ]
GIG_LABELS = {
GIG_COUNTS [ 0 ] = > 'Any' ,
GIG_COUNTS [ 1 ] = > 'under 10' ,
GIG_COUNTS [ 2 ] = > '10 to 50' ,
GIG_COUNTS [ 3 ] = > '50 to 100' ,
GIG_COUNTS [ 4 ] = > 'over 100'
}
INSTRUMENT_PROFICIENCY = {
1 = > 'Beginner' ,
2 = > 'Intermediate' ,
3 = > 'Expert' ,
}
2015-05-18 04:00:12 +00:00
def self . json_schema
{
KEY_SORT_ORDER = > self :: SORT_VALS [ 0 ] ,
KEY_INSTRUMENTS = > [ ] ,
KEY_GENRES = > [ ] ,
KEY_GIGS = > self :: GIG_COUNTS [ 0 ] . to_s ,
2021-08-09 14:18:10 +00:00
KEY_JOINED_WITHIN = > self :: JOINED_WITHIN_VALS [ 0 ] ,
2015-05-18 04:00:12 +00:00
}
end
def self . search_filter_meta ( jschema = nil , sort_order = nil )
jschema || = self . json_schema
schema_keys = jschema . keys
sort_order || = { keys : self :: SORT_VALS , map : self :: SORT_ORDERS }
multi_keys = jschema . collect { | kk , vv | vv . is_a? ( Array ) ? kk : nil } . compact
{
per_page : self :: PER_PAGE ,
filter_keys : {
keys : schema_keys ,
multi : multi_keys ,
single : schema_keys - multi_keys ,
} ,
sort_order : sort_order
}
end
2015-05-07 12:55:13 +00:00
2015-05-07 13:21:53 +00:00
RESULT_FOLLOW = :follows
RESULT_FRIEND = :friends
COUNT_FRIEND = :count_friend
COUNT_FOLLOW = :count_follow
COUNT_RECORD = :count_record
COUNT_SESSION = :count_session
COUNTERS = [ COUNT_FRIEND , COUNT_FOLLOW , COUNT_RECORD , COUNT_SESSION ]
2015-05-07 12:55:13 +00:00
def self . user_search_filter ( user )
unless ss = user . send ( self . name . demodulize . tableize . singularize )
ss = self . create_search ( user )
end
ss
end
def self . search_filter_json ( user )
self . user_search_filter ( user ) . json
end
def self . create_search ( user )
ms = self . new
ms . user = user
2015-05-18 04:00:12 +00:00
ms . data_blob = self . json_schema
2015-05-07 12:55:13 +00:00
ms . save!
ms
end
def self . search_target_class
end
2015-09-19 21:19:27 +00:00
def self . genre_ids
@@genre_ids || = Hash [ * Genre . pluck ( :id ) . collect { | v | [ v , v ] } . flatten ]
end
def self . instrument_ids
@@instrument_ids || = Hash [ * Instrument . pluck ( :id ) . collect { | v | [ v , v ] } . flatten ]
end
2015-05-26 08:43:11 +00:00
def _genres ( rel , query_data = json )
gids = query_data [ KEY_GENRES ]
2015-05-07 12:55:13 +00:00
unless gids . blank?
2015-09-19 21:19:27 +00:00
allgids = self . class . genre_ids
gids = gids . select { | gg | allgids . has_key? ( gg ) }
2015-08-25 08:23:52 +00:00
unless gids . blank?
gidsql = gids . join ( " ',' " )
gpsql = " SELECT player_id FROM genre_players WHERE (player_type = ' #{ self . class . search_target_class . name } ' AND genre_id IN (' #{ gidsql } ')) "
rel = rel . where ( " #{ self . class . search_target_class . table_name } .id IN ( #{ gpsql } ) " )
end
2015-05-07 12:55:13 +00:00
end
rel
end
2015-05-26 08:43:11 +00:00
def _instruments ( rel , query_data = json )
unless ( instruments = query_data [ KEY_INSTRUMENTS ] ) . blank?
2015-09-19 21:19:27 +00:00
instrids = self . class . instrument_ids
instruments = instruments . select { | ii | instrids . has_key? ( ii [ 'instrument_id' ] ) }
2015-08-25 08:23:52 +00:00
unless instruments . blank?
instsql = " SELECT player_id FROM musicians_instruments WHERE (( "
instsql += instruments . collect do | inst |
unless MusicianInstrument :: PROFICIENCY_RANGE === ( proflvl = inst [ 'proficiency_level' ] . to_i )
proflvl = MusicianInstrument :: LEVEL_INTERMEDIATE
end
" instrument_id = ' #{ inst [ 'instrument_id' ] } ' AND proficiency_level = #{ proflvl } "
end . join ( " ) OR ( " )
instsql += " )) "
rel = rel . where ( " #{ self . class . search_target_class . table_name } .id IN ( #{ instsql } ) " )
end
2015-05-26 06:39:43 +00:00
end
2015-05-07 12:55:13 +00:00
rel
end
def _gigs ( rel )
gg = json [ KEY_GIGS ] . to_i
rel = rel . where ( 'concert_count = ?' , gg ) if 0 < = gg
rel
end
def _skills ( rel )
if 0 < ( val = json [ KEY_SKILL ] . to_i )
rel = rel . where ( [ 'skill_level = ?' , val ] )
end
rel
end
2021-08-09 14:18:10 +00:00
def _joined_within ( rel )
#debugger
if 0 < ( val = json [ KEY_JOINED_WITHIN ] . to_i )
2021-12-22 13:26:13 +00:00
rel = rel . where ( " users.created_at >= ? " , val . days . ago . at_beginning_of_day )
2021-08-09 14:18:10 +00:00
end
rel
end
2015-05-07 12:55:13 +00:00
def _sort_order ( rel )
end
def do_search ( params = { } )
end
def process_results_page ( objs )
end
def search_includes ( rel )
rel
end
2022-10-18 16:37:15 +00:00
def filter_includes ( rel )
rel
end
2022-03-18 13:57:48 +00:00
def user_search_results ( user_ids )
rel = do_filter ( user_ids )
2022-10-18 16:37:15 +00:00
rel = self . filter_includes ( rel )
process_results_page ( rel . all , true ) #skip_counters = true
2022-03-18 13:57:48 +00:00
end
2021-12-18 14:41:28 +00:00
def search_results_page ( filter = nil , page = 1 , user_ids = nil )
2015-05-07 12:55:13 +00:00
if filter
self . data_blob = filter
self . save
else
filter = self . data_blob
end
2022-02-08 15:25:00 +00:00
#NOTE: user_ids parameter is used to track users returned from neo4j user's latency data service. if this variable is not null means we are calling this method with user_ids returned from neo4j
if user_ids
rel = do_filter ( user_ids )
else
rel = do_search ( filter , user_ids )
end
2015-05-07 12:55:13 +00:00
@page_number = [ page . to_i , 1 ] . max
2015-05-07 18:08:36 +00:00
rel = rel . paginate ( :page = > @page_number , :per_page = > self . class :: PER_PAGE )
2015-05-07 12:55:13 +00:00
rel = self . search_includes ( rel )
@page_count = rel . total_pages
process_results_page ( rel . all )
end
def reset_filter
2015-05-18 04:00:12 +00:00
self . data_blob = self . class . json_schema
2015-05-07 12:55:13 +00:00
self . save
end
def reset_search_results
reset_filter
search_results_page
end
def search_type
self . class . to_s
end
def is_blank?
2015-05-18 04:00:12 +00:00
self . data_blob == self . class . json_schema
2015-05-07 12:55:13 +00:00
end
def description
end
end
end