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
/
amp_comparison.php
executable file
·301 lines (242 loc) · 7.7 KB
/
amp_comparison.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
<?php
/*
* AMP Data Display Interface
*
* Graph Comparisons
*
* Author: Matt Brown <[email protected]>
* Version: $Id: amp_comparison.php 2103 2011-04-12 23:59:22Z brendonj $
*
* Functions and definitions to facilitate the generation of comparision
* graphs.
*
* The session is used to keep track of the graphs to be compared and the
* parameters for it.
*
*/
/**** Variables ****/
/* Define Preference Stuff */
define('PREF_COMPARISON', "comparison");
/**** Initialisation ****/
register_preference(array(PREF_GLOBAL), GP_COMPARISONS, array(PREF_GLOBAL),
"Allow comparisons between sites", "", PREF_TYPE_BOOL,
array(PREF_GLOBAL=>PREF_TRUE));
/* Register Preferences Relating To Graph Options */
register_preference(array(PREF_COMPARISON), PREF_BINSIZE,
array(PREF_LONGTERM,PREF_SHORTTERM), "Bin Size", "",
PREF_TYPE_INPUT,
array(PREF_LONGTERM=>35, PREF_SHORTTERM=>5), 3);
register_preference(array(PREF_COMPARISON), PREF_YMAX,
array(PREF_LONGTERM,PREF_SHORTTERM),
"Maximum y-axis Value", "", PREF_TYPE_INPUT,
array(PREF_LONGTERM=>-1,PREF_SHORTTERM=>-1), 3);
register_preference(array(PREF_COMPARISON), PREF_LINES,
array(PREF_LONGTERM,PREF_SHORTTERM),
"Display as Line Graph", "", PREF_TYPE_BOOL,
array(PREF_LONGTERM=>PREF_TRUE,PREF_SHORTTERM=>PREF_TRUE));
/**** Functions ****/
/*
* Process a page submission to check for comparison stuff
*/
function process_comparison()
{
/* Check for a posted form */
if ( ! isset($_REQUEST["comparison"]) )
return;
if ( $_REQUEST["comparison"] == "add" ) {
/* Initialise items array if it's not setup already */
if ( ! isset($_SESSION["comparison_items"]) ) {
$_SESSION["comparison_items"] = array();
}
/* Check if object is already in list */
if ( find_comparison_item($_REQUEST["cobject"], $_REQUEST["src"],
$_REQUEST["dst"]) != -1 ) {
/* Already in list, don't add again */
return;
}
/* Create new object */
$item = array();
$item["object"] = $_REQUEST["cobject"];
$item["subType"] = $_REQUEST["csubType"];
$item["src"] = $_REQUEST["src"];
$item["dst"] = $_REQUEST["dst"];
$item["time"] = $_REQUEST["ctime"];
$item["scope"] = $_REQUEST["cscope"];
/* Add it to the list */
$_SESSION["comparison_items"][] = $item;
} else if ( $_REQUEST["comparison"] == "del" ) {
$idx = $_REQUEST["idx"];
unset($_SESSION["comparison_items"][$idx]);
} else if ( $_REQUEST["comparison"] == "reset" ) {
reset_comparison();
}
}
/*
* Find an item in the comparison list
*
* Returns the index of the item in the list, or -1 if not found.
*/
function find_comparison_item($object, $src, $dst)
{
foreach ($_SESSION["comparison_items"] as $idx=>$item) {
/* Check required parameters */
if ( $item["object"] != $object ) {
continue;
}
if ( $item["src"] != $src ) {
continue;
}
if ( $item["dst"] != $dst ) {
continue;
}
/* Found it */
return $idx;
}
return -1;
}
/*
* Reset the comparison
*/
function reset_comparison()
{
/* Clear all session variables */
$_SESSION["comparison_items"] = array();
unset($_SESSION["comparison_items"]);
}
function build_comparison_graph($dir, $list, $startTimeSec,
$secOfData, $binSize) {
global $comp_colors;
$cgraphs = array();
/* For any src/dst with more than 1 display object selected, build
* a graph to show the comparison
*/
foreach ( $list as $host=>$count ) {
/* Skip dests with single object */
if ( $count <= 1 ) {
continue;
}
/* Build graphs if more than 1 object */
$graph = new display_item_t();
if($dir == "dst") {
$graph->name = "comparison-dst-$host-src";
$graph->title = "Comparison to $host";
} else {
$graph->name = "comparison-src-$host-dst";
$graph->title = "Comparison from $host";
}
$graph->cdst = $host;
$graph->dataSets = array();
$graph->objects = array();
$c=0;
foreach ( $_SESSION["comparison_items"] as $idx=>$item ) {
/* Skip other objects */
if ( $item["$dir"] != $host ) {
continue;
}
$object = get_display_object($item["object"]);
/* Skip non comparable objects */
if ( $object->comparable == FALSE ) {
continue;
}
/* Add to destination comparison */
if ( $graph->name != "" ) {
$graph->name .= "-";
}
/* get the other end of the link */
if($dir == "dst")
$graph->name .= $item["src"];
else
$graph->name .= $item["dst"];
$graph->objects[] = $object->name;
$graph->category = $object->category;
$graph->displayObject = $object;
$graph->subType = $item["subType"];
if ( $startTimeSec > 0 ) {
$graph->time = $startTimeSec;
} else {
$graph->time = start_of_week($item["time"]);
}
/* Retrieve data sets */
$dsf = $object->dataSetFunc;
$dataSet = $dsf($item["src"], $item["dst"], $item["subType"],
$graph->time, $secOfData, $binSize);
/* Loop through returned data sets */
for ( $i=0; $i<count($dataSet); $i++ ) {
$dataSet[$i]["color"] = $comp_colors[$c];
$dataSet[$i]["key"] = $item["src"] . " to " . $item["dst"] . " " .
$dataSet[$i]["key"];
$c++;
}
$graph->dataSets = array_merge($dataSet, $graph->dataSets);
} // foreach ( $_SESSION["comparison_items"] as $idx=>$item )
/* Add to graph list */
$cgraphs["$dir-$host"] = $graph;
} // foreach ( $dests as $dest=>$count )
return $cgraphs;
}
/* Look for display objects with common destinations in the comparison */
function comp_common_dests($startTimeSec, $secOfData, $binSize)
{
/* Initialisation */
$cgraphs = array();
/* Get list of destinations in selected objects */
$dests = comp_get_dest_list();
$sources = comp_get_source_list();
$cgraphs = build_comparison_graph("dst", $dests, $startTimeSec,
$secOfData, $binSize);
$cgraphs = array_merge($cgraphs,
build_comparison_graph("src", $sources, $startTimeSec,
$secOfData, $binSize));
return $cgraphs;
}
/* Return a list of all destinations in comparison list */
function comp_get_dest_list()
{
/* Initialisation */
$list = array();
if ( ! isset($_SESSION["comparison_items"]) ) {
return $list;
}
/* Loop through comparison items and add to list */
foreach( $_SESSION["comparison_items"] as $idx=>$item ) {
if ( array_key_exists($item["dst"], $list) ) {
$list[$item["dst"]]++;
} else {
$list[$item["dst"]] = 1;
}
}
return $list;
}
/* Return a list of all destinations in comparison list */
function comp_get_source_list()
{
/* Initialisation */
$list = array();
if ( ! isset($_SESSION["comparison_items"]) ) {
return $list;
}
/* Loop through comparison items and add to list */
foreach( $_SESSION["comparison_items"] as $idx=>$item ) {
if ( array_key_exists($item["src"], $list) ) {
$list[$item["src"]]++;
} else {
$list[$item["src"]] = 1;
}
}
return $list;
}
/* Return the timestamp for the start of the week the specified time is in */
function start_of_week($time) {
$parts = getdate($time);
$daystart = mktime(0, 0, 0, $parts["mon"], $parts["mday"],
$parts["year"], -1);
/* If time is already start of week, just return midnight */
if ( $parts["wday"] == 0 ) {
return $daystart;
}
/* Return start of week */
$ntime = $daystart - (86400 * $parts["wday"]);
return $ntime;
}
// vim:set sw=2 ts=2 sts=2 et:
?>