diff --git a/data/interfaces/default/js/tables/history_table.js b/data/interfaces/default/js/tables/history_table.js index 1cafd218..516ed0bc 100644 --- a/data/interfaces/default/js/tables/history_table.js +++ b/data/interfaces/default/js/tables/history_table.js @@ -18,7 +18,7 @@ history_table_options = { "lengthMenu":"Show _MENU_ entries per page", "info":"Showing _START_ to _END_ of _TOTAL_ history items", "infoEmpty":"Showing 0 to 0 of 0 entries", - "infoFiltered":"(filtered from _MAX_ total entries)", + "infoFiltered":"", "emptyTable": "No data in table" }, "pagingType": "bootstrap", @@ -265,6 +265,9 @@ history_table_options = { createChildTable(this, rowData) } }); + + $("#history_table_info").append(''); }, "preDrawCallback": function(settings) { var msg = " Fetching rows..."; diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 6dcdef26..4d7a4fdb 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -45,7 +45,7 @@ class DataFactory(object): 'platform', 'player', 'ip_address', - 'session_history_metadata.media_type', + 'session_history.media_type', 'session_history_metadata.rating_key', 'session_history_metadata.parent_rating_key', 'session_history_metadata.grandparent_rating_key', @@ -80,7 +80,7 @@ class DataFactory(object): ['session_history.id', 'session_history_media_info.id']], kwargs=kwargs) except: - logger.warn("Unable to execute database query.") + logger.warn("Unable to execute database query for get_history.") return {'recordsFiltered': 0, 'recordsTotal': 0, 'draw': 0, @@ -89,8 +89,13 @@ class DataFactory(object): history = query['result'] + filter_duration = 0 + total_duration = self.get_total_duration(custom_where=custom_where) + rows = [] for item in history: + filter_duration += int(item['duration']) + if item["media_type"] == 'episode' and item["parent_thumb"]: thumb = item["parent_thumb"] elif item["media_type"] == 'episode': @@ -144,7 +149,9 @@ class DataFactory(object): dict = {'recordsFiltered': query['filteredCount'], 'recordsTotal': query['totalCount'], 'data': rows, - 'draw': query['draw'] + 'draw': query['draw'], + 'filter_duration': helpers.human_duration(filter_duration, sig='dhm'), + 'total_duration': helpers.human_duration(total_duration, sig='dhm') } return dict @@ -1083,3 +1090,26 @@ class DataFactory(object): ip_address = item['ip_address'] return ip_address + + def get_total_duration(self, custom_where=None): + monitor_db = database.MonitorDatabase() + + # Split up custom wheres + if custom_where: + where = 'WHERE ' + ' AND '.join([w[0] + ' = "' + w[1] + '"' for w in custom_where]) + else: + where = '' + + try: + query = 'SELECT SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - ' \ + 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS total_duration ' \ + 'FROM session_history %s ' % where + result = monitor_db.select(query) + except: + logger.warn("Unable to execute database query for get_total_duration.") + return None + + for item in result: + total_duration = item['total_duration'] + + return total_duration \ No newline at end of file diff --git a/plexpy/helpers.py b/plexpy/helpers.py index b4f86f49..6319f423 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -146,7 +146,7 @@ def now(): now = datetime.datetime.now() return now.strftime("%Y-%m-%d %H:%M:%S") -def human_duration(s): +def human_duration(s, sig='dhms'): hd = '' @@ -157,20 +157,24 @@ def human_duration(s): s = int(((s % 84600) % 3600) % 60) hd_list = [] - if d > 0: + if sig >= 'd' and d > 0: + d = d + 1 if sig == 'd' and h >= 12 else d hd_list.append(str(d) + ' days') - if h > 0: + + if sig >= 'dh' and h > 0: + h = h + 1 if sig == 'dh' and m >= 30 else h hd_list.append(str(h) + ' hrs') - if m > 0: + + if sig >= 'dhm' and m > 0: + m = m + 1 if sig == 'dhm' and s >= 30 else m hd_list.append(str(m) + ' mins') - if s > 0: + + if sig >= 'dhms' and s > 0: hd_list.append(str(s) + ' secs') hd = ' '.join(hd_list) - return hd - else: - return hd + return hd def get_age(date): diff --git a/plexpy/webserve.py b/plexpy/webserve.py index ff52d69e..2251b85e 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -617,7 +617,7 @@ class WebInterface(object): if 'media_type' in kwargs: media_type = kwargs.get('media_type', "") if media_type != 'all': - custom_where.append(['session_history_metadata.media_type', media_type]) + custom_where.append(['session_history.media_type', media_type]) data_factory = datafactory.DataFactory() history = data_factory.get_history(kwargs=kwargs, custom_where=custom_where, grouping=grouping, watched_percent=watched_percent)