From 91a664016e9282a3432bb7d4c7688e6afd3067bd Mon Sep 17 00:00:00 2001 From: Hipster Hitman Date: Tue, 12 Apr 2011 06:46:07 +0400 Subject: [PATCH] mongodb select tracks optimized --- app/controllers/artist_controller.rb | 13 +++++++++++-- app/models/album.rb | 25 +++++++++++++++++++++++++ app/models/track.rb | 5 +---- app/views/artist/view.rhtml | 26 +++++++++++--------------- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 06289e8..50360b7 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -5,19 +5,28 @@ class ArtistController < ApplicationController if params[:name].nil? name = '' else - if request.request_method == 'POST' + if request.request_method == 'POST' redirect_to :action => 'view', :name => params[:name].gsub(' ', '+') end name = params[:name].gsub('+', ' ') end @artist = Artist.getByName(name) + @albums = [] + @artist.albums.each do |album| + unless album.releases.empty? + tracks = album.tracksInDb() + @albums << { + :object => album, + :tracks => tracks + } + end + end end def autocomplete autocomplete = Artist.getLastFmAutocomplete(params[:query]) return render :nothing => true if autocomplete.nil? suggestions = [] - pp autocomplete["response"] autocomplete["response"]["docs"].each do |doc| suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) end diff --git a/app/models/album.rb b/app/models/album.rb index b9866ce..45934f5 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -32,5 +32,30 @@ class Album < ActiveRecord::Base m[3][1] end end + + def tracksInDb + tracks = [] + result = [] + tracks_in_db = [] + track_ids = [] + + self.releases.first.tracks.each do |track| + tracks << track + track_ids << track.id.to_s + end + in_db = TrackData.any_in(id: track_ids).only("id") + unless in_db.nil? + in_db.each do |track| + tracks_in_db << track["id"].to_i; + end + end + tracks.each do |track| + result << { + :object => track, + :in_db => tracks_in_db.include?(track.id) ? nil : true + } + end + result + end end diff --git a/app/models/track.rb b/app/models/track.rb index 8fdf4d4..2e5b447 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -1,8 +1,5 @@ class Track < ActiveRecord::Base set_table_name 'musicbrainz.bh_track' belongs_to :release - - def inDb - ! TrackData.where(id: id.to_s).empty? - end end + diff --git a/app/views/artist/view.rhtml b/app/views/artist/view.rhtml index 0e8b8ad..c8a2073 100644 --- a/app/views/artist/view.rhtml +++ b/app/views/artist/view.rhtml @@ -5,28 +5,24 @@ <% else %>

<%= @artist.name %>

- <% @artist.albums.each do |album| %> - <% unless album.releases.empty? %> + <% @albums.each do |album| %>
-
-

<%= album.name %> <%= (album.year ? album.year : '') %>

- <% - releases = album.releases - first_release = releases.first - %> +
+

<%= album[:object].name %> <%= (album[:object].year ? album[:object].year : '') %>

- <% end %> <% end %> <% end %>