1
0
Fork 0

mongodb select tracks optimized

This commit is contained in:
Hipster Hitman 2011-04-12 06:46:07 +04:00
parent fbf7a8480a
commit 91a664016e
4 changed files with 48 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -5,28 +5,24 @@
</div>
<% else %>
<h1 class="artist"><%= @artist.name %></h1>
<% @artist.albums.each do |album| %>
<% unless album.releases.empty? %>
<% @albums.each do |album| %>
<div class="album">
<div class="pic"><img src="<%= album.cover(@artist.name) %>" width="250" height="250" alt=""/></div>
<h3 class="name"><%= album.name %> <%= (album.year ? album.year : '') %></h3>
<%
releases = album.releases
first_release = releases.first
%>
<div class="pic"><img src="<%= album[:object].cover(@artist.name) %>" width="250" height="250" alt=""/></div>
<h3 class="name"><%= album[:object].name %> <%= (album[:object].year ? album[:object].year : '') %></h3>
<ul class="tracks">
<% first_release.tracks.each do |track| %>
<li id="<%= track.id %>">
<span class="play<%= (track.inDb ? '' : ' disabled') %>"><img src="/images/player/play.svg" alt="play" /></span>
<span class="track-name"><%= track.name %></span>
<% unless track.length.nil? %>
<span class="duration" data-length="<%= track.length %>"><%= track.length.toTime %></span>
<% album[:tracks].each do |track| %>
<li id="<%= track[:object].id %>">
<span class="play<%= (track[:in_db].nil? ? '' : ' disabled') %>">
<img src="/images/player/play.svg" alt="play" />
</span>
<span class="track-name"><%= track[:object].name %></span>
<% unless track[:object].length.nil? %>
<span class="duration" data-length="<%= track[:object].length %>"><%= track[:object].length.toTime %></span>
<% end %>
</li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
<% end %>