diff --git a/data/interfaces/default/js/tables/history_table.js b/data/interfaces/default/js/tables/history_table.js index dcd94933..053f994e 100644 --- a/data/interfaces/default/js/tables/history_table.js +++ b/data/interfaces/default/js/tables/history_table.js @@ -245,7 +245,7 @@ history_table_options = { history_table.rows().every(function () { var rowData = this.data(); - if (rowData['group_count'] != 1 && rowData['group_start_id'] in history_child_table) { + if (rowData['group_count'] != 1 && rowData['reference_id'] in history_child_table) { // if grouped row and a child table was already created this.child(childTableFormat(rowData)).show(); createChildTable(this, rowData) @@ -267,7 +267,7 @@ history_table_options = { // toggle the parent button to danger $(row).find('button[data-id="' + rowData['id'] + '"]').toggleClass('btn-warning').toggleClass('btn-danger'); // check if any child rows are not selected - for (var i = rowData['group_start_id']; i <= rowData['id']; i++) { + for (var i = rowData['reference_id']; i <= rowData['id']; i++) { var index = $.inArray(i, history_to_delete); if (index == -1) { // if any child row is not selected, toggle parent button to warning @@ -277,7 +277,7 @@ history_table_options = { } } - if (rowData['group_count'] != 1 && rowData['group_start_id'] in history_child_table) { + if (rowData['group_count'] != 1 && rowData['reference_id'] in history_child_table) { // if grouped row and a child table was already created $(row).addClass('shown') history_table.row(row).child(childTableFormat(rowData)).show(); @@ -348,7 +348,7 @@ $('#history_table').on('click', '> tbody > tr > td.delete-control > button', fun // if grouped rows if ($(this).hasClass('btn-warning')) { // add all grouped rows to history_to_delete - for (var i = rowData['group_start_id']; i <= rowData['id']; i++) { + for (var i = rowData['reference_id']; i <= rowData['id']; i++) { var index = $.inArray(i, history_to_delete); if (index == -1) { history_to_delete.push(i); @@ -361,7 +361,7 @@ $('#history_table').on('click', '> tbody > tr > td.delete-control > button', fun } } else { // remove all grouped rows to history_to_delete - for (var i = rowData['group_start_id']; i <= rowData['id']; i++) { + for (var i = rowData['reference_id']; i <= rowData['id']; i++) { var index = $.inArray(i, history_to_delete); if (index != -1) { history_to_delete.splice(index, 1); @@ -386,7 +386,7 @@ $('#history_table').on('click', '> tbody > tr > td.expand-history a', function ( $('div.slider', row.child()).slideUp(function () { row.child.hide(); tr.removeClass('shown'); - delete history_child_table[rowData['group_start_id']]; + delete history_child_table[rowData['reference_id']]; }); } else { tr.addClass('shown'); @@ -412,7 +412,7 @@ function childTableOptions(rowData) { return { 'json_data': JSON.stringify(d), 'grouping': false, - 'group_start_id': rowData['group_start_id'] + 'reference_id': rowData['reference_id'] }; } } @@ -449,7 +449,7 @@ function childTableOptions(rowData) { // Format the detailed history child table function childTableFormat(rowData) { return '
' + - '' + + '
' + '' + '' + '' + @@ -476,23 +476,23 @@ history_child_table = {}; function createChildTable(row, rowData) { history_child_options = childTableOptions(rowData); // initialize the child table - history_child_table[rowData['group_start_id']] = $('#history_child-' + rowData['group_start_id']).DataTable(history_child_options); + history_child_table[rowData['reference_id']] = $('#history_child-' + rowData['reference_id']).DataTable(history_child_options); // Set child table column visibility to match parent table var visibility = history_table.columns().visible(); for (var i = 0; i < visibility.length; i++) { - if (!(visibility[i])) { history_child_table[rowData['group_start_id']].column(i).visible(visibility[i]); } + if (!(visibility[i])) { history_child_table[rowData['reference_id']].column(i).visible(visibility[i]); } } history_table.on('column-visibility', function (e, settings, colIdx, visibility) { if (row.child.isShown()) { - history_child_table[rowData['group_start_id']].column(colIdx).visible(visibility); + history_child_table[rowData['reference_id']].column(colIdx).visible(visibility); } }); // Child table platform modal - $('#history_child-' + rowData['group_start_id']).on('click', 'td.modal-control', function () { + $('#history_child-' + rowData['reference_id']).on('click', 'td.modal-control', function () { var tr = $(this).closest('tr'); - var childRow = history_child_table[rowData['group_start_id']].row(tr); + var childRow = history_child_table[rowData['reference_id']].row(tr); var childRowData = childRow.data(); function showStreamDetails() { @@ -510,9 +510,9 @@ function createChildTable(row, rowData) { }); // Child table ip address modal - $('#history_child-' + rowData['group_start_id']).on('click', 'td.modal-control-ip', function () { + $('#history_child-' + rowData['reference_id']).on('click', 'td.modal-control-ip', function () { var tr = $(this).closest('tr'); - var childRow = history_child_table[rowData['group_start_id']].row(tr); + var childRow = history_child_table[rowData['reference_id']].row(tr); var childRowData = childRow.data(); function getUserLocation(ip_address) { @@ -533,9 +533,9 @@ function createChildTable(row, rowData) { }); // Child table delete mode - $('#history_child-' + rowData['group_start_id']).on('click', 'td.delete-control > button', function () { + $('#history_child-' + rowData['reference_id']).on('click', 'td.delete-control > button', function () { var tr = $(this).closest('tr'); - var childRow = history_child_table[rowData['group_start_id']].row(tr); + var childRow = history_child_table[rowData['reference_id']].row(tr); var childRowData = childRow.data(); // add or remove row from history_to_delete @@ -549,7 +549,7 @@ function createChildTable(row, rowData) { tr.parents('tr').prev().find('td.delete-control > button.btn-warning').toggleClass('btn-warning').toggleClass('btn-danger'); // check if any child rows are not selected - for (var i = rowData['group_start_id']; i <= rowData['id']; i++) { + for (var i = rowData['reference_id']; i <= rowData['id']; i++) { var index = $.inArray(i, history_to_delete); if (index == -1) { // if any child row is not selected, toggle parent button to warning diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 0ae6f576..50696f83 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -29,101 +29,52 @@ class DataFactory(object): def get_history(self, kwargs=None, custom_where=None, grouping=0): data_tables = datatables.DataTables() - group_by = 'group_start_id' if grouping else 'id' - - from_table = '(SELECT ' \ - ' /* Session info */ ' \ - ' (CASE ' \ - ' /* IF rating_key AND user are NOT EQUAL to previous row */ ' \ - ' WHEN t1.rating_key <> ( ' \ - ' SELECT rating_key FROM session_history WHERE id = ( ' \ - ' SELECT MAX(id) FROM session_history WHERE id < t1.id)) ' \ - ' AND t1.user <> ( ' \ - ' SELECT user FROM session_history WHERE id = ( ' \ - ' SELECT MAX(id) FROM session_history WHERE id < t1.id)) ' \ - ' /* THEN select the row */ ' \ - ' THEN t1.id ' \ - ' /* IF rating_key OR user are NOT EQUAL to previous row */ ' \ - ' WHEN ( ' \ - ' SELECT MIN(id) FROM session_history WHERE id > ( ' \ - ' SELECT MAX(id) FROM session_history ' \ - ' WHERE (rating_key <> t1.rating_key OR user <> t1.user) AND id < t1.id)) IS NULL /* First row */ ' \ - ' /* THEN select the first row */ ' \ - ' THEN (SELECT MIN(id) FROM session_history) ' \ - ' /* ELSE select the row where the rating key or user changed */ ' \ - ' ELSE (SELECT MIN(id) FROM session_history ' \ - ' WHERE id > (SELECT MAX(id) FROM session_history ' \ - ' WHERE (rating_key <> t1.rating_key OR user <> t1.user) AND id < t1.id)) ' \ - ' END) AS group_start_id, ' \ - ' t1.id, ' \ - ' t1.started as date, ' \ - ' t1.started, ' \ - ' t1.stopped, ' \ - ' (CASE WHEN t1.stopped > 0 THEN (t1.stopped - t1.started) ELSE 0 END) AS duration, ' \ - ' (CASE WHEN t1.paused_counter IS NULL THEN 0 ELSE t1.paused_counter END) AS paused_counter, ' \ - ' /* User and player info */ ' \ - ' t1.user_id, ' \ - ' t1.user, ' \ - ' (CASE WHEN t2.friendly_name IS NULL THEN t1.user ELSE t2.friendly_name END) as friendly_name, ' \ - ' t1.player, ' \ - ' t1.ip_address, ' \ - ' /* Metadata info */ ' \ - ' t3.media_type, ' \ - ' t3.rating_key, ' \ - ' t3.parent_rating_key, ' \ - ' t3.grandparent_rating_key, ' \ - ' t3.full_title, ' \ - ' t3.parent_title, ' \ - ' t3.year, ' \ - ' t3.media_index, ' \ - ' t3.parent_media_index, ' \ - ' t3.thumb, ' \ - ' t3.parent_thumb, ' \ - ' t3.grandparent_thumb, ' \ - ' /* Stream info */ ' \ - ' ((CASE WHEN t1.view_offset IS NULL THEN 0.1 ELSE t1.view_offset * 1.0 END) / ' \ - ' (CASE WHEN t3.duration IS NULL THEN 1.0 ELSE t3.duration * 1.0 END) * 100) as percent_complete, ' \ - ' t4.video_decision ' \ - 'FROM session_history AS t1 ' \ - ' LEFT OUTER JOIN users AS t2 ON t1.user_id = t2.user_id ' \ - ' JOIN session_history_metadata AS t3 ON t1.id = t3.id ' \ - ' JOIN session_history_media_info AS t4 ON t1.id = t4.id) ' + group_by = ['session_history.reference_id'] if grouping else ['session_history.id'] - columns = ['group_start_id', - 'id', - 'date', + columns = ['reference_id', + 'session_history.id', + 'started AS date', 'MIN(started) AS started', 'MAX(stopped) AS stopped', - 'SUM(duration) - SUM(paused_counter) AS duration', - 'SUM(paused_counter) AS paused_counter', - 'user_id', + '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 duration', + 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter', + 'session_history.user_id', 'user', - 'friendly_name', + '(CASE WHEN users.friendly_name IS NULL THEN user ELSE users.friendly_name END) as friendly_name', 'player', 'ip_address', - 'media_type', - 'rating_key', - 'parent_rating_key', - 'grandparent_rating_key', - 'full_title', - 'parent_title', - 'year', - 'media_index', - 'parent_media_index', - 'thumb', - 'parent_thumb', - 'grandparent_thumb', - 'percent_complete', - 'video_decision', + 'session_history_metadata.media_type', + 'session_history_metadata.rating_key', + 'session_history_metadata.parent_rating_key', + 'session_history_metadata.grandparent_rating_key', + 'session_history_metadata.full_title', + 'session_history_metadata.parent_title', + 'session_history_metadata.year', + 'session_history_metadata.media_index', + 'session_history_metadata.parent_media_index', + 'session_history_metadata.thumb', + 'session_history_metadata.parent_thumb', + 'session_history_metadata.grandparent_thumb', + '((CASE WHEN view_offset IS NULL THEN 0.1 ELSE view_offset * 1.0 END) / \ + (CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 ELSE session_history_metadata.duration * 1.0 END) * 100) AS percent_complete', + 'session_history_media_info.video_decision', 'COUNT(*) AS group_count' ] try: - query = data_tables.ssp_query(table_name=from_table, + query = data_tables.ssp_query(table_name='session_history', columns=columns, custom_where=custom_where, - group_by=[group_by], - join_types=[], - join_tables=[], + group_by=group_by, + join_types=['LEFT OUTER JOIN', + 'JOIN', + 'JOIN'], + join_tables=['users', + 'session_history_metadata', + 'session_history_media_info'], + join_evals=[['session_history.user_id', 'users.user_id'], + ['session_history.id', 'session_history_metadata.id'], + ['session_history.id', 'session_history_media_info.id']], kwargs=kwargs) except: logger.warn("Unable to execute database query.") @@ -144,7 +95,7 @@ class DataFactory(object): else: thumb = item["thumb"] - row = {"group_start_id": item["group_start_id"], + row = {"reference_id": item["reference_id"], "id": item["id"], "date": item["date"], "started": item["started"], diff --git a/plexpy/webserve.py b/plexpy/webserve.py index d61355c2..d0c1c5d9 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -593,9 +593,9 @@ class WebInterface(object): if 'start_date' in kwargs: start_date = kwargs.get('start_date', "") custom_where = [['strftime("%Y-%m-%d", datetime(date, "unixepoch", "localtime"))', start_date]] - if 'group_start_id' in kwargs: - group_start_id = kwargs.get('group_start_id', "") - custom_where = [['group_start_id', int(group_start_id)]] + if 'reference_id' in kwargs: + reference_id = kwargs.get('reference_id', "") + custom_where = [['reference_id', reference_id]] data_factory = datafactory.DataFactory() history = data_factory.get_history(kwargs=kwargs, custom_where=custom_where, grouping=grouping)
Delete