100% non-blocking using our awesome API

This commit is contained in:
DL6ER
2016-12-23 13:39:29 +01:00
parent 0656526d43
commit 1009dba240
5 changed files with 54 additions and 47 deletions
+4
View File
@@ -80,6 +80,10 @@
$data = array_merge($data, getGravity());
}
if (isset($_GET['tailLog'])) {
$data = array_merge($data, tailPiholeLog($_GET['tailLog']));
}
function filterArray(&$inArray) {
$outArray = array();
foreach ($inArray as $key=>$value) {
+27
View File
@@ -293,6 +293,33 @@
return $allQueries;
}
function tailPiholeLog($param) {
// Not using SplFileObject here, since direct
// usage of f-streams will be much faster for
// files as large as the pihole.log
global $logListName;
$file = fopen($logListName,"r");
$offset = intval($param);
if($offset > 0)
{
// Seeks on the file pointer where we want to continue reading is known
fseek($file, $offset);
$lines = [];
while (!feof($file)) {
array_push($lines,fgets($file));
}
return ["offset" => ftell($file), "lines" => $lines];
}
else
{
// Locate the current position of the file read/write pointer
fseek($file, -1, SEEK_END);
// Add one to skip the very last "\n" in the log file
return ["offset" => ftell($file)+1];
}
fclose($file);
}
/******** Private Members ********/
function gravityCount() {
global $gravityListName,$blackListFile;
+22 -23
View File
@@ -1,30 +1,29 @@
var source;
function eventsource() {
var ta = $("#output");
source = new EventSource("php/taillog.php");
var offset, timer, pre;
ta.html("");
ta.show();
// Check every 200msec for fresh data
var interval = 200;
source.addEventListener("message", function(e) {
if(e.data.indexOf("Ctrl-C") === -1)
{
// Hide the "Press Ctrl-C to exit" message
ta.append(e.data);
}
}, false);
// Will be called when script has finished
source.addEventListener("error", function(e) {
source.close();
}, false);
// Function that asks the API for new data
function reloadData(){
clearTimeout(timer);
$.getJSON("api.php?tailLog="+offset, function (data)
{
offset = data["offset"];
pre.append(data["lines"]);
console.log(offset);
});
timer = setTimeout(reloadData, interval);
}
$(function(){
eventsource();
// Get offset at first loading of page
$.getJSON("api.php?tailLog", function (data)
{
offset = data["offset"];
console.log(offset);
});
pre = $("#output");
// Trigger function that looks for new data
reloadData();
});
$(window).unload(function(){
source.close();
});
-23
View File
@@ -1,23 +0,0 @@
<?php
require "password.php";
if(!$auth) die("Not authorized");
ob_end_flush();
ini_set("output_buffering", "0");
ob_implicit_flush(true);
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
function echoEvent($datatext) {
echo "data: ".implode("\ndata: ", explode("\n", $datatext))."\n\n";
}
// echoEvent("***START***");
$proc = popen("sudo pihole -t", 'r');
while (!feof($proc)) {
echoEvent(fread($proc, 4096));
}
// echoEvent("***END***");
?>
+1 -1
View File
@@ -6,7 +6,7 @@
<h1>Output the last lines of the pihole.log file (live)</h1>
</div>
<pre id="output" style="width: 100%; height: 100%;" hidden="true"></pre>
<pre id="output" style="width: 100%; height: 100%;"></pre>
<?php
require "footer.php";