This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.php
executable file
·1689 lines (1403 loc) · 48.2 KB
/
query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?
require("amplib.php");
//require("/home/httpd/erg.wand.net.nz/html/amp/testing/bmc26/profiler.php");
/* XXX amplib.php actually sets these, though it uses the empty string if there
* is no useful value in $_REQUEST. Lets unset them for now so we can set them
* to what we expect later on. This used to work, did PHP change the behaviour
* of isset() to be true for the empty string? Or did we change amplib.php to
* add those lines?
*/
unset($src);
unset($dst);
/*
* Print an error using the appropriate formatting and terminate.
*/
function printError($format, $code, $brief, $details) {
/* actually do want to return 200 so error details get through? */
//header("HTTP/1.1 $code $brief");
switch ( $format ) {
case "xml":
$error = new SimpleXMLElement("<error/>");
$error->addChild("code", $code);
$error->addChild("message", $details);
echo $error->asXML();
break;
case "json":
$error = json_encode(array("error" =>
array("code" => $code,"message" => $details)));
echo $error;
break;
case "csv":
case "text": echo $details; break;
};
exit;
}
/*
* Print a list of sites reachable.
*/
function printSiteList($format, $siteList, $src=null) {
switch ( $format ) {
case "xml":
$response = new SimpleXMLElement("<response/>");
$sites = $response->addChild("sites");
if ( $src != null )
$sites->addAttribute("src", $src);
foreach ( $siteList as $site ) {
$sites->addChild("site", $site);
}
echo $response->asXML();
break;
case "json":
echo json_encode(array("response" => array("sites" => $siteList)));
break;
case "csv":
case "text":
if ( $src != null )
echo "# src='$src'\n";
foreach ( $siteList as $site ) {
echo "$site\n";
}
break;
};
}
/*
* Print a list of tests available
*/
function printTestList($format, $testList, $src, $dst) {
if ( $testList == NULL )
$testList = array();
switch ( $format ) {
case "xml":
$response = new SimpleXMLElement("<response/>");
$tests = $response->addChild("tests");
$tests->addAttribute("src", $src);
$tests->addAttribute("dst", $dst);
foreach ( $testList as $test ) {
$node = $tests->addChild("test");
$node->addChild("id", $test);
$node->addChild("name", getTestName($test));
}
echo $response->asXML();
break;
case "json":
$tests = array();
foreach ( $testList as $test )
$tests[$test] = getTestName($test);
/* XXX if the test ids are consecutive this makes an array,
* otherwise it makes an object. The version of PHP being used
* is too old to use JSON_FORCE_OBJECT, though it appears you
* can probably always set $assoc=true when decoding to make it
* convert everything to an array so it is consistent.
*/
echo json_encode(array("response" => array("tests" => $tests)));
break;
case "csv":
case "text":
echo "# src='$src' dst='$dst'\n";
foreach ( $testList as $test ) {
echo "$test " . getTestName($test) . "\n";
}
break;
};
}
/*
* Print a list of subtypes available for a test
*/
function printSubtypeList($format, $subtypeList, $test, $src, $dst) {
if ( $subtypeList == array() )
$subtypeList = array($test);
switch ( $format ) {
case "xml":
$response = new SimpleXMLElement("<response/>");
$subtypes = $response->addChild("subtypes");
$subtypes->addAttribute("src", $src);
$subtypes->addAttribute("dst", $dst);
$subtypes->addAttribute("test", $test);
foreach ( $subtypeList as $subtype ) {
$node = $subtypes->addChild("subtype", $subtype);
}
echo $response->asXML();
break;
case "json":
echo json_encode(array("response" =>
array("subtypes" => $subtypeList)));
break;
case "csv":
case "text":
echo "# src='$src' dst='$dst' test='$test'\n";
foreach ( $subtypeList as $subtype ) {
echo "$subtype\n";
}
break;
};
}
/*
* Data objects have some properties that we don't really care about when
* binning data, so remove them.
*/
function filterObjectVars($var) {
$illegal = array("isNumeric", "error", "time", "secInPeriod", "instance");
if ( in_array($var, $illegal) )
return false;
return true;
}
/*
* Zero all the current bin counters for everything reported by this test type
*/
function initialiseThisBin($test, $statnames) {
$thisBin = array();
foreach ( $statnames as $stat ) {
$thisBin[$stat]["binTotal"] = 0;
$thisBin[$stat]["binCount"] = 0;
$thisBin[$stat]["max"] = 0;
$thisBin[$stat]["min"] = PHP_INT_MAX;
$thisBin[$stat]["missing"] = 0;
}
return $thisBin;
}
/*
* Build a new set of instance stats with their own summary and bins to use
* for tracking data.
*/
function initialiseInstanceStats($test, $statnames, $summary) {
$result = array (
"thisBin" => array(),
"bins" => array(),
);
$result["thisBin"] = initialiseThisBin($test, $statnames);
if ( $summary ) {
$result["summary"] = array();
foreach ( $statnames as $stat ) {
$result["summary"][$stat]["mean"] = 0;
$result["summary"][$stat]["count"] = 0;
$result["summary"][$stat]["max"] = 0;
$result["summary"][$stat]["min"] = PHP_INT_MAX;
$result["summary"][$stat]["missing"] = 0;
}
}
return $result;
}
/*
* Update the current bin with the stats from the most recent object.
* Passing thisBin by reference gives a slight speed increase...
*/
function updateThisBin($obj, &$thisBin, $test, $statnames) {
foreach ( $statnames as $stat=>$statname ) {
/* XXX this wont work with an array like udpstream test uses */
$value = $obj->$stat;
if ( $value >= 0 ) {
$thisBin[$statname]["binTotal"] += $value;
$thisBin[$statname]["binCount"]++;
if ( $value > $thisBin[$statname]["max"] )
$thisBin[$statname]["max"] = $value;
if ( $value < $thisBin[$statname]["min"] )
$thisBin[$statname]["min"] = $value;
} else {
$thisBin[$statname]["missing"]++;
}
}
return $thisBin;
}
//XXX stats and outputstat need better names!
function updateFinishedBin($time, $test, &$thisBin, $statnames, $outputstat) {
$binned = array("time"=>$time);
foreach ( $statnames as $stat ) {
foreach ( $outputstat as $output ) {
$thisBinStat = $thisBin[$stat];
$finalStat = &$binned[$stat];
$finalStat["missing"] = $thisBinStat["missing"];
$finalStat["count"] = $thisBinStat["binCount"];
if ( $thisBinStat["binCount"] > 0 ) {
/* XXX loss and count here or up a level? */
switch ( $output ) {
case "mean":
/* calculate mean for all stats in the bin*/
$finalStat["mean"] =
$thisBinStat["binTotal"] /
$thisBinStat["binCount"];
break;
case "max":
$finalStat["max"] = $thisBinStat["max"];
break;
case "min":
$finalStat["min"] = $thisBinStat["min"];
break;
case "jitter":
$finalStat["jitter"] =
$thisBinStat["max"] -
$thisBinStat["min"];
break;
case "loss":
$finalStat["loss"] =
$thisBinStat["missing"] /
($thisBinStat["missing"]+
$thisBinStat["binCount"]);
break;
};
} else if ( $thisBinStat["missing"] > 0 ) {
/* time is set but no count, therefore loss */
switch ( $output ) {
case "loss": $finalStat["loss"] = 1;
break;
case "missing": break;
case "count": break;
default: $finalStat[$output] = -1;
break;
};
}
}
}
return $binned;
}
/*
* Update the most recent bin for all instances once we have a new datapoint
* that falls outside of the bin. Now is the time to update global summary
* stats, copy thisBin data into its proper location and then reinitialise it.
* Passing $instances by reference here makes this function much faster!
*/
function updateInstanceBins(&$instances, $bin, $test, $statnames,
$outputstat, $summary) {
foreach($instances as $name => &$instance) {
if ( !empty($instance["bins"][$bin]["time"]) ) {
//if ( strlen($instance["bins"][$bin]["time"]) > 0 ) {
$instance["bins"][$bin] = updateFinishedBin(
$instance["bins"][$bin]["time"], $test,
$instance["thisBin"], $statnames, $outputstat);
}
if ( $summary ) {
$instance["summary"] = updateSummary($instance["summary"],
$test, $instance["thisBin"], $statnames);
}
/* zero everything ready for the next bin */
$instance["thisBin"] = initialiseThisBin($test, $statnames);
}
return $instances;
}
/*
* Update global summary stats based on the most recently completed bin
*/
function updateSummary($summary, $test, $thisBin, $statnames) {
foreach ( $statnames as $stat ) {
/* if there were no legitimate measurements we can skip everything
* that doesn't involve counting loss
*/
$count = $thisBin[$stat]["binCount"];
if ( $count > 0 ) {
/*
* update the running mean of all values by treating every
* measurement in the bin as if it were the bin mean.
*/
for($i=0; $i<$count; $i++) {
$summary[$stat]["count"]++;
$value = $thisBin[$stat]["binTotal"]/$count;
$delta = $value - $summary[$stat]["mean"];
$summary[$stat]["mean"] += ($delta / $summary[$stat]["count"]);
}
if ( $thisBin[$stat]["max"] > $summary[$stat]["max"] )
$summary[$stat]["max"] = $thisBin[$stat]["max"];
if ( $thisBin[$stat]["min"] < $summary[$stat]["min"] )
$summary[$stat]["min"] = $thisBin[$stat]["min"];
}
//$summary[$stat]["missing"] += $thisBin[$stat]["lossCount"];
$summary[$stat]["missing"] += $thisBin[$stat]["missing"];
}
return $summary;
}
/*
*
*/
function getDataInstanceFromObject($obj) {
//if ( $obj && property_exists($obj, "instance") ) // really slow
if ( $obj && isset($obj->instance) )
return $obj->instance;
else
return "default";
}
/*
* To do "binning" of traceroutes it is simply taking the most common
* path observed during that period, in much the same way as the graphs
* only show the most common path.
*
* The getCommonPath() function also appears to return at least one path
* for any time period
*/
function getBinnedTraceValues($dataset, $start, $secOfData, $binsize) {
$paths = array("default" => array("bins" => array()));
$bin = 0;
$binStartTime = 0;
$binsize = (int)$binsize;
if ( $binsize < 1 )
$binsize = 1;
/* $info here is used as an easy way to get information about the dataset
* that was handed to us so we can open the same one, just one binsize
* into the future.
*
* getCommonPath seems to leave the dataset in a bad state when it
* completes (it is pointing to a file 2 days in the future when it
* should only have consumed a few minutes of data). Ideally we should
* just be able to loop around getCommonPath without having to reopen
* the dataset every time.
*/
$info = ampInfoObj($dataset);
while ( $binStartTime < $secOfData ) {
$dataset = ampOpenDb($info->dataType, $info->dataSubtype,
$info->src, $info->dst, $start+$binStartTime, 0, "UTC");
$paths["default"]["bins"][$bin]["time"] = $start+$binStartTime;
$paths["default"]["bins"][$bin]["path"] =
getCommonPath($dataset, $binsize);
$bin++;
$binStartTime += $binsize;
}
return $paths;
}
/*
* XXX TODO: make it so things like packetsize aren't calculated for loss!
* exclude them from all things and just have them as attributes? or just do
* it for things that make sense like max, mean etc? see what other test
* types suggest perhaps
*
* binsize is now in seconds!
*/
function getBinnedValues($dataset, $test, $start, $secOfData, $outputstat,
$binsize, $summary) {
$bin = 0;
$binStartTime = 0;
$binsize = (int)$binsize;
if ( $binsize < 0 )
$binsize = 0;
$statnames = array();
$instances = array();
/*
$prof_ampnextobj = new Profile();
$prof_is_object = new Profile();
$prof_getdatainstance = new Profile();
$prof_buildinstance = new Profile();
$prof_finishedbin = new Profile();
$prof_finishedbincheck = new Profile();
$prof_currentbin_pre = new Profile();
$prof_currentbin = new Profile();
$prof_updatethisbin = new Profile();
$prof_updateinstancebins = new Profile();
$prof_finishedA = new Profile();
$prof_finishedC = new Profile();
*/
/* Retrieve the data - Loop until bins are filled */
do {
//$prof_ampnextobj->start('ampnextobj');
/* Get a measurement */
$obj = ampNextObj($dataset);
//$prof_ampnextobj->stop();
//$prof_is_object->start('is_object');
if ( $obj == FALSE || !is_object($obj) )
break;
//$prof_is_object->stop();
/* only NZRS tests will have instances, but we need to track them
* separately. Other tests may benefit from this in the future,
* (maybe the tput test?)
*/
//$prof_getdatainstance->start('getdatainstance');
$dataInstance = getDataInstanceFromObject($obj);
$secInPeriod = $obj->secInPeriod;
//$prof_getdatainstance->stop();
//$prof_buildinstance->start('buildinstance');
/* initialise values for a new server instance */
if ( !isset($instances[$dataInstance]) &&
strlen($dataInstance) > 0/* && $dataInstance != "NULL"*/ ) {
/*
* remove the elements that every object has to find the ones
* that are specific to this test
*/
$stats = array_filter(array_keys(get_object_vars($obj)),
"filterObjectVars");
//XXX Why? why not just leave everything in the obj name as is
// until we go to print it out and then give the pretty name?
if ( $statnames == array() ) {
//$statnames = getStatName($test);
foreach($stats as $stat)
$statnames[$stat] = getStatName($test, $stat);
}
$instances[$dataInstance] =
initialiseInstanceStats($test, $statnames, $summary);
}
//$prof_buildinstance->stop();
//$prof_finishedbin->start('finishedbin');
//$prof_finishedbincheck->start('finishedbincheck');
/* if measurement is outside the bin, calculate the stats for the bin */
if ( $secInPeriod > $secOfData || $binsize == 0 ||
$secInPeriod > ($binStartTime + $binsize) ) {
//$prof_finishedbincheck->stop();
//$prof_finishedA->start("updateinstancebinsA");
if ( $binsize > 0 ) {
$binStartTime = $secInPeriod - ($secInPeriod%$binsize);
}
//$prof_finishedA->stop();
//$prof_updateinstancebins->start("updateinstancebinsB");
$instances = updateInstanceBins($instances, $bin, $test,
$statnames, $outputstat, $summary);
//$prof_updateinstancebins->stop();
//$prof_finishedC->start("updateinstancebinsC");
/* TODO bin can be incremented even if nothing is saved, care? */
$bin++;
/* Check this measurement is still in requested time period */
if ( $secInPeriod > $secOfData ) {
/* End loop if it's not */
break;
}
//$prof_finishedC->stop();
} //else $prof_finishedbincheck->stop();
//$prof_finishedbin->stop();
/* ignore loss for now, might want to record them but against
* which instance?
* XXX: maybe only ignore loss if part of the bin is lost, if the
* whole bin is lost then report it?
* XXX no instance to associate loss with in NZRS test
*/
//$prof_currentbin_pre->start('currentbin_pre');
if ( (empty($dataInstance) && strlen($dataInstance) < 1)/* ||
$dataInstance == "NULL"*/ )
continue;
/* measurement is good, record it */
$base = &$instances[$dataInstance]["bins"][$bin];
//$prof_currentbin_pre->stop();
//$prof_currentbin->start('currentbin');
/* first measurement in this bin should also record the time */
if ( !isset($base["time"]) ) {
if ( $binsize == 0 )
$base["time"] = $obj->time;
else
$base["time"] = $start + $binStartTime + ((int)($binsize/2));
}
//$prof_currentbin->stop();
//$prof_updatethisbin->start("updatethisbin");
$instances[$dataInstance]["thisBin"] = updateThisBin($obj,
$instances[$dataInstance]["thisBin"], $test, $statnames);
//$prof_updatethisbin->stop();
} while(TRUE);
/* if we get here because the object is wrong, it means we haven't
* put the last bin together properly, do so now.
*/
if ( $obj == FALSE || !is_object($obj) ) {
$instances = updateInstanceBins($instances, $bin, $test,
$statnames, $outputstat, $summary);
}
/*
echo $prof_ampnextobj->getSummary();
echo $prof_is_object->getSummary();
echo $prof_getdatainstance->getSummary();
echo $prof_buildinstance->getSummary();
echo $prof_finishedbin->getSummary();
echo $prof_finishedbincheck->getSummary();
echo $prof_updateinstancebins->getSummary();
echo $prof_finishedA->getSummary();
echo $prof_finishedC->getSummary();
echo $prof_currentbin_pre->getSummary();
echo $prof_updatethisbin->getSummary();
echo $prof_currentbin->getSummary();
*/
return $instances;
}
/*
* XXX this whole function is hax because i want instances inside summaries
* while the data has summaries inside instances
*/
function printSummary($format, $info, $start, $end, $binsize, $summary) {
$src = $info["src"];
$dst = $info["dst"];
$test = $info["testType"];
$subtype = $info["testSubType"];
switch ( $format ) {
case "xml":
echo "<summary src='$src' dst='$dst' " .
"start='$start' end='$end' test='" . getTestName($test) .
"' subtype='$subtype' binsize='$binsize'>";
/*
$response = new SimpleXMLElement("<response/>");
$node = $response->addChild("summary");
$node->addAttribute("src", $src);
$node->addAttribute("dst", $dst);
$node->addAttribute("start", $start);
$node->addAttribute("end", $end);
$node->addAttribute("test", getTestName($test));
$node->addAttribute("subtype", $subtype);
$node->addAttribute("binsize", $binsize);
*/
foreach($summary as $instance => $data) {
$response = new SimpleXMLElement("<response/>");
foreach($data["summary"] as $stat => $values) {
$node = $response->addChild($stat);
foreach($values as $type => $value)
$node->addChild($type, $value);
printInstanceHeaders($format, $test, $instance);
echo $node->asXML();
printInstanceFooters($format, $test, $instance);
}
}
echo "</summary>";
break;
case "json":
/*
$local = array();
foreach($summary as $instance => $data) {
$local[$instance] = $data["summary"];
}
echo json_encode(array("summary" => $local));
echo ",";
*/
foreach($summary as $instance => $data) {
printInstanceHeaders($format, $test, $instance);
//echo json_encode(array("summary" => $data["summary"]));
/* print this separately so we don't get an ending set of
* parentheses... we want to put the dataset after it
*/
echo "\"summary\":";
echo json_encode($data["summary"]);
printInstanceFooters($format, $test, $instance);
echo ",";
}
break;
case "csv":
case "text":
echo "# SUMMARY:\n";
foreach($summary as $instance => $data) {
if ( $instance != "default" )
echo "# instance: $instance\n";
foreach($data["summary"] as $stat => $values) {
echo "# $stat";
foreach($values as $type => $value) {
echo " $type:$value";
}
echo "\n";
}
}
break;
};
}
function printResponseStart($format, $info, $start, $end, $binsize) {
$src = $info["src"];
$dst = $info["dst"];
$test = $info["testType"];
$subtype = $info["testSubType"];
switch ( $format ) {
case "xml":
/* cant use proper xml printing here because we want to leave
* the tag open
*/
echo "<?xml version=\"1.0\"?>";
echo "<response>";
break;
case "json":
echo "{\"response\":{";
break;
case "csv":
case "text":
echo "# src='$src' dst='$dst' start='$start' end='$end' " .
"test='" . getTestName($test) . "' subtype='$subtype' " .
"binsize='$binsize'\n";
break;
};
}
/*
* do initialisation stuff that only needs to be done once - headers etc
*
* REQUIRES:
* xml - actual src, dst, test, subtype, start, end, binsize
* json - null
* csv - column headings (time, src, dst, test, subtype, exta...)
*/
function printDataStart($format, $info, $start, $end, $binsize, $outputstats) {
global $raw_data_funcs;
$src = $info["src"];
$dst = $info["dst"];
$test = $info["testType"];
$subtype = $info["testSubType"];
switch ( $format ) {
case "xml":
echo "<dataset src='$src' dst='$dst' " .
"start='$start' end='$end' test='" . getTestName($test) .
"' subtype='$subtype' binsize='$binsize'>";
break;
case "json":
echo "\"dataset\":[";
break;
case "csv":
case "text":
/* use custom names if we've got them */
$otherKeys = getStatName($test);
/* XXX can remove the raw data func bit? because all tests
* now should have a nice lookup/ordering info block?
*/
if ( $otherKeys == array() ) {
/* check the appropriate data function exists for the test */
$func = $raw_data_funcs[getTestId($test)];
if (!function_exists($func)) {
printError($format, 501,
"No raw data format function available",
"raw data format function doesn't exist for " .
"test $test (" . getTestName($test) . ")");
}
$otherKeys = explode(",", $func($subtype, NULL));
}
/* merge generic headings with test specific ones */
$keys = array_merge(array_keys($info), $otherKeys);
/* print the keys that are always present */
$first = true;
foreach ( array_keys($info) as $header ) {
if ( !$first )
echo ",";
echo $header;
$first = false;
}
/* prefix each of the results with the type of measurement */
$keys = array_diff($keys, array_keys($info));
foreach($keys as $header) {
if($header == "instance" || $header == "traceroute")
echo ",$header";
else
foreach($outputstats as $stat) {
echo ",";
echo $stat . "_" . $header;
}
}
echo "\n";
break;
};
}
/*
* do any final end things, printing those that require it
*/
function printDataEnd($format) {
switch ( $format ) {
case "xml": echo "</dataset></response>"; break;
case "json": echo "]}}"; break;
case "csv":
case "text": break;
};
}
function printInstanceHeaders($format, $test, $instance) {
$testId = getTestId($test);
if ( $testId == NZRS_DATA || $testId == DNS2_DATA ) {
switch($format) {
case "xml": echo "<instance name='$instance'>"; break;
case "json": echo "{\"$instance\":["; break;
};
}
}
function printInstanceFooters($format, $test) {
$testId = getTestId($test);
if ( $testId == NZRS_DATA || $testId == DNS2_DATA ) {
switch($format) {
case "xml": echo "</instance>"; break;
case "json": echo "]}"; break;
};
}
}
function printData($format, $info, $test, &$data) {
$first = true;
foreach ( $data as $bin ) {
if ( getTestId($test) == TRACE_DATA /*||
getTestId($test) == SCAMPER_DATA*/ ) {
printTraceDataValue($format, $info, $bin, $first);
} else {
printDataValue($format, $info, $bin, $first);
}
$first = false;
}
}
function printTraceDataValue($format, $info, $data, $first) {
switch ( $format ) {
case "xml":
// not printed, just used as something to make subtrees under
// should be able to use LIBXML_NOXMLDECL but it's broken
$response = new SimpleXMLElement("<response/>");
$node = $response->addChild("data");
$node->addChild("time", $data["time"]);
$path = $node->addChild("path", "");
$path->addAttribute("count", sizeof($data["path"]));
foreach($data["path"] as $hopinfo) {
$hop = $path->addChild("hop", "");
$hop->addChild("ip", $hopinfo["ip"]);
$hop->addChild("hostname", $hopinfo["hostname"]);
}
echo $node->asXML();
break;
case "json":
$node = array("time" => $data["time"]);
foreach($data as $key => $value)
$node[$key] = $value;
if ( !$first )
echo ",";
echo json_encode(array("data" => $node));
break;
case "csv":
case "text":
echo $data["time"];
//XXX hax
unset($data["time"]);
unset($info["timestamp"]);
foreach($info as $stat)
echo ",$stat";
foreach($data["path"] as $hop) {
$ip = $hop["ip"];
$name = $hop["hostname"];
$this_route .= "$name($ip) ";
}
echo ",";
echo $this_route;
echo "\n";
break;
};
}
/*
* Print an individual data value. Ideally this could all be done at the end
* by printing a whole XML tree or something, but we may not have enough
* memory to hold such large strings this could produce so we are only ever
* printing a single data point at a time.
*/
function printDataValue($format, $info, &$data, $first) {
switch ( $format ) {
case "xml":
// not printed, just used as something to make subtrees under
// should be able to use LIBXML_NOXMLDECL but it's broken
$response = new SimpleXMLElement("<response/>");
$node = $response->addChild("data");
foreach($data as $key => $value) {
if ( is_array($value) ) {
$child = $node->addChild($key, "");
foreach($value as $subkey => $subvalue)
if ( $subkey == "missing" || $subkey == "count")
$child->addAttribute($subkey, $subvalue);
else
$child->addChild($subkey, $subvalue);
} else {
$node->addChild($key, $value);
}
}
echo $node->asXML();
break;
case "json":
$node = array("time" => $data["time"]);
foreach($data as $key => $value)
$node[$key] = $value;
if ( !$first )
echo ",";
echo json_encode(array("data" => $node));
break;
case "csv":
case "text":
echo $data["time"];
//XXX hax
unset($data["time"]);
unset($info["timestamp"]);
foreach($info as $stat)
echo ",$stat";
foreach($data as $stat) {
if(is_array($stat)) {
foreach($stat as $key=>$value) {
echo ",$value";
}
} else {
echo ",$stat";
}
}
echo "\n";
break;
};
}
/*
*
*/
function fetchData($format, $test, $subtype, $src, $dst, $start, $end, $stat,
$binsize, $summary) {
$secOfData = $end - $start;
/*
* little bit more sanity checking before we go off and do potentionally
* long duration investigations
*/
if ( $secOfData < 0 ) {
printError($format, 400, "Negative duration",
"End time was earlier than start time - negative duration");
}
if ( $secOfData > (60*60*24*365) ) {
printError($format, 400, "Duration too long",
"Maximum duration is currently capped at one year for all " .
"users. Let us know if this will be a problem for you.");
}
/* Open the Database */
$dataset = ampOpenDb($test, $subtype, $src, $dst, $start, 0, "UTC");
if ( !$dataset ) {
printError($format, 400, "Failed to open database",
"Failed to open database, probably because the host/test " .
"combination doesn't exist, or you gave it a timeframe it " .
"wasn't happy with. Check arguments are correct.");
}
/* add all the keys that this test uses */
$keys = array("timestamp", "src", "dst", "testType", "testSubType");
$info = array_combine($keys, array(0, $src, $dst, getTestName($test),
$subtype));
if ( getTestId($test) == TRACE_DATA /*|| getTestId($test) == SCAMPER_DATA*/ )
$results = getBinnedTraceValues($dataset,$start,$secOfData,$binsize);
else
$results = getBinnedValues($dataset, $test, $start, $secOfData,
$stat, $binsize, $summary);
return $results;
}
/*
* Cause all the data available between $src and $dst for test type