Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Terminal *live* Text Graph In Php, Unix Terminal

DZone's Guide to

Terminal *live* Text Graph In Php, Unix Terminal

·
Free Resource
I often run php scripts from the command line in linux and need a way to view time series of data, often I need to see the number of mysql queries or want to view other stats.
That look like the following, it moves from right to left in real time as the data points change. 

+local mysql: 565 queries/sec 189----------------------+
 |                                                      |
 |                                       %              |
 |                                       %              |
 |                                       %              |
 |                                       %              |
 |                                       %              |
 |                   %                   %              |
 | %   %%   %% % %  %%%%%                % %%        %% |
 |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% %%%%%%  %%%%%|
 |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|
+-------------------------------------high: 100, low: 1+

Just copy and past the following code into a file called "termgraph.php" and change the mysql password (if needed) and change the database name (if needed as it it set to work with the default mysql install), then run it and see, very easy to modify this to work with whatever you need. In addition, you can also do colored graphs in terminals using another function I am going to post here.

This needs a couple changes to work with your mysql database, but the graphs are fun to watch, hope you enjoy this.



   $oldval){
            if($pkey == true){
                $ret[$key] = $val;
            }else{
                $ret[] = $val;
            }
        }
        $oldval = $val;
    }
    return $ret;
}
/*
    take two arrays, remove the flatspots in the first.
    return an array containing the first with it's corresponding
    values in the second array..
    -joeldg
*/
function dual_remove_flatspots($arr1, $arr2, $pkey=true){
    while(list($key,$val) = each($arr1)){
        if($val <> $oldval){
            if($pkey == true){
                $ret[0][$key] = $val;
                $ret[1][$key] = $arr2[$key];
            }else{
                $ret[0][] = $val;
                $ret[1][] = $arr2[$key];
            }
        }
        $oldval = $val;
    }
    return $ret;
}

// return lowest val of array -joeldg
function least($inarr){
    $ret = $inarr[0];
    for($i=0;$i
  
    ""){
        array_reverse($arr);
        $end = count($arr)-1;
        reset($arr);
        while(list($key,$val)=each($arr)){
            $newarr[] = $arr[$key];
            $end--;
            if($end <= 0){break;}
        }
        #array_reverse($newarr);
        $arr=$newarr;
    }
    #print_R($arr);
    $bottom = "high: ".most($arr).", low: ".least($arr);
    $arr = normalize($arr, 0, $total-2);
    #$out[] = genchars($border, $total);
    for($a=0;$a
   
     ($hw['w'] - 6)){
		array_shift($testarr_z);
	}
	$testarr = normalize($testarr_z, 1, 100);

	$testarr = remove_flatspots($testarr, false);
	$tottime = $time_end - $start;
	$qps = round($qcount/$tottime);
	echo print_horz_graph($testarr, $hw['h']-3, "-", "local mysql: $qcount queries/sec $qps", $hw['w']-1);

}

?>

   
  
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}