Log viewer: download all (filtered) items. closes https://github.com/opnsense/core/issues/4026

- replace grid export with download link generation
- add endpoint /api/diagnostics/log/{{module}}/{{scope}}/export?searchPhrase={{filter}}
This commit is contained in:
Ad Schellevis 2020-04-08 22:04:03 +02:00
parent fb9f963879
commit 4bdb4cfeb4
2 changed files with 24 additions and 27 deletions

View File

@ -43,25 +43,23 @@ class LogController extends ApiControllerBase
$module = substr($name, 0, strlen($name) - 6);
$scope = count($arguments) > 0 ? $arguments[0] : "";
$action = count($arguments) > 1 ? $arguments[1] : "";
$searchPhrase = '';
// create filter to sanitize input data
$filter = new Filter();
$filter->add('query', new QueryFilter());
$backend = new Backend();
if ($this->request->isPost() && substr($name, -6) == 'Action') {
$this->sessionClose();
$backend = new Backend();
if ($action == "clear") {
$backend->configdpRun("system clear log", array($module, $scope));
return ["status" => "ok"];
} else {
// create filter to sanitize input data
$filter = new Filter();
$filter->add('query', new QueryFilter());
// fetch query parameters (limit results to prevent out of memory issues)
$itemsPerPage = $this->request->getPost('rowCount', 'int', 9999);
$currentPage = $this->request->getPost('current', 'int', 1);
if ($this->request->getPost('searchPhrase', 'string', '') != "") {
$searchPhrase = $filter->sanitize($this->request->getPost('searchPhrase'), "query");
} else {
$searchPhrase = '';
}
$response = $backend->configdpRun("system diag log", array($itemsPerPage,
@ -74,6 +72,19 @@ class LogController extends ApiControllerBase
return $result;
}
}
} elseif ($this->request->isGet() && substr($name, -6) == 'Action') {
if ($action == "export") {
if ($this->request->get('searchPhrase', 'string', '') != "") {
$searchPhrase = $filter->sanitize($this->request->get('searchPhrase'), "query");
}
$response = $backend->configdpRun("system diag log", array(0, 0, $searchPhrase, $module, $scope));
$this->response->setRawHeader("Content-Type: text/csv");
$this->response->setRawHeader("Content-Disposition: attachment; filename=".$scope.".log");
foreach (json_decode($response, true)['rows'] as $row) {
printf("%s\t%s\n", $row['timestamp'], $row['line']);
}
return;
}
}
return array();
}

View File

@ -57,27 +57,13 @@
}]
});
});
// download visible items
// download (filtered) items
$("#exportbtn").click(function(event){
let records = [];
$("#grid-log > tbody > tr").each(function(){
let fields = [];
$(this).find("td").each(function(){fields.push($(this).text().trim())});
records.push(fields.join("\t"));
});
let output_data = records.join("\n");
$('<a></a>').attr('id','downloadFile')
.attr('href','data:text/csv;charset=utf8,' + encodeURIComponent(output_data))
.attr('download','{{scope}}.log')
.appendTo('body');
$('#downloadFile').ready(function() {
if ( window.navigator.msSaveOrOpenBlob && window.Blob ) {
var blob = new Blob( [ output_data ], { type: "text/csv" } );
navigator.msSaveOrOpenBlob( blob, '{{scope}}.log' );
} else {
$('#downloadFile').get(0).click();
}
});
let download_link = "/api/diagnostics/log/{{module}}/{{scope}}/export";
if ($("input.search-field").val() !== "") {
download_link = download_link + "?searchPhrase=" + encodeURIComponent($("input.search-field").val());
}
$('<a></a>').attr('href',download_link).get(0).click();
});
});