File:
0.04.20a/server/web/test.graph.php (
View as Code)
1: 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 FreeNATS. 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:
28:
29:
30: function ty($y)
31: {
32: global $height;
33: return $height-$y;
34: }
35:
36: //if (!isset($_REQUEST['testid'])) $_REQUEST['testid']="L26";
37:
38: // width height suid startx finishx
39:
40: if (isset($_REQUEST['width'])) $width=$_REQUEST['width'];
41: else $width=700;
42: if (isset($_REQUEST['height'])) $height=$_REQUEST['height'];
43: else $height=150;
44:
45: // other incoming stuff
46: if (isset($_REQUEST['draw_spike'])) $draw_spike=$_REQUEST['draw_spike'];
47: else $draw_spike=1;
48: if (isset($_REQUEST['draw_track'])) $draw_track=$_REQUEST['draw_track'];
49: else $draw_track=1;
50: if (isset($_REQUEST['draw_under'])) $draw_under=$_REQUEST['draw_under'];
51: else $draw_under=1;
52: if (isset($_REQUEST['draw_under_pass'])) $draw_under=$_REQUEST['draw_under_pass'];
53: else $draw_under_pass=0;
54:
55:
56: // start image
57: $im=@imagecreate($width,$height)
58: or die("Cannot create image");
59:
60: // setup colours
61: $col=array();
62: $col['white']=imagecolorallocate($im,255,255,255);
63: $col['black']=imagecolorallocate($im,0,0,0);
64: $col['red']=imagecolorallocate($im,250,50,50);
65: $col['lightgrey']=imagecolorallocate($im,200,200,200);
66: $col['blue']=imagecolorallocate($im,150,150,255);
67: $col['green']=imagecolorallocate($im,0,200,0);
68: $col['lightgreen']=imagecolorallocate($im,150,250,150);
69: $col['grey']=imagecolorallocate($im,150,150,150);
70: $col['orange']=imagecolorallocate($im,200,200,0);
71:
72: if (isset($_REQUEST['bgcol'])) $c_bg=$col[$_REQUEST['bgcol']];
73: else $c_bg=$col['white'];
74:
75: if (isset($_REQUEST['txtcol'])) $c_txt=$col[$_REQUEST['txtcol']];
76: else $c_txt=$col['black'];
77:
78: if (isset($_REQUEST['axescol'])) $c_axes=$col[$_REQUEST['axescol']];
79: else $c_axes=$col['lightgrey'];
80:
81: if (isset($_REQUEST['trackcol'])) $c_track=$col[$_REQUEST['trackcol']];
82: else $c_track=$col['blue'];
83:
84: // fill background
85: imagefill($im,1,1,$c_bg);
86:
87: function ierror($t)
88: {
89: global $im,$width,$height,$col;
90: header("Content-type: image/png");
91: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$col['red']);
92: imagepng($im);
93: imagedestroy($im);
94: exit();
95: }
96:
97: //ierror("Test");
98:
99: if ( $NATS->Cfg->Get("site.graph.public",0)!=1 ) // private so must authenticate
100: {
101: if (!$NATS_Session->Check($NATS->DB))
102: {
103: ierror("Authorisation Failure");
104: exit();
105: }
106: if ($NATS_Session->userlevel<1) ierror("User Disabled");
107: }
108: else // public so use site.graph.key if set
109: {
110: $key=$NATS->Cfg->Get("site.graph.key","");
111: if ( ($key!="") && ( (!isset($_REQUEST['graphkey'])) || ($_REQUEST['graphkey']!=$key) ) )
112: { // there IS a key AND either graphkey is not provided or doesn't match
113: ierror("Graph Key Failure");
114: exit();
115: }
116: }
117:
118: if (!isset($_REQUEST['nodeid'])) $nodeid="";
119: else $nodeid=$_REQUEST['nodeid'];
120:
121: if (!isset($_REQUEST['testid'])) ierror("No test ID");
122:
123: $day=date("d");
124: $month=date("m");
125: $year=date("Y");
126:
127: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
128: else
129: { // 0:00 today HMS mo da yr
130: $startx=mktime(0,0,0,$month,$day,$year);
131: //$startx=1203451396;
132: }
133:
134: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
135: else $finishx=mktime(23,59,59,$month,$day,$year);
136:
137: if ($startx<=0) $startx=time()+$startx;
138: if ($finishx<=0) $finishx=time()+$finishx;
139:
140: //$finishx=1203454996;
141: $periodx=$finishx-$startx;
142: $startt=date("H:i:s d/m/y",$startx);
143: $finisht=date("H:i:s d/m/y",$finishx);
144:
145:
146: // titles and stuff
147: imagestring($im,2,2,2,$startt,$c_txt);
148: // -90 for size 1
149: imagestring($im,2,$width-108,2,$finisht,$c_txt);
150:
151: if (isset($_REQUEST['title'])) $title=$_REQUEST['title'];
152: else $title=$nodeid;
153: $len=strlen($title)*4;
154: imagestring($im,4,($width/2)-$len,2,$title,$c_txt);
155:
156: // offsets and lengths
157: $xoff=50+1;
158: $xlen=$width-$xoff-5;
159:
160: $yoff=1;
161: $ylen=$height-$yoff-20;
162:
163:
164: // v-axes
165: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
166: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
167: // y-axes
168: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
169: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
170: //ierror("hello");
171:
172:
173: // range data
174:
175: // Lowest
176:
177: if (isset($_REQUEST['rangemin'])) $dlow=$_REQUEST['rangemin'];
178: else
179: {
180: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
181: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
182: $q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
183: $r=$NATS->DB->Query($q);
184: //ierror($q);
185: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
186: $lowest=$row['testvalue'];
187: $dlow=$lowest;
188: if ($dlow>0) $dlow=0;
189: $NATS->DB->Free($r);
190: }
191:
192:
193: // Highest
194:
195: if (isset($_REQUEST['rangemax'])) $dhigh=$_REQUEST['rangemax'];
196: else
197: {
198: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
199: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
200: $q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
201:
202: $r=$NATS->DB->Query($q);
203: $row=$NATS->DB->Fetch_Array($r);
204: $highest=$row['testvalue'];
205: $dhigh=$highest;
206: $NATS->DB->Free($r);
207: if (isset($_REQUEST['rangemaxmin']))
208: {
209: if ($dhigh<$_REQUEST['rangemaxmin']) $dhigh=$_REQUEST['rangemaxmin'];
210: }
211: }
212:
213: $drange=$dhigh-$dlow;
214:
215: // calculate scales
216: $xscale=$xlen/$periodx;
217: if ($drange>0) $yscale=$ylen/$drange;
218: else $yscale=1; // doesn't display but no change!
219:
220: function posx($time) // timex
221: {
222: global $xscale,$startx,$xoff;
223: $drawx=$xscale*($time-$startx);
224: $screenx=$drawx+$xoff;
225: return $screenx;
226: }
227:
228: function posy($value)
229: {
230: global $yscale,$dlow,$yoff,$dhigh;
231: if ($value>$dhigh) $value=$dhigh;
232: $drawy=$yscale*($value-$dlow);
233: $screeny=$drawy+$yoff;
234: return $screeny;
235: }
236:
237:
238:
239: // show axes scales
240: imagestring($im,1,2,ty(10),$dlow,$c_txt);
241: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
242:
243:
244: if (isset($_REQUEST['units']))
245: {
246: if (strpos($_REQUEST['units'],"/")===false)
247: imagestring($im,1,2,ty($height-28),$_REQUEST['units'],$c_txt);
248:
249:
250: else
251: {
252: $unit_array=explode("/",$_REQUEST['units']);
253: $a=0;
254: foreach($unit_array as $unit_string)
255: {
256: imagestring($im,1,2,ty($height-28-($a*8)),$unit_string,$c_txt);
257: $a++;
258: }
259: }
260: }
261:
262:
263: // get data and draw
264: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
265: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ORDER BY recordx ASC";
266: //$q.="LIMIT 0,100";
267: $r=$NATS->DB->Query($q);
268: $lastx=0;
269: $lasty=0;
270:
271: $startval=0;
272: $finishval=0;
273:
274: while ($row=mysql_fetch_array($r))
275: {
276: $x=posx($row['recordx']);
277: $y=posy($row['testvalue']);
278:
279: if ($row['alertlevel']==-1) $c=$col['grey'];
280: else if ($row['alertlevel']==0) $c=$col['lightgreen'];
281: else if ($row['alertlevel']==1) $c=$col['orange'];
282: else if ($row['alertlevel']==2) $c=$col['red'];
283: else $c=$col['black'];
284:
285: // up lines
286: if ($draw_spike==1) imageline($im,$x,ty(0),$x,ty($y),$c);
287:
288:
289:
290: if ($lastx!=0)
291: {
292:
293: // join-the-dots
294: if ($draw_track==1) imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
295:
296: // fill -- DOES NOT WORK
297: //if (($draw_fill==1)&&($draw_spike==1)&&($draw_track==1))
298: //imagefill($im,$x-1,ty(1),$c);
299:
300: // bottom line
301: if ($draw_under==1)
302: {
303: if (($row['alertlevel']>0)||
304: (($row['alertlevel']==0)&&($draw_under_pass==1)) )
305: {
306: imageline($im,$lastx,ty(1),$x,ty(1),$c);
307: imageline($im,$lastx,ty(2),$x,ty(2),$c);
308: }
309: }
310: }
311:
312: //imageellipse($im,$x,ty($y),1,1,$c_red);
313:
314: $lastx=$x;
315: $lasty=$y;
316:
317: }
318: mysql_free_result($r);
319:
320:
321:
322: // output image
323: header("Content-type: image/png");
324: imagepng($im);
325: imagedestroy($im);
326: exit(); // just in case
327:
328: ?>
329: