File: 0.02.16a/server/web/test.graph.php (View as HTML)

  1: <?php // test.graph.php
  2: ob_start();
  3: /* -------------------------------------------------------------
  4: This file is part of FreeNATS
  5: 
  6: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  7: 
  8: FreeNATS is free software: you can redistribute it and/or modify
  9: it under the terms of the GNU General Public License as published by
 10: the Free Software Foundation, either version 3 of the License, or
 11: (at your option) any later version.
 12: 
 13: FreeNATS is distributed in the hope that it will be useful,
 14: but WITHOUT ANY WARRANTY; without even the implied warranty of
 15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16: GNU General Public License for more details.
 17: 
 18: You should have received a copy of the GNU General Public License
 19: along with Foobar.  If not, see www.gnu.org/licenses
 20: 
 21: For more information see www.purplepixie.org/freenats
 22: -------------------------------------------------------------- */
 23: 
 24: require("include.php");
 25: $NATS->Start();
 26: 
 27: if (!$NATS_Session->Check($NATS->DB))
 28: 	{
 29: 	header("Location: ./?login_msg=Invalid+Or+Expired+Session");
 30: 	exit();
 31: 	}
 32: if ($NATS_Session->userlevel<1) UL_Error("View Test History");
 33: 
 34: 
 35: function ty($y)
 36: {
 37: global $height;
 38: return $height-$y;
 39: }
 40: 
 41: 
 42: // width height suid startx finishx
 43: 
 44: if (isset($_REQUEST['width'])) $width=$_REQUEST['width'];
 45: else $width=700;
 46: if (isset($_REQUEST['height'])) $width=$_REQUEST['height'];
 47: else $height=150;
 48: // start image
 49: $im=@imagecreate($width,$height)
 50: 	or die("Cannot create image");
 51: 
 52: // setup colours
 53: $c_white=imagecolorallocate($im,255,255,255);
 54: $c_black=imagecolorallocate($im,0,0,0);
 55: $c_red=imagecolorallocate($im,250,50,50);
 56: $c_axes=imagecolorallocate($im,200,200,200);
 57: $c_blue=imagecolorallocate($im,150,150,255);
 58: $c_green=imagecolorallocate($im,0,200,0);
 59: $c_lightgreen=imagecolorallocate($im,150,250,150);
 60: $c_grey=imagecolorallocate($im,150,150,150);
 61: $c_orange=imagecolorallocate($im,200,200,0);
 62: 
 63: $c_bg=$c_white;
 64: $c_txt=$c_black;
 65: 
 66: // fill background
 67: imagefill($im,1,1,$c_bg);
 68: 	
 69: function ierror($t)
 70: {
 71: global $im,$width,$height,$c_red;
 72: header("Content-type: image/png");
 73: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$c_red);
 74: imagepng($im);
 75: imagedestroy($im);
 76: exit();
 77: }
 78: 
 79: 
 80: if (!isset($_REQUEST['nodeid'])) ierror("No node ID");
 81: else $nodeid=$_REQUEST['nodeid'];
 82: 
 83: $day=date("d");
 84: $month=date("m");
 85: $year=date("Y");
 86: 
 87: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
 88: else
 89: 	{ // 0:00 today    HMS mo da yr
 90: 	$startx=mktime(0,0,0,$month,$day,$year);
 91: 	//$startx=1203451396;
 92: 	}
 93: 	
 94: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
 95: else $finishx=mktime(23,59,59,$month,$day,$year);
 96: //$finishx=1203454996;
 97: $periodx=$finishx-$startx;
 98: $startt=date("H:i:s d/m/y",$startx);
 99: $finisht=date("H:i:s d/m/y",$finishx);
100: 
101: 
102: // titles and stuff
103: imagestring($im,2,2,2,$startt,$c_txt);
104: // -90 for size 1
105: imagestring($im,2,$width-108,2,$finisht,$c_txt);
106: imagestring($im,4,($width/2)-40,2,$nodeid,$c_txt);
107: 
108: // v-axes
109: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
110: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
111: // y-axes
112: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
113: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
114: $xoff=50+1;
115: $xlen=$width-$xoff-5;
116: 
117: $yoff=1;
118: $ylen=$height-20;
119: 
120: //ierror("hello");
121: 
122: // range data
123: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
124: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
125: $q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
126: $r=$NATS->DB->Query($q);
127: //ierror($q);
128: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
129: $lowest=$row['testvalue'];
130: $dlow=$lowest;
131: if ($dlow>0) $dlow=0;
132: $NATS->DB->Free($r);
133: 
134: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
135: 
136: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($_REQUEST['finishx'])." ";
137: 
138: $q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
139: 
140: $r=$NATS->DB->Query($q);
141: $row=$NATS->DB->Fetch_Array($r);
142: $highest=$row['testvalue'];
143: $dhigh=$highest;
144: $NATS->DB->Free($r);
145: 
146: $drange=$dhigh-$dlow;
147: 
148: // calculate scales
149: $xscale=$xlen/$periodx;
150: if ($drange>0) $yscale=$ylen/$drange;
151: else $yscale=1; // doesn't display but no change!
152: 
153: function posx($time) // timex
154: {
155: global $xscale,$startx,$xoff;
156: $drawx=$xscale*($time-$startx);
157: $screenx=$drawx+$xoff;
158: return $screenx;
159: }
160: 
161: function posy($value) // stock value
162: {
163: global $yscale,$dlow,$yoff;
164: $drawy=$yscale*($value-$dlow);
165: $screeny=$drawy+$yoff;
166: return $screeny;
167: }
168: 
169: 
170: 
171: // show axes scales
172: imagestring($im,1,2,ty(10),$dlow,$c_txt);
173: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
174: 
175: 
176: 
177: // get data and draw
178: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
179: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx);
180: //$q.="LIMIT 0,100";
181: $r=$NATS->DB->Query($q);
182: $lastx=0;
183: $lasty=0;
184: 
185: $startval=0;
186: $finishval=0;
187: 
188: while ($row=mysql_fetch_array($r))
189: 	{
190: 	$x=posx($row['recordx']);
191: 	$y=posy($row['testvalue']);
192: 	
193: 	if ($row['alertlevel']==-1) $c=$c_grey;
194: 	else if ($row['alertlevel']==0) $c=$c_lightgreen;
195: 	else if ($row['alertlevel']==1) $c=$c_orange;
196: 	else if ($row['alertlevel']==2) $c=$c_red;
197: 	else $c=$c_black;
198: 	
199: 	// up lines
200: 	imageline($im,$x,ty(0),$x,ty($y),$c);
201: 
202: 
203: 	if ($lastx!=0)
204: 		{	
205: 		// join-the-dots
206: 		imageline($im,$lastx,ty($lasty),$x,ty($y),$c_blue);
207: 		
208: 		}	
209: 	
210: 	//imageellipse($im,$x,ty($y),1,1,$c_red);
211: 	
212: 	$lastx=$x;
213: 	$lasty=$y;
214: 	
215: 	}
216: mysql_free_result($r);
217: 
218: 	
219: 
220: // output image
221: header("Content-type: image/png");
222: imagepng($im);
223: imagedestroy($im);
224: exit(); // just in case
225: 
226: ?>
227: