Update get_history query to use the new reference_id

This commit is contained in:
Jonathan Wong 2015-09-16 11:37:19 -07:00
parent c1b5514789
commit 881142d4a1
3 changed files with 57 additions and 106 deletions

View file

@ -245,7 +245,7 @@ history_table_options = {
history_table.rows().every(function () { history_table.rows().every(function () {
var rowData = this.data(); 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 // if grouped row and a child table was already created
this.child(childTableFormat(rowData)).show(); this.child(childTableFormat(rowData)).show();
createChildTable(this, rowData) createChildTable(this, rowData)
@ -267,7 +267,7 @@ history_table_options = {
// toggle the parent button to danger // toggle the parent button to danger
$(row).find('button[data-id="' + rowData['id'] + '"]').toggleClass('btn-warning').toggleClass('btn-danger'); $(row).find('button[data-id="' + rowData['id'] + '"]').toggleClass('btn-warning').toggleClass('btn-danger');
// check if any child rows are not selected // 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); var index = $.inArray(i, history_to_delete);
if (index == -1) { if (index == -1) {
// if any child row is not selected, toggle parent button to warning // 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 // if grouped row and a child table was already created
$(row).addClass('shown') $(row).addClass('shown')
history_table.row(row).child(childTableFormat(rowData)).show(); 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 grouped rows
if ($(this).hasClass('btn-warning')) { if ($(this).hasClass('btn-warning')) {
// add all grouped rows to history_to_delete // 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); var index = $.inArray(i, history_to_delete);
if (index == -1) { if (index == -1) {
history_to_delete.push(i); history_to_delete.push(i);
@ -361,7 +361,7 @@ $('#history_table').on('click', '> tbody > tr > td.delete-control > button', fun
} }
} else { } else {
// remove all grouped rows to history_to_delete // 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); var index = $.inArray(i, history_to_delete);
if (index != -1) { if (index != -1) {
history_to_delete.splice(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 () { $('div.slider', row.child()).slideUp(function () {
row.child.hide(); row.child.hide();
tr.removeClass('shown'); tr.removeClass('shown');
delete history_child_table[rowData['group_start_id']]; delete history_child_table[rowData['reference_id']];
}); });
} else { } else {
tr.addClass('shown'); tr.addClass('shown');
@ -412,7 +412,7 @@ function childTableOptions(rowData) {
return { return {
'json_data': JSON.stringify(d), 'json_data': JSON.stringify(d),
'grouping': false, '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 // Format the detailed history child table
function childTableFormat(rowData) { function childTableFormat(rowData) {
return '<div class="slider">' + return '<div class="slider">' +
'<table id="history_child-' + rowData['group_start_id'] + '">' + '<table id="history_child-' + rowData['reference_id'] + '">' +
'<thead>' + '<thead>' +
'<tr>' + '<tr>' +
'<th align="left" id="delete_row">Delete</th>' + '<th align="left" id="delete_row">Delete</th>' +
@ -476,23 +476,23 @@ history_child_table = {};
function createChildTable(row, rowData) { function createChildTable(row, rowData) {
history_child_options = childTableOptions(rowData); history_child_options = childTableOptions(rowData);
// initialize the child table // 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 // Set child table column visibility to match parent table
var visibility = history_table.columns().visible(); var visibility = history_table.columns().visible();
for (var i = 0; i < visibility.length; i++) { 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) { history_table.on('column-visibility', function (e, settings, colIdx, visibility) {
if (row.child.isShown()) { 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 // 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 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(); var childRowData = childRow.data();
function showStreamDetails() { function showStreamDetails() {
@ -510,9 +510,9 @@ function createChildTable(row, rowData) {
}); });
// Child table ip address modal // 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 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(); var childRowData = childRow.data();
function getUserLocation(ip_address) { function getUserLocation(ip_address) {
@ -533,9 +533,9 @@ function createChildTable(row, rowData) {
}); });
// Child table delete mode // 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 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(); var childRowData = childRow.data();
// add or remove row from history_to_delete // 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'); 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 // 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); var index = $.inArray(i, history_to_delete);
if (index == -1) { if (index == -1) {
// if any child row is not selected, toggle parent button to warning // if any child row is not selected, toggle parent button to warning

View file

@ -29,101 +29,52 @@ class DataFactory(object):
def get_history(self, kwargs=None, custom_where=None, grouping=0): def get_history(self, kwargs=None, custom_where=None, grouping=0):
data_tables = datatables.DataTables() data_tables = datatables.DataTables()
group_by = 'group_start_id' if grouping else 'id' group_by = ['session_history.reference_id'] if grouping else ['session_history.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) '
columns = ['group_start_id', columns = ['reference_id',
'id', 'session_history.id',
'date', 'started AS date',
'MIN(started) AS started', 'MIN(started) AS started',
'MAX(stopped) AS stopped', 'MAX(stopped) AS stopped',
'SUM(duration) - SUM(paused_counter) AS duration', 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - \
'SUM(paused_counter) AS paused_counter', SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS duration',
'user_id', 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter',
'session_history.user_id',
'user', 'user',
'friendly_name', '(CASE WHEN users.friendly_name IS NULL THEN user ELSE users.friendly_name END) as friendly_name',
'player', 'player',
'ip_address', 'ip_address',
'media_type', 'session_history_metadata.media_type',
'rating_key', 'session_history_metadata.rating_key',
'parent_rating_key', 'session_history_metadata.parent_rating_key',
'grandparent_rating_key', 'session_history_metadata.grandparent_rating_key',
'full_title', 'session_history_metadata.full_title',
'parent_title', 'session_history_metadata.parent_title',
'year', 'session_history_metadata.year',
'media_index', 'session_history_metadata.media_index',
'parent_media_index', 'session_history_metadata.parent_media_index',
'thumb', 'session_history_metadata.thumb',
'parent_thumb', 'session_history_metadata.parent_thumb',
'grandparent_thumb', 'session_history_metadata.grandparent_thumb',
'percent_complete', '((CASE WHEN view_offset IS NULL THEN 0.1 ELSE view_offset * 1.0 END) / \
'video_decision', (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' 'COUNT(*) AS group_count'
] ]
try: try:
query = data_tables.ssp_query(table_name=from_table, query = data_tables.ssp_query(table_name='session_history',
columns=columns, columns=columns,
custom_where=custom_where, custom_where=custom_where,
group_by=[group_by], group_by=group_by,
join_types=[], join_types=['LEFT OUTER JOIN',
join_tables=[], '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) kwargs=kwargs)
except: except:
logger.warn("Unable to execute database query.") logger.warn("Unable to execute database query.")
@ -144,7 +95,7 @@ class DataFactory(object):
else: else:
thumb = item["thumb"] thumb = item["thumb"]
row = {"group_start_id": item["group_start_id"], row = {"reference_id": item["reference_id"],
"id": item["id"], "id": item["id"],
"date": item["date"], "date": item["date"],
"started": item["started"], "started": item["started"],

View file

@ -593,9 +593,9 @@ class WebInterface(object):
if 'start_date' in kwargs: if 'start_date' in kwargs:
start_date = kwargs.get('start_date', "") start_date = kwargs.get('start_date', "")
custom_where = [['strftime("%Y-%m-%d", datetime(date, "unixepoch", "localtime"))', start_date]] custom_where = [['strftime("%Y-%m-%d", datetime(date, "unixepoch", "localtime"))', start_date]]
if 'group_start_id' in kwargs: if 'reference_id' in kwargs:
group_start_id = kwargs.get('group_start_id', "") reference_id = kwargs.get('reference_id', "")
custom_where = [['group_start_id', int(group_start_id)]] custom_where = [['reference_id', reference_id]]
data_factory = datafactory.DataFactory() data_factory = datafactory.DataFactory()
history = data_factory.get_history(kwargs=kwargs, custom_where=custom_where, grouping=grouping) history = data_factory.get_history(kwargs=kwargs, custom_where=custom_where, grouping=grouping)