zoneminder/web/skins/classic/views/js/log.js

345 lines
10 KiB
JavaScript
Raw Normal View History

var logParms = 'view=request&request=log&task=query';
var logReq = new Request.JSON( {url: thisUrl, method: 'post', timeout: AJAX_TIMEOUT, link: 'cancel', onSuccess: logResponse} );
var logTimer = undefined;
var logTable = undefined;
var logCodes = {
'0': 'INF',
'-1': 'WAR',
'-2': 'ERR',
'-3': 'FAT',
'-4': 'PNC',
};
var minSampleTime = 2000;
var maxSampleTime = 16000;
var minLogTime = 0;
var maxLogTime = 0;
var logCount = 0;
var maxLogFetch = 100;
var filter = {};
var logTimeout = maxSampleTime;
var firstLoad = true;
var initialDisplayLimit = 200;
var sortReversed = false;
var filterFields = ['Component', 'ServerId', 'Pid', 'Level', 'File', 'Line'];
var options = {};
function escapeHtml(unsafe) {
return unsafe
2019-06-07 01:40:00 +08:00
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
2017-05-19 01:50:56 +08:00
function buildFetchParms( parms ) {
var fetchParms = logParms+'&limit='+maxLogFetch;
if ( parms ) {
2017-05-19 01:50:56 +08:00
fetchParms += '&'+parms;
}
2017-05-19 01:50:56 +08:00
Object.each(filter,
function( value, key ) {
fetchParms += '&filter['+key+']='+value;
}
);
2018-05-17 02:17:57 +08:00
return fetchParms;
}
2017-05-19 01:50:56 +08:00
function fetchNextLogs() {
logReq.send( buildFetchParms( 'minTime='+maxLogTime ) );
}
2017-05-19 01:50:56 +08:00
function fetchPrevLogs() {
logReq.send( buildFetchParms( 'maxTime='+minLogTime ) );
}
2017-05-19 01:50:56 +08:00
function logResponse( respObj ) {
if ( logTimer ) {
2017-05-19 01:50:56 +08:00
logTimer = clearTimeout( logTimer );
}
2017-05-19 01:50:56 +08:00
if ( respObj.result == 'Ok' ) {
if ( respObj.logs.length > 0 ) {
logTimeout = minSampleTime;
logCount += respObj.logs.length;
try {
respObj.logs.each(
function( log ) {
if ( ( !maxLogTime ) || ( log.TimeKey > maxLogTime ) ) {
2017-05-19 01:50:56 +08:00
maxLogTime = log.TimeKey;
}
if ( ( !minLogTime ) || ( log.TimeKey < minLogTime ) ) {
2017-05-19 01:50:56 +08:00
minLogTime = log.TimeKey;
}
var row = logTable.push([
{content: log.DateTime, properties: {style: 'white-space: nowrap'}},
log.Component, log.Server, log.Pid, log.Code,
escapeHtml(log.Message),
escapeHtml(log.File),
log.Line
]);
2017-05-19 01:50:56 +08:00
delete log.Message;
row.tr.store( 'log', log );
if ( log.Level <= -3 ) {
row.tr.addClass( 'log-fat' );
} else if ( log.Level <= -2 ) {
row.tr.addClass( 'log-err' );
} else if ( log.Level <= -1 ) {
row.tr.addClass( 'log-war' );
} else if ( log.Level > 0 ) {
row.tr.addClass( 'log-dbg' );
}
2017-05-19 01:50:56 +08:00
if ( !firstLoad ) {
var color = document.defaultView.getComputedStyle(row.tr, null).getPropertyValue('color');
var colorParts = color.match(/^rgb.*\((\d+),\s*(\d+),\s*(\d+)/);
rowOrigColor = '#' + parseInt(colorParts[1]).toString(16) + parseInt(colorParts[2]).toString(16) + parseInt(colorParts[3]).toString(16);
//new Fx.Tween( row.tr, { duration: 10000, transition: Fx.Transitions.Sine } ).start( 'color', '#6495ED', rowOrigColor );
2017-05-19 01:50:56 +08:00
}
}
2017-05-19 01:50:56 +08:00
);
if ( typeof(respObj.options) == 'object' ) {
$j.each( respObj.options,
function( field ) {
if ( options[field] ) {
options[field] = Object.assign(options[field], respObj.options[field]);
} else {
options[field] = respObj.options[field];
}
}
);
}
2017-05-19 01:50:56 +08:00
updateFilterSelectors();
$('lastUpdate').set('text', respObj.updated);
$('logState').set('text', respObj.state);
$('logState').removeClass('ok');
$('logState').removeClass('alert');
$('logState').removeClass('alarm');
$('logState').addClass(respObj.state);
$('totalLogs').set('text', respObj.total);
$('availLogs').set('text', respObj.available);
$('displayLogs').set('text', logCount);
if ( firstLoad ) {
if ( logCount < displayLimit ) {
2017-05-19 01:50:56 +08:00
fetchPrevLogs();
}
}
2017-05-19 01:50:56 +08:00
logTable.reSort();
} catch ( e ) {
2017-05-19 01:50:56 +08:00
console.error( e );
}
logTimeout /= 2;
if ( logTimeout < minSampleTime ) {
2017-05-19 01:50:56 +08:00
logTimeout = minSampleTime;
}
2017-05-19 01:50:56 +08:00
} else {
firstLoad = false;
logTimeout *= 2;
if ( logTimeout > maxSampleTime ) {
2017-05-19 01:50:56 +08:00
logTimeout = maxSampleTime;
}
} // end logs.length > 0
} // end if result == Ok
2017-05-19 01:50:56 +08:00
logTimer = fetchNextLogs.delay( logTimeout );
}
2017-05-19 01:50:56 +08:00
function refreshLog() {
options = {};
$j('#logTable tbody').empty();
2017-05-19 01:50:56 +08:00
firstLoad = true;
maxLogTime = 0;
minLogTime = 0;
logCount = 0;
logTimeout = maxSampleTime;
displayLimit = initialDisplayLimit;
fetchNextLogs();
}
2017-05-19 01:50:56 +08:00
function expandLog() {
displayLimit += maxLogFetch;
fetchPrevLogs();
}
function clearResponse() {
refreshLog();
}
function clearError() {
}
2017-05-19 01:50:56 +08:00
function clearLog() {
logReq.cancel();
var clearParms = 'view=request&request=log&task=delete';
2019-12-21 05:11:38 +08:00
var clearReq = new Request.JSON({url: thisUrl, method: 'post', timeout: AJAX_TIMEOUT, link: 'cancel', onSuccess: clearResponse});
var tbody = $(logTable).getElement('tbody');
var rows = tbody.getElements('tr');
if ( rows && rows.length ) {
var minTime = rows[0].getElement('td').get('text');
clearParms += "&minTime="+encodeURIComponent(minTime);
var maxTime = rows[rows.length-1].getElement('td').get('text');
clearParms += "&maxTime="+encodeURIComponent(maxTime);
}
var form = $('logForm');
clearReq.send(clearParms+"&"+form.toQueryString());
}
2017-05-19 01:50:56 +08:00
function filterLog() {
filter = {};
filterFields.each(
function( field ) {
var selector = $('filter['+field+']');
2019-12-21 05:11:38 +08:00
if ( !selector ) {
2017-05-19 01:50:56 +08:00
if ( window.console && window.console.log ) {
window.console.log('No selector found for ' + field);
2017-05-19 01:50:56 +08:00
}
return;
}
2017-05-19 01:50:56 +08:00
var value = selector.get('value');
if ( value ) {
2017-05-19 01:50:56 +08:00
filter[field] = value;
}
2017-05-19 01:50:56 +08:00
}
);
2017-05-19 01:50:56 +08:00
refreshLog();
}
2017-05-19 01:50:56 +08:00
function resetLog() {
filter = {};
refreshLog();
}
var exportFormValidator;
2017-05-19 01:50:56 +08:00
function exportLog() {
exportFormValidator.reset();
$('exportLog').overlayShow();
}
2017-05-19 01:50:56 +08:00
function exportResponse( response ) {
$('exportLog').unspin();
if ( response.result == 'Ok' ) {
window.location.replace( thisUrl+'?view=request&request=log&task=download&key='+response.key+'&format='+response.format );
}
}
2017-05-19 01:50:56 +08:00
function exportFail( request ) {
$('exportLog').unspin();
2019-12-21 05:11:38 +08:00
$('exportErrorText').set('text', request.status+' / '+request.statusText);
2017-05-19 01:50:56 +08:00
$('exportError').show();
2019-12-21 05:11:38 +08:00
Error('Export request failed: '+request.status+' / '+request.statusText);
}
2017-05-19 01:50:56 +08:00
function exportRequest() {
var form = $('exportForm');
$('exportErrorText').set('text', '');
2017-05-19 01:50:56 +08:00
$('exportError').hide();
if ( form.validate() ) {
var exportParms = "view=request&request=log&task=export";
2019-12-21 05:11:38 +08:00
var exportReq = new Request.JSON({url: thisUrl, method: 'post', link: 'cancel', onSuccess: exportResponse, onFailure: exportFail});
2017-05-19 01:50:56 +08:00
var selection = form.getElement('input[name=selector]:checked').get('value');
if ( selection == 'filter' || selection == 'current' ) {
$$('#filters select').each(
function( select ) {
exportParms += "&"+select.get('id')+"="+select.get('value');
}
);
2017-05-19 01:50:56 +08:00
}
if ( selection == 'current' ) {
var tbody = $(logTable).getElement( 'tbody' );
var rows = tbody.getElements( 'tr' );
if ( rows ) {
var minTime = rows[0].getElement('td').get('text');
exportParms += "&minTime="+encodeURIComponent(minTime);
var maxTime = rows[rows.length-1].getElement('td').get('text');
exportParms += "&maxTime="+encodeURIComponent(maxTime);
}
}
2019-12-21 05:11:38 +08:00
exportReq.send(exportParms+"&"+form.toQueryString());
2017-05-19 01:50:56 +08:00
$('exportLog').spin();
}
}
2017-05-19 01:50:56 +08:00
function updateFilterSelectors() {
Object.each(options,
function( values, key ) {
var selector = $('filter['+key+']');
2019-12-21 05:11:38 +08:00
if ( !selector ) {
if ( window.console && window.console.log ) {
window.console.log('No selector found for ' + key);
2017-05-19 01:50:56 +08:00
}
return;
}
selector.options.length = 1;
if ( key == 'Level' ) {
Object.each(values,
function( value, label ) {
selector.options[selector.options.length] = new Option(value, label);
}
);
} else if ( key == 'ServerId' ) {
Object.each(values,
function( value, label ) {
selector.options[selector.options.length] = new Option(value, label);
}
);
} else {
Object.each(values,
function( value, label ) {
selector.options[selector.options.length] = new Option(value, label);
}
);
}
if ( filter[key] ) {
selector.set('value', filter[key]);
}
}
2017-05-19 01:50:56 +08:00
);
}
2017-05-19 01:50:56 +08:00
function initPage() {
displayLimit = initialDisplayLimit;
for ( var i = 1; i <= 9; i++ ) {
2017-05-19 01:50:56 +08:00
logCodes[''+i] = 'DB'+i;
}
2017-05-19 01:50:56 +08:00
logTable = new HtmlTable( $('logTable'),
{
2018-05-17 02:17:57 +08:00
zebra: true,
sortable: true,
sortReverse: true
}
);
2017-05-19 01:50:56 +08:00
logTable.addEvent( 'sort', function( tbody, index ) {
var header = tbody.getParent('table').getElement('thead');
var columns = header.getElement('tr').getElements('th');
var column = columns[index];
sortReversed = column.hasClass('table-th-sort-rev');
if ( logCount > displayLimit ) {
var rows = tbody.getElements('tr');
var startIndex;
if ( sortReversed ) {
startIndex = displayLimit;
} else {
startIndex = 0;
}
for ( var i = startIndex; logCount > displayLimit; i++ ) {
rows[i].destroy();
logCount--;
}
$('displayLogs').set('text', logCount);
} // end if loCount > displayLimit
}
2017-05-19 01:50:56 +08:00
);
exportFormValidator = new Form.Validator.Inline($('exportForm'), {
useTitles: true,
warningPrefix: '',
errorPrefix: ''
2017-05-19 01:50:56 +08:00
});
new Asset.css('css/spinner.css');
2017-05-19 01:50:56 +08:00
fetchNextLogs();
}
// Kick everything off
window.addEventListener('DOMContentLoaded', initPage);