File:
1.15.0a/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: // Transparent logic (1.14.1/2)
56: // options in order of presidence (URI, Config, Default):
57: // transparent=1 in URI: transparent, transparent=0 in URI: non-transparent,
58: // site.graph.transparent=1: transparent, default: non-transparent
59: if (isset($_REQUEST['transparent']) && $_REQUEST['transparent']==1)
60: $transparent=1;
61: else if (isset($_REQUEST['transparent']) && $_REQUEST['transparent']==0)
62: $transparent=0;
63: else if ($NATS->Cfg->Get("site.graph.transparent",0)==1)
64: $transparent=1;
65: else
66: $transparent=0;
67:
68: $draw_x_grid=true;
69: $draw_y_grid=true;
70:
71: if (isset($_REQUEST['no_x_grid'])) $draw_x_grid=false;
72: if (isset($_REQUEST['no_y_grid'])) $graw_y_grid=false;
73: if (isset($_REQUEST['no_grid']))
74: {
75: $draw_x_grid=false;
76: $draw_y_grid=false;
77: }
78:
79: // start image
80: $im=@imagecreate($width,$height)
81: or die("Cannot create image");
82:
83: // setup colours
84: $col=array();
85: $col['white']=imagecolorallocate($im,255,255,255);
86: $col['black']=imagecolorallocate($im,0,0,0);
87: $col['red']=imagecolorallocate($im,250,50,50);
88: $col['lightgrey']=imagecolorallocate($im,200,200,200);
89: $col['verylightgrey']=imagecolorallocate($im,240,240,240);
90: $col['blue']=imagecolorallocate($im,150,150,255);
91: $col['green']=imagecolorallocate($im,0,200,0);
92: $col['lightgreen']=imagecolorallocate($im,150,250,150);
93: $col['grey']=imagecolorallocate($im,150,150,150);
94: $col['orange']=imagecolorallocate($im,200,200,0);
95:
96: if (isset($_REQUEST['bgcol'])) $c_bg=$col[$_REQUEST['bgcol']];
97: else $c_bg=$col['white'];
98:
99: if (isset($_REQUEST['txtcol'])) $c_txt=$col[$_REQUEST['txtcol']];
100: else $c_txt=$col['black'];
101:
102: if (isset($_REQUEST['axescol'])) $c_axes=$col[$_REQUEST['axescol']];
103: else $c_axes=$col['lightgrey'];
104:
105: if (isset($_REQUEST['trackcol'])) $c_track=$col[$_REQUEST['trackcol']];
106: else $c_track=$col['blue'];
107:
108: if (isset($_REQUEST['gridcol'])) $c_grid=$col[$_REQUEST['gridcol']];
109: else $c_grid=$col['verylightgrey'];
110:
111: // transparent check 1.14.1
112: if ($transparent == 1)
113: imagecolortransparent($im, $c_bg);
114:
115: // fill background
116: imagefill($im,1,1,$c_bg);
117:
118: function ierror($t)
119: {
120: global $im,$width,$height,$col;
121: ob_clean();
122: header("Content-type: image/png");
123: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$col['red']);
124: imagepng($im);
125: imagedestroy($im);
126: exit();
127: }
128:
129: //ierror("Test");
130:
131: $session=$NATS_Session->Check($NATS->DB);
132:
133: if (!$session)
134: {
135: if ($NATS->Cfg->Get("site.graph.public",0)!=1)
136: {
137: ierror("Authorisation Failure");
138: exit();
139: }
140: $key=$NATS->Cfg->Get("site.graph.key","");
141: if (isset($_REQUEST['graphkey'])) $userkey=$_REQUEST['graphkey'];
142: else $userkey="";
143:
144: if ( ($key!="") && ($key!=$userkey) )
145: {
146: ierror("Graph Key Failure");
147: exit();
148: }
149: }
150:
151:
152: if (!isset($_REQUEST['nodeid'])) $nodeid="";
153: else $nodeid=$_REQUEST['nodeid'];
154:
155: if (!isset($_REQUEST['testid'])) ierror("No test ID");
156:
157: $day=date("d");
158: $month=date("m");
159: $year=date("Y");
160:
161: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
162: else
163: { // 0:00 today HMS mo da yr
164: $startx=mktime(0,0,0,$month,$day,$year);
165: //$startx=1203451396;
166: }
167:
168: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
169: else $finishx=mktime(23,59,59,$month,$day,$year);
170:
171: if ($startx<=0) $startx=time()+$startx;
172: if ($finishx<=0) $finishx=time()+$finishx;
173:
174: //$finishx=1203454996;
175: $periodx=$finishx-$startx;
176: $startt=date("H:i:s d/m/y",$startx);
177: $finisht=date("H:i:s d/m/y",$finishx);
178:
179:
180: // titles and stuff
181: imagestring($im,2,2,2,$startt,$c_txt);
182: // -90 for size 1
183: imagestring($im,2,$width-108,2,$finisht,$c_txt);
184:
185: if (isset($_REQUEST['title'])) $title=$_REQUEST['title'];
186: else $title=$nodeid;
187: $len=strlen($title)*4;
188: imagestring($im,4,($width/2)-$len,2,$title,$c_txt);
189:
190: // offsets and lengths
191: $xoff=50+1;
192: $xlen=$width-$xoff-5;
193:
194: $yoff=1;
195: $ylen=$height-$yoff-20;
196:
197:
198: // v-axes
199: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
200: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
201: // y-axes
202: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
203: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
204: //ierror("hello");
205:
206:
207: // range data
208:
209: // Lowest
210:
211: if (isset($_REQUEST['rangemin'])) $dlow=$_REQUEST['rangemin'];
212: else
213: {
214: $dlow=0;
215: /*
216: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
217: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
218: $q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
219: $r=$NATS->DB->Query($q);
220: //ierror($q);
221: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
222: $lowest=$row['testvalue'];
223: $dlow=$lowest;
224: if ($dlow>0) $dlow=0;
225: $NATS->DB->Free($r);
226: */
227: }
228:
229:
230: // Highest
231:
232: if (isset($_REQUEST['rangemax'])) $dhigh=$_REQUEST['rangemax'];
233: else
234: {
235: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
236: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
237: $q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
238:
239: $r=$NATS->DB->Query($q);
240: $row=$NATS->DB->Fetch_Array($r);
241: $highest=$row['testvalue'];
242: $dhigh=$highest;
243: $NATS->DB->Free($r);
244: if (isset($_REQUEST['rangemaxmin']))
245: {
246: if ($dhigh<$_REQUEST['rangemaxmin']) $dhigh=$_REQUEST['rangemaxmin'];
247: }
248: }
249:
250: $drange=$dhigh-$dlow;
251:
252: // calculate scales
253: $xscale=$xlen/$periodx;
254: if ($drange>0) $yscale=$ylen/$drange;
255: else $yscale=1; // doesn't display but no change!
256:
257: // Grid Lines
258:
259: // Grid X - the X values
260:
261: /* Oh if only I was actually any good at programming. There MUST be soooo many better ways to do these ranges
262: with all sorts of calculations to round to the nearest power of ten etc etc. Unfortunately I have the programming
263: skill of a walrus and typing code with tusks is difficult! */
264:
265: /* TODO: Make this all like really elegant and stuff */
266:
267: if ($draw_x_grid)
268: {
269: $xg_step=1;
270: $xg_scale=1;
271: if ($drange<0.001) $xg_scale=0.0001;
272: else if ($drange<0.01) $xg_scale=0.001;
273: else if ($drange<0.1) $xg_scale=0.01;
274: else if ($drange<1) $xg_scale=0.1;
275: else if ($drange<6) $xg_scale=1;
276: else if ($drange<11) $xg_scale=2;
277: else if ($drange<16) $xg_scale=4;
278: else if ($drange<21) $xg_scale=5;
279: else if ($drange<51) $xg_scale=10;
280: else if ($drange<101) $xg_scale=20;
281: else if ($drange<201) $xg_scale=50;
282: else if ($drange<501) $xg_scale=100;
283: else if ($drange<1001) $xg_scale=250;
284: else if ($drange<10001) $xg_scale=1000;
285: else if ($drange<100001) $xg_scale=10000;
286: else if ($drange<1000001) $xg_scale=100000;
287: else $draw_x_grid=false;
288:
289: if ($xg_scale<0) $xg_step=($xg_scale);
290: }
291:
292: if ($draw_x_grid)
293: {
294: //imagestring($im,1,2,ty(50),"Drawing X Grid ".$xg_scale,$c_txt);
295: for ($a=$dlow; $a<=$dhigh; $a+=$xg_scale)
296: {
297: if (($a!=0)&&( ($a % $xg_scale) == 0))
298: {
299: //imagestring($im,1,2,ty(50+($a*5)),"Drawing X Grid ".$a,$c_txt);
300: // draw a line
301: imageline($im,posx($startx),ty(posy($a)),posx($finishx),ty(posy($a)),$c_grid);
302: // imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
303: }
304: }
305: }
306:
307: // Grid Y - the time values
308:
309: if ($draw_y_grid)
310: {
311: $min=60;
312: $hour=$min*60;
313: $day=$hour*24;
314:
315: $syr=date("Y",$startx);
316: $smo=date("m",$startx);
317: $sda=date("d",$startx);
318: $shr=date("H",$startx);
319: // h m s mo da yr
320: $lhr=mktime($shr,0,0,$smo,$sda,$syr);
321: $lda=mktime(0,0,0,$smo,$sda,$syr);
322:
323: if ($periodx< (($hour*2)+1) )
324: {
325: $yg_scale=$min;
326: $yg_start=$lhr;
327: }
328: else if ($periodx< (($day*2)+1) )
329: {
330: $yg_scale=$hour;
331: $yg_start=$lhr;
332: }
333: else if ($periodx < ($day*32) )
334: {
335: $yg_scale=$day;
336: $yg_start=$lda;
337: }
338: else $draw_y_grid=false;
339: }
340:
341: if ($draw_y_grid)
342: {
343: //imagestring($im,1,2,ty(50),"Drawing Y Grid ",$c_txt);
344: for ($a=$yg_start; $a<$finishx; $a+=$yg_scale)
345: {
346: if ($a>$startx)
347: {
348: // draw line
349: imageline($im,posx($a),ty(posy($dlow)),posx($a),ty(posy($dhigh)),$c_grid);
350: }
351: }
352: }
353: function posx($time) // timex
354: {
355: global $xscale,$startx,$xoff;
356: $drawx=$xscale*($time-$startx);
357: $screenx=$drawx+$xoff;
358: $screenx=floor($screenx);
359: return $screenx;
360: }
361:
362: function posy($value)
363: {
364: global $yscale,$dlow,$yoff,$dhigh;
365: if ($value>$dhigh) $value=$dhigh;
366: $drawy=$yscale*($value-$dlow);
367: $screeny=$drawy+$yoff;
368: $screeny=floor($screeny);
369: return $screeny;
370: }
371:
372:
373:
374: // show axes scales
375: imagestring($im,1,2,ty(10),$dlow,$c_txt);
376: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
377:
378:
379: if (isset($_REQUEST['units']))
380: {
381: if (strpos($_REQUEST['units'],"/")===false)
382: imagestring($im,1,2,ty($height-28),$_REQUEST['units'],$c_txt);
383:
384:
385: else
386: {
387: $unit_array=explode("/",$_REQUEST['units']);
388: $a=0;
389: foreach($unit_array as $unit_string)
390: {
391: imagestring($im,1,2,ty($height-28-($a*8)),$unit_string,$c_txt);
392: $a++;
393: }
394: }
395: }
396:
397:
398: // get data and draw
399: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
400: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ORDER BY recordx ASC";
401: //$q.="LIMIT 0,100";
402: $r=$NATS->DB->Query($q);
403: $lastx=0;
404: $lasty=0;
405:
406: $startval=0;
407: $finishval=0;
408:
409: while ($row=mysql_fetch_array($r))
410: {
411: $x=posx($row['recordx']);
412: //$y=posy($row['testvalue']);
413: $val=$row['testvalue'];
414: if ($val<0) $y=posy(0);
415: else $y=posy($val);
416:
417: if ($row['alertlevel']==-1) $c=$col['grey'];
418: else if ($row['alertlevel']==0) $c=$col['lightgreen'];
419: else if ($row['alertlevel']==1) $c=$col['orange'];
420: else if ($row['alertlevel']==2) $c=$col['red'];
421: else $c=$col['black'];
422:
423: // up lines
424: if ($draw_spike==1) imageline($im,$x,ty(0),$x,ty($y),$c);
425:
426:
427:
428: if ($lastx!=0)
429: {
430:
431: // join-the-dots
432: if ($draw_track==1) imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
433:
434: // fill -- DOES NOT WORK
435: //if (($draw_fill==1)&&($draw_spike==1)&&($draw_track==1))
436: //imagefill($im,$x-1,ty(1),$c);
437:
438: // bottom line
439: if ($draw_under==1)
440: {
441: if (($row['alertlevel']>0)||
442: (($row['alertlevel']==0)&&($draw_under_pass==1)) )
443: {
444: imageline($im,$lastx,ty(1),$x,ty(1),$c);
445: imageline($im,$lastx,ty(2),$x,ty(2),$c);
446: }
447: }
448: }
449:
450: //imageellipse($im,$x,ty($y),1,1,$c_red);
451:
452: $lastx=$x;
453: $lasty=$y;
454:
455: }
456: mysql_free_result($r);
457:
458:
459:
460: // output image
461: ob_clean();
462: header("Content-type: image/png");
463: imagepng($im);
464: imagedestroy($im);
465: exit(); // just in case
466:
467: ?>
468: