class ApiSearchController < ApiController # have to be signed in currently to see this screen before_filter :api_signed_in_user, :except => :jam_tracks respond_to :json include LatencyHelper def index if 1 == params[Search::PARAM_MUSICIAN].to_i || 1 == params[Search::PARAM_BAND].to_i query = params.clone query[:remote_ip] = request.remote_ip if 1 == query[Search::PARAM_MUSICIAN].to_i @search = Search.musician_filter(query, current_user) else @search = Search.band_filter(query, current_user) end respond_with @search, responder: ApiResponder, :status => 200 elsif 1 == params[Search::PARAM_SESSION_INVITE].to_i @search = Search.session_invite_search(params[:query], current_user) else @search = Search.text_search(params, current_user) end end def musicians if request.get? if params[:results] @search = MusicianSearch.user_search_filter(current_user).search_results_page respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' else render :json => MusicianSearch.search_filter_json(current_user), :status => 200 end elsif request.post? sobj = MusicianSearch.user_search_filter(current_user) filter = params[:filter] if filter == 'reset' @search = sobj.reset_search_results else json = JSON.parse(filter, :create_additions => false) @search = sobj.search_results_page(json, [params[:page].to_i, 1].max) end respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' end end def bands if request.get? if params[:results] @search = BandSearch.user_search_filter(current_user).search_results_page(params[:subtype]) respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' else render :json => BandSearch.search_filter_json(current_user, params[:subtype]), :status => 200 end elsif request.post? sobj = BandSearch.user_search_filter(current_user) filter = params[:filter] if filter == 'reset' @search = sobj.reset_search_results(params[:subtype]) else json = JSON.parse(filter, :create_additions => false) @search = sobj.search_results_page(params[:subtype], json, [params[:page].to_i, 1].max) end respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' end end def jam_tracks if request.get? if params[:iso639] render(json: JamTrackSearch.all_languages.to_json, status: 200) elsif params[:indexed] fname = "#{Rails.root}/tmp/jtx_indices.json" unless File.exists?(fname) && File.size(fname) > 0 SampleApp::Application.load_tasks Rake::Task["mobile:jam_tracks_json"].invoke end json = JSON.parse(File.read(fname)) render(json: json, status: 200) else render(json: {}) end elsif request.post? jts = JamTrackSearch.new filter = request.params[:api_search] result = jts.search_results_page(filter) render(json: result.to_json, status: 200) end end #Filter users by first fetching users from latency graph database #for latency specific filter options and then query the postgresql relational #database for other filter options # def filter # page = [params[:page].to_i, 1].max # limit = 20 # filter_params = {} # filter_params.merge!(from_location: params[:from_location] ? '1' : '0') # genres = params[:genres] # if genres && genres.any? # genres.map!{|genre| {id: genre} } # filter_params.merge!(genres: genres) # end # beginner = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_beginner]) # intermediate = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_intermediate]) # expert = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_expert]) # proficiency_levels = [] # proficiency_levels.push(1) if beginner # proficiency_levels.push(2) if intermediate # proficiency_levels.push(3) if expert # instruments = params[:instruments] # if instruments && instruments.any? # inst = [] # instruments.each do |ii| # proficiency_levels.each do |pl| # inst << { instrument_id: ii, proficiency_level: pl} # end # end # filter_params.merge!(instruments: inst) # end # filter_params.merge!(joined_within_days: params[:joined_within_days]) unless params[:joined_within_days].blank? # filter_params.merge!(active_within_days: params[:active_within_days]) unless params[:active_within_days].blank? # latency_good = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_good]) # latency_fair = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_fair]) # latency_high = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_high]) # @latency_data = [] # #begin # #bm = Benchmark.measure do # @latency_data = users_latency_data(latency_good, latency_fair, latency_high, filter_params, page, limit) # user_ids = @latency_data.map{ |l_data| l_data[:user_id] } # user_ids = User.musicians.limit(3).map(&:id) # #end # # Bugsnag.notify("search_users_benchmark") do |report| # # report.severity = "info" # # report.add_tab(:benchmark, benchmark: bm.to_s) # # end if Rails.env.production? # # filter_params = { # # "sort_order"=>"latency", # # "instruments"=>[], # # "genres"=> [], # # "concert_gigs"=>"-1", # # "interests"=>"any", # # "studio_sessions"=>"-1", # # "ages"=>[], # # "skill_level"=>"-1", # # "joined_within_days"=>"-1", # # "active_within_days"=>"-1", # # "from_location" => "0", # # } # sobj = MusicianSearch.user_search_filter(current_user) # #debugger # @search = sobj.search_results_page(filter_params, page, limit, user_ids) # respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' # # rescue => exception # # logger.debug("Latency exception: #{exception.message}") # # Bugsnag.notify(exception) do |report| # # report.severity = "error" # # report.add_tab(:latency, { # params: params, # user_id: current_user.id, # name: current_user.name, # url: filter_latency_url, # }) # end # render json: {}, status: 500 # end #end def filter latency_good = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_good]) latency_fair = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_fair]) latency_high = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_high]) page = [params[:page].to_i, 1].max limit = 20 filter_params = {} filter_params.merge!(from_location: params[:from_location] ? '1' : '0') genres = params[:genres] filter_params.merge!(genres: genres) if genres # if genres && genres.any? # genres.map!{|genre| {id: genre} } # filter_params.merge!(genres: genres) # end beginner = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_beginner]) intermediate = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_intermediate]) expert = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_expert]) proficiency_levels = [] proficiency_levels.push(1) if beginner proficiency_levels.push(2) if intermediate proficiency_levels.push(3) if expert instruments = params[:instruments] #debugger if instruments && instruments.any? && proficiency_levels.any? inst = [] instruments.each do |ii| proficiency_levels.each do |pl| inst << { id: ii[:value], proficiency: pl} end end filter_params.merge!(instruments: inst) end filter_params.merge!(joined_within_days: params[:joined_within_days]) unless params[:joined_within_days].blank? filter_params.merge!(active_within_days: params[:active_within_days]) unless params[:active_within_days].blank? @latency_data = [] begin #bm = Benchmark.measure do @latency_data = users_latency_data(latency_good, latency_fair, latency_high, filter_params, page, limit) user_ids = @latency_data.map{ |l_data| l_data[:user_id] } #end # Bugsnag.notify("search_users_benchmark") do |report| # report.severity = "info" # report.add_tab(:benchmark, benchmark: bm.to_s) # end if Rails.env.production? sobj = MusicianSearch.user_search_filter(current_user) @search = sobj.search_results_page(filter_params, page, user_ids) respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' rescue => exception logger.debug("Latency exception: #{exception.message}") Bugsnag.notify(exception) do |report| report.severity = "error" report.add_tab(:latency, { params: params, user_id: current_user.id, name: current_user.name, url: filter_latency_url, }) end render json: {}, status: 500 end end private def filter_latency_url "#{Rails.application.config.latency_data_host}/search_users" end end