Optimize and order dashboard list

This commit is contained in:
Ruud committed 2013-09-06 20:03:34 +02:00
1 parent bc94e90994
commit 1b6bf13619
1 file changed
+41 -39
+41 -39
View File
@@ -4,8 +4,9 @@ from couchpotato.core.event import fireEvent
from couchpotato.core.helpers.variable import splitString, tryInt from couchpotato.core.helpers.variable import splitString, tryInt
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin from couchpotato.core.plugins.base import Plugin
from couchpotato.core.settings.model import Movie from couchpotato.core.settings.model import Movie, Library, LibraryTitle
from sqlalchemy.orm import joinedload_all from sqlalchemy.orm import joinedload_all
from sqlalchemy.sql.expression import asc
import random as rndm import random as rndm
import time import time
@@ -48,64 +49,65 @@ class Dashboard(Plugin):
# Get all active movies # Get all active movies
active_status = fireEvent('status.get', ['active'], single = True) active_status = fireEvent('status.get', ['active'], single = True)
active = db.query(Movie) \ q = db.query(Movie) \
.join(Library) \
.filter(Movie.status_id == active_status.get('id')) \ .filter(Movie.status_id == active_status.get('id')) \
.all() .with_entities(Movie.id, Movie.profile_id, Library.info, Library.year) \
all_movie_ids = [r.id for r in active] .group_by(Movie.id)
if not random:
q = q.join(LibraryTitle) \
.filter(LibraryTitle.default == True) \
.order_by(asc(LibraryTitle.simple_title))
active = q.all()
# Do the shuffle # Do the shuffle
if random: if random:
rndm.shuffle(all_movie_ids) rndm.shuffle(active)
group_limit = limit * 5 movie_ids = []
group_offset = 0 for movie in active:
movies = [] movie_id, profile_id, info, year = movie
while group_offset < len(all_movie_ids) and len(movies) < limit: pp = profile_pre.get(profile_id)
if not pp: continue
movie_ids = all_movie_ids[group_offset:group_offset + group_limit] eta = info.get('release_date', {}) or {}
group_offset += group_limit coming_soon = False
# Only joined needed # Theater quality
q = db.query(Movie) \ if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, year, single = True):
.options(joinedload_all('library')) \ coming_soon = True
.filter(Movie.id.in_(movie_ids)) elif pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, year, single = True):
all_movies = q.all() coming_soon = True
for movie in all_movies: if coming_soon:
pp = profile_pre.get(movie.profile_id)
if not pp: continue
eta = movie.library.info.get('release_date', {}) or {} # Don't list older movies
coming_soon = False if ((not late and (not eta.get('dvd') and not eta.get('theater') or eta.get('dvd') and eta.get('dvd') > (now - 2419200))) or
(late and (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200))):
movie_ids.append(movie_id)
# Theater quality if len(movie_ids) >= limit:
if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, movie.library.year, single = True): break
coming_soon = True
elif pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, movie.library.year, single = True):
coming_soon = True
if coming_soon:
# Don't list older movies
if ((not late and (not eta.get('dvd') and not eta.get('theater') or eta.get('dvd') and eta.get('dvd') > (now - 2419200))) or
(late and (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200))):
movies.append(movie.id)
if len(movies) >= limit:
break
# Get all movie information # Get all movie information
movies_raw = db.query(Movie) \ movies_raw = db.query(Movie) \
.options(joinedload_all('library.titles')) \ .options(joinedload_all('library.titles')) \
.options(joinedload_all('library.files')) \ .options(joinedload_all('library.files')) \
.options(joinedload_all('files')) \ .options(joinedload_all('files')) \
.filter(Movie.id.in_(movies)) \ .filter(Movie.id.in_(movie_ids)) \
.all() .all()
# Create dict by movie id
movie_dict = {}
for movie in movies_raw:
movie_dict[movie.id] = movie
movies = [] movies = []
for r in movies_raw: for movie_id in movie_ids:
movies.append(r.to_dict({ movies.append(movie_dict[movie_id].to_dict({
'library': {'titles': {}, 'files':{}}, 'library': {'titles': {}, 'files':{}},
'files': {}, 'files': {},
})) }))