Location: PHPKode > projects > Monyet > classes/grandayplotter.php
include ("plotter/jpgraph/src/jpgraph.php");
include ("plotter/jpgraph/src/jpgraph_log.php");
include ("plotter/jpgraph/src/jpgraph_line.php");
include ("plotter/jpgraph/src/jpgraph_pie.php");
include ("plotter/jpgraph/src/jpgraph_bar.php");
include ("plotter/jpgraph/src/jpgraph_pie3d.php");

//plot a graph with daily granularity

Class GranDayPlotter{

	private $starttime;
	private $finishtime;
	//determine active window monitoring
	private $startwindow;
	private $finishwindow;
	private $table;
	private $dba;
	//the line graph to plot the actual data
	private $graph;
	//the pie graph to plot availability
	private $pieGraph;

	private $data;
	private $availdata;
	private $interval;
	private $legend;
	private $redvalue;
	private $greenvalue;
	private $map;
	//average (graphtype==avg) or full rough data (graphtype==full)
	//or undefined/null if you want button average or detail disabled
	private $graphtype; 	
	private $caller; //actual class that invoke plotter, to determine href for image maps
	private $post; //caller post
	private $title;

	private $graphAvail;

	private $maxstat;
	private $minstat;
	private $averagestat;
	private $lateststat;

	//return the downtime/totalcheckpoint
	private $availability;

	public function __construct(){
		// Setup the graph
		$this->dba = DBAccess::getInstance();
		//defaul is line graph
		$this->graphAvail = false;
		$this->interval = 1;
	public function setTitle($title){
		$this->title = $title;
	public function getData(){
			return $this->availdata;
			  return $this->data;
	public function setLegend($legend){
		$this->legend = $legend;
	//boolean value. determine wether graph should be displayed as bar availability or line detail line
	public function setGraphAvail($graphavail){
		$this->graphAvail = $graphavail;
	public function setCaller($caller,$post){
	   $this->caller = $caller;
	   $this->post = $post;
	public function setRedLine($red){
		$this->redvalue = $red;
	public function setGreenLine($green){
		$this->greenvalue = $green;

	//set the graph graphtype. avg for average and full for full rough data
	//null or undefined if you want average and detail button disabled
	public function setGraphType($graphtype){
		$this->graphtype = $graphtype;

	//please invoke after setGraph
	public function getAvailability(){
		return $this->availability;
	public function setRangeTime($st,$ft){
		$this->starttime = $st;
		$this->finishtime = $ft;
	public function parseTime(){
		//parse from start time to finish time into small daily chunk
		$stts = strtotime($this->starttime);
		$ftts = strtotime($this->finishtime);
		$diff = ($ftts - $stts)/(3600*24);
			$message .= 'tick: '.date('Y-m-d',$stts+($i*3600*24)).'<br>';

	public function setGraph($table,$st,$ft){
	   $this->starttime = $st;
		$this->finishtime = $ft;
		$this->table = $table;

		//table determine the monitoring id
		//plotter self aware

		$obj = '';

			$obj = new Ping();
			$obj = new ServiceMon();
			$objservdef = new ServicesDef();
			$this->setLegend('Service '.$objservdef->getName());
			die('currently not support for monitoring with priority id '.$table);
		$this->redvalue = $obj->getRedLine();
		$this->greenvalue = $obj->getGreenLine();
		$this->startwindow = $obj->getStartTime();
		$this->finishwindow = $obj->getFinishTime();

		//setting up the graph
		$posttime = array();
		$datay = array();
		$redline = array();
		$greenine = array();
		$this->data = array();
		$this->availdata = array();
		$availability = array();
		$peak = 0;
		//magic number here :)
		$valley = 10000;

		//time range
		$stts = strtotime($this->starttime);
		$ftts = strtotime($this->finishtime);
		$tmpobj = new StdClass();
		$diff = ($ftts - $stts)/(3600*24);
		$result = array();
			$offset = $stts+($i*24*3600);
			$q_start_time = " AND posttime >= \"".date('Y-m-d',$offset)." 00:00:00\"";
			$q_finish_time = " AND posttime <= \"".date('Y-m-d',$offset)." 23:59:59\"";
			$sql = "SELECT posttime,value FROM ".$this->table." WHERE value>=0 ".$q_start_time." ".$q_finish_time;
			$subresult = $this->dba->query($sql);	
			$total = 0;
			$count = 0;
			//windowtime parse here. can not be inserted in sql
				$tmpdate = date('G:i:s',strtotime($subresult[$j]->posttime));
					//outside the window time we just ignore the value
			$average = null;
			$tmpobj->itemid = date('Y-m-d',$offset);
			$tmpobj->posttime = date('d',$offset);
			$tmpobj->value = $average;
			$datay[] = $average;
			$posttime[] = $tmpobj->posttime;
			$redline[] = $this->redvalue; 
			$greenline[] = $this->greenvalue;
			$result[] = $tmpobj;
			if($peak < $average){
				$peak = $average;
			if($valley > $average){
				$valley = $average;
		$peak += ($peak-$valley)*0.15;
		$valley -= ($peak-$valley)*0.05;
		//if negatif pick zero as valley
		// Setup the graph
		$this->graph = new Graph(510,300);

			die('Data not big enought to be plotted. Please wait while system is collecting the data');

		$p1 = new LinePlot($datay);
			// Create red line
			$p3 = new LinePlot($redline);
			$p2 = new LinePlot($greenline);

	//similar with getrecent
	public function getLatestStat(){
		return $this->lateststat;

	public function getMaxStat(){
		return $this->maxstat;

	public function getMinStat(){
		return $this->minstat;
	public function getAverageStat(){
		return $this->averagestat;

	public function getImageMaps(){
		return '<MAP NAME="MyMap">'.$this->map.'</MAP>';

	public function attachLine(){
		//set graph
		$file = rand(2,20000).'plotline.png';
		return $file;

Return current item: Monyet