Commit 9e3d8621 authored by echel0n's avatar echel0n

Added SQL queries to speed up accessing episode stats

parent 0ab02957
......@@ -372,76 +372,102 @@ class TVShow(object):
@property
def airs_next(self):
_airs_next = datetime.date.min
for episode_object in self.episodes:
if episode_object.season != 0 and episode_object.status in [UNAIRED, WANTED]:
if episode_object.airdate >= datetime.date.today() and _airs_next == datetime.date.min:
_airs_next = episode_object.airdate
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
query = show_query.episodes.filter(
MainDB.TVEpisode.season != 0,
MainDB.TVEpisode.airdate >= datetime.date.today(),
MainDB.TVEpisode.status.in_([UNAIRED, WANTED])
).order_by(
MainDB.TVEpisode.airdate
).first()
if query:
_airs_next = query.airdate
return _airs_next
@property
def airs_prev(self):
_airs_prev = datetime.date.min
for episode_object in self.episodes:
if episode_object.season != 0 and episode_object.status != UNAIRED:
if episode_object.airdate < datetime.date.today() > _airs_prev:
_airs_prev = episode_object.airdate
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
query = show_query.episodes.filter(
MainDB.TVEpisode.season != 0,
MainDB.TVEpisode.airdate < datetime.date.today(),
MainDB.TVEpisode.status != UNAIRED
).order_by(
MainDB.TVEpisode.airdate
).first()
if query:
_airs_prev = query.airdate
return _airs_prev
@property
def episodes_unaired(self):
_episodes_unaired = 0
for episode_object in self.episodes:
if episode_object.season != 0 and episode_object.status == UNAIRED:
_episodes_unaired += 1
return _episodes_unaired
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season != 0,
MainDB.TVEpisode.status == UNAIRED
).count()
@property
def episodes_snatched(self):
_episodes_snatched = 0
for episode_object in self.episodes:
if episode_object.season != 0 and episode_object.status in Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER:
_episodes_snatched += 1
return _episodes_snatched
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season != 0,
MainDB.TVEpisode.status.in_(Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER)
).count()
@property
def episodes_downloaded(self):
_episodes_downloaded = 0
for episode_object in self.episodes:
if episode_object.season != 0 and episode_object.status in Quality.DOWNLOADED + Quality.ARCHIVED:
_episodes_downloaded += 1
return _episodes_downloaded
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season != 0,
MainDB.TVEpisode.status.in_(Quality.DOWNLOADED + Quality.ARCHIVED)
).count()
@property
def episodes_special(self):
_episodes_specials = 0
for episode_object in self.episodes:
if episode_object.season == 0:
_episodes_specials += 1
return _episodes_specials
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season == 0
).count()
@property
def episodes_special_unaired(self):
_episodes_specials_unaired = 0
for episode_object in self.episodes:
if episode_object.season == 0 and episode_object.status == UNAIRED:
_episodes_specials_unaired += 1
return _episodes_specials_unaired
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season == 0,
MainDB.TVEpisode.status == UNAIRED
).count()
@property
def episodes_special_downloaded(self):
_episodes_special_downloaded = 0
for episode_object in self.episodes:
if episode_object.season == 0 and episode_object.status in Quality.DOWNLOADED + Quality.ARCHIVED:
_episodes_special_downloaded += 1
return _episodes_special_downloaded
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season == 0,
MainDB.TVEpisode.status.in_(Quality.DOWNLOADED + Quality.ARCHIVED)
).count()
@property
def episodes_special_snatched(self):
_episodes_special_snatched = 0
for episode_object in self.episodes:
if episode_object.season == 0 and episode_object.status in Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER:
_episodes_special_snatched += 1
return _episodes_special_snatched
with sickrage.app.main_db.session() as session:
show_query = session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id, indexer=self.indexer).one()
return show_query.episodes.filter(
MainDB.TVEpisode.season == 0,
MainDB.TVEpisode.status.in_(Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER)
).count()
@property
def new_episodes(self):
......
......@@ -22,6 +22,7 @@ from functools import cmp_to_key
import sickrage
from sickrage.core.common import Quality, get_quality_string, WANTED, UNAIRED, timeFormat, dateFormat
from sickrage.core.databases.main import MainDB
from sickrage.core.helpers.srdatetime import SRDateTime
from sickrage.core.tv.show.helpers import get_show_list
......@@ -54,7 +55,7 @@ class ComingEpisodes:
"""
def result(show, episode):
return [{
return {
'airdate': episode.airdate,
'airs': show.airs,
'description': episode.description,
......@@ -71,7 +72,7 @@ class ComingEpisodes:
'show_name': show.name,
'showid': episode.showid,
'status': show.status
}]
}
paused = sickrage.app.config.coming_eps_display_paused or paused
......@@ -94,24 +95,47 @@ class ComingEpisodes:
Quality.IGNORED
results = []
for s in get_show_list():
for e in s.episodes:
if e.season == 0:
continue
if today <= e.airdate < next_week and e.status not in qualities_list:
results += result(s, e)
if e.showid not in [int(r['showid']) for r in results] and e.airdate >= next_week and e.status \
not in Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER:
results += result(s, e)
if today > e.airdate >= recently and e.status in [WANTED, UNAIRED] and e.status not in qualities_list:
results += result(s, e)
for show in get_show_list():
with sickrage.app.main_db.session() as session:
results += [result(show, episode) for episode in session.query(
MainDB.TVEpisode
).filter_by(
showid=show.indexer_id,
indexer=show.indexer
).filter(
MainDB.TVEpisode.airdate < next_week,
MainDB.TVEpisode.airdate >= today,
MainDB.TVEpisode.season != 0,
~MainDB.TVEpisode.status.in_(qualities_list)
)]
if show.indexer_id not in [int(r['showid']) for r in results]:
results += [result(show, episode) for episode in session.query(
MainDB.TVEpisode
).filter_by(
showid=show.indexer_id,
indexer=show.indexer
).filter(
MainDB.TVEpisode.airdate >= next_week,
MainDB.TVEpisode.season != 0,
~MainDB.TVEpisode.status.in_(Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER)
)]
results += [result(show, episode) for episode in session.query(
MainDB.TVEpisode
).filter_by(
showid=show.indexer_id,
indexer=show.indexer
).filter(
MainDB.TVEpisode.airdate >= recently,
MainDB.TVEpisode.airdate < today,
MainDB.TVEpisode.season != 0,
MainDB.TVEpisode.status.in_([WANTED, UNAIRED]),
~MainDB.TVEpisode.status.in_(qualities_list)
)]
for index, item in enumerate(results):
results[index]['localtime'] = SRDateTime(
sickrage.app.tz_updater.parse_date_time(item['airdate'], item['airs'], item['network']), convert=True).dt
results[index]['localtime'] = SRDateTime(sickrage.app.tz_updater.parse_date_time(item['airdate'], item['airs'], item['network']), convert=True).dt
results.sort(key=ComingEpisodes.sorts[sort])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment