1

I am trying to find a way to take an array of keywords that are user createda and filter a JSON array, and have the results appended to the screen.

Below is the actual code I am using.

var keywords= [];
var interval = "";
var pointer = '';
var scroll = document.getElementById("tail_print");

$("#filter_button").click(
function(){
    var id = $("#filter_box").val(); 
    if(id == "--Text--" || id == ""){
        alert("Please enter text before searching.");
    }else{
        keywords.push(id);
        $("#keywords-row").append("<td><img src=\"images/delete.png\" class=\"delete_filter\" /> " + id + "</td>");
    }
}
);

$(".delete_filter").click(
function(){
   ($(this)).remove(); 
}
);

function startTail(){
clearInterval(interval);
interval = setInterval(
function(){
    $.getJSON("ajax.php?function=tail&pointer=" + pointer + "&nocache=" + new Date(),
        function(data){
            pointer = data.pointer;
            $("#tail_print").append(data.log);
            scroll.scrollTop = scroll.scrollHeight;
        });
}, 1000);
}

PHP code that pulls the tailing info:

function tail(){
    $file = "/path/to/the/log/filelog.log";
    $handle = fopen($file, "r");
    clearstatcache();       

    if ($_REQUEST['pointer'] == '') {
        fseek($handle, -1024, SEEK_END);
    } else {
        fseek($handle, $_REQUEST['pointer']);
    }

    while ($buffer = fgets($handle)) { 
        $log .= $buffer . "<br />\n";
    } 

    $output = array("pointer" => ftell($handle), "log" => $log);
    fclose($handle);

    echo json_encode($output);
}

The whole purpose of this is to allow the user to filter log results. So the user performs an action that starts startTail() and $.getJSON() retrieves a JSON object that is built by a PHP function and prints the results. Works flawlessly. Now I want to give the user the option to filter the incoming tailing items. The user clicks a filter button and jQuery takes the filter text and adds it to the keywords array then the data.log from the JSON object is filtered using the keywords array and then appended to the screen.

I also have a delete filter function that isn't working. Maybe someone can help me with that.

2 Answers 2

1

The delete event handler should be deleting the parent td element

$(".delete_filter").click(
  function(){
    $(this).parent().remove(); 
  }
);

You might also want to remove the keyword from the keywords array when you do that.

As for filtering the logs, pass along the keywords array to the server where it can be filtered. That would save on the datasize returned by the server. If you need to do client side, provide a sample response to indicate to the structure of the data returned.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems like this tool is constantly getting more results. You would probably want to filter the results out on server-side to reduce bandwidth and client-side load. You may also want to filter the previous results, which you could do in javascript. Without a little more info about the data, there is little we can recommend.

I'm not sure what the data looks like in the log, but I'll make up an example:

1/12/2012 login user_name success

1/12/2012 login user_name fail

Your keyword might be success (you don't want to see the ones that succeed). So in your while loop, check if the entry has the keywords (success) and only add it if it does not.

   while ($buffer = fgets($handle)) { 
      //if $buffer does not contain keywords
            $log .= $buffer . "<br />\n";
    } 

excuse my pseudo code, I don't write php.

2 Comments

Thanks. I added the PHP code to help. If you think that I should filter on the server-side then how would i modify my PHP function to do so?
What does the data look like in the log file? What are you filtering out? You'd want to pass the keywords to the server, and if a given log entry matched them, you would not include it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.