PageRenderTime 298ms CodeModel.GetById 71ms app.highlight 101ms RepoModel.GetById 120ms app.codeStats 0ms

/details/index.php

http://showslow.googlecode.com/
PHP | 434 lines | 372 code | 55 blank | 7 comment | 56 complexity | 863a05e92a6417d3c03fffc7b579061d MD5 | raw file
  1<?php 
  2require_once(dirname(dirname(__FILE__)).'/global.php');
  3require_once(dirname(dirname(__FILE__)).'/users/users.php');
  4
  5if (!array_key_exists('url', $_GET) || ($url = filter_var($_GET['url'], FILTER_VALIDATE_URL)) === false) {
  6?><html>
  7<head>
  8<title>Error - no URL specified</title>
  9</head>
 10<body>
 11<h1>Error - no URL specified</h1>
 12<p><a href="../">Go back</a> and pick the URL</p>
 13</body></html>
 14<?php 
 15return;
 16}
 17
 18# building a query to select all beacon data in one swoop
 19$query = "SELECT urls.id AS url_id, UNIX_TIMESTAMP(last_update) AS t, last_event_update, yslow2.details AS yslow_details";
 20
 21foreach ($all_metrics as $provider_name => $provider) {
 22	$query .= ",\n\t".$provider['table'].'_last_id, UNIX_TIMESTAMP('.$provider['table'].'.timestamp) AS '.$provider_name.'_timestamp';
 23
 24	foreach ($provider['metrics'] as $section_name => $section) {
 25		foreach ($section as $metric) {
 26			$query .= ",\n\t\t".$provider['table'].'.'.$metric[1].' AS '.$provider_name.'_'.$metric[1];
 27		}
 28	}
 29}
 30
 31$query .= "\nFROM urls";
 32
 33foreach ($all_metrics as $provider_name => $provider) {
 34	$query .= "\n\tLEFT JOIN ".$provider['table'].' ON urls.'.$provider['table'].'_last_id = '.$provider['table'].'.id';
 35}
 36
 37$query .= "\nWHERE urls.url = '".mysql_real_escape_string($url)."'";
 38
 39#echo $query; exit;
 40
 41$result = mysql_query($query);
 42
 43if (!$result) {
 44	error_log(mysql_error());
 45}
 46
 47$row = mysql_fetch_assoc($result);
 48$lastupdate = $row['t'];
 49$eventupdate = $row['last_event_update'];
 50$urlid = $row['url_id'];
 51$yslow2_last_id = $row['yslow2_last_id'];
 52$pagespeed_last_id = $row['pagespeed_last_id'];
 53$dynatrace_last_id = $row['dynatrace_last_id'];
 54mysql_free_result($result);
 55
 56header('Last-modified: '.date(DATE_RFC2822, $lastupdate));
 57
 58if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER) && ($lastupdate <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
 59	header('HTTP/1.0 304 Not Modified');
 60	exit;
 61}
 62
 63$TITLE = 'Details for '.htmlentities($url);
 64$SCRIPTS = array(
 65	$showslow_base.'ajax/simile-ajax-api.js?bundle=true',
 66	$showslow_base.'timeline/timeline-api.js?bundle=true',
 67	$showslow_base.'timeplot/timeplot-api.js?bundle=true',
 68	'http://yui.yahooapis.com/combo?2.8.1/build/yahoo/yahoo-min.js&2.8.1/build/event/event-min.js&2.8.1/build/yuiloader/yuiloader-min.js',
 69	assetURL('details/details.js')
 70);
 71
 72$SECTION = 'all';
 73require_once(dirname(dirname(__FILE__)).'/header.php');
 74?>
 75<script>
 76<?php
 77echo 'var metrics = '.json_encode($metrics);
 78?>
 79</script>
 80<style>
 81.yslow1 {
 82	color: #55009D;
 83}
 84
 85.yslow2 {
 86	color: #2175D9;
 87}
 88
 89.details {
 90	cursor: help;
 91}
 92
 93.sectionname {
 94	padding-top: 1em;
 95}
 96.breakdowntitle {
 97	clear: both;
 98	margin-bottom: 0;
 99}
100.titlecol {
101	padding: 0 2em;
102}
103.value {
104	font-weight: bold;
105}
106</style>
107<h1 style="margin-bottom: 0">Details for <a href="<?php echo htmlentities($url)?>" rel="nofollow"><?php echo htmlentities(ellipsis($url, 31)) ?></a></h1>
108<?php if (!is_null($addThisProfile)) {?>
109<!-- AddThis Button BEGIN -->
110<div class="addthis_toolbox addthis_default_style" style="margin-right: 10px;">
111<a href="http://www.addthis.com/bookmark.php?v=250&amp;username=<?php echo urlencode($addThisProfile)?>" class="addthis_button_compact">Share</a>
112<span class="addthis_separator">|</span>
113<a class="addthis_button_twitter"></a>
114<a class="addthis_button_facebook"></a>
115<a class="addthis_button_google"></a>
116<a class="addthis_button_delicious"></a>
117<a class="addthis_button_stumbleupon"></a>
118<a class="addthis_button_reddit"></a>
119<span class="addthis_separator">|</span>
120<a class="addthis_button_favorites"></a>
121<a class="addthis_button_print"></a>
122<a class="addthis_button_email"></a>
123</div>
124<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=<?php echo urlencode($addThisProfile)?>"></script>
125<!-- AddThis Button END -->
126<?php
127}
128
129// checking if there is har data
130$query = sprintf("SELECT har.timestamp as t, har.id as id, har.link as link FROM har WHERE har.url_id = '%d' ORDER BY timestamp DESC",
131	mysql_real_escape_string($urlid)
132);
133
134$result = mysql_query($query);
135
136if (!$result) {
137        error_log(mysql_error());
138}
139
140$har = array();
141while ($har_row = mysql_fetch_assoc($result)) {
142	$har[] = $har_row;
143}
144
145// checking if there were PageTest tests ran
146$query = sprintf("SELECT pagetest.timestamp as t, test_url, location FROM pagetest WHERE pagetest.url_id = '%d' ORDER BY timestamp DESC",
147	mysql_real_escape_string($urlid)
148);
149
150$result = mysql_query($query);
151
152if (!$result) {
153        error_log(mysql_error());
154}
155
156$pagetest = array();
157while ($pagetest_row = mysql_fetch_assoc($result)) {
158	$pagetest[] = $pagetest_row;
159}
160mysql_free_result($result);
161
162if ($row && !(is_null($row['yslow_timestamp'])
163	&& is_null($row['pagespeed_timestamp'])
164	&& is_null($row['dynatrace_timestamp']))
165	)
166{
167	?>
168	<table cellpadding="15" cellspacing="5"><tr>
169	<?php
170	foreach ($all_metrics as $provider_name => $provider) {
171		$score = $row[$provider_name.'_'.$provider['score_column']];
172		if (!is_null($score)) {
173			$pretty_score = prettyScore($score);
174		?>
175		<td valign="top" align="center" class="<?php echo $provider_name ?>">
176		<img src="http://chart.apis.google.com/chart?chs=225x108&cht=gom&chd=t:<?php echo urlencode($score)?>&chl=<?php echo urlencode($pretty_score.' ('.$score.')') ?>" alt="<?php echo $pretty_score ?> (<?php echo htmlentities($score)?>)" title="Current <?php echo $provider['title'] ?> <?php echo $provider['score_name'] ?>: <?php echo $pretty_score ?> (<?php echo htmlentities($score)?>)"/>
177		<div>Current <a target="_blank" href="<?php echo $provider['url'] ?>"><?php echo $provider['title'] ?></a> <?php echo $provider['score_name'] ?>: <b><?php echo $pretty_score ?> (<i><?php echo htmlentities($score)?></i>)</b></div>
178		</td>
179		<?php
180		}
181	}
182	?>
183	</tr></table>
184<?php
185}
186
187// fetching locations only when needed
188getPageTestLocations();
189
190if (!is_null($webPageTestBase) && !is_null($webPageTestKey)) { ?>
191	<a name="pagetest"/><h2>Run a test using <a href="<?php echo htmlentities($webPageTestBase)?>" target="_blank">WebPageTest</a> and store the results</h2>
192	<form action="<?php echo htmlentities($showslow_base)?>pagetest.php" method="GET" target="_blank">
193	<input type="hidden" name="url" size="40" value="<?php echo htmlentities($url)?>"/>
194	Location: <select name="location">
195	<?php foreach ($webPageTestLocations as $location) {
196		if ($location['tests'] > 50) {
197			continue;
198		}
199	?>
200		<option <?php echo htmlentities($location['default']) ? 'selected ' : ''?>value="<?php echo htmlentities($location['id'])?>"><?php echo htmlentities($location['title'])?></option>
201	<?php } ?></select>
202	<input type="checkbox" name="private" id="wpt_private" value="1"<?php if ($webPageTestPrivateByDefault) {?> checked="true"<?php } ?>/><label for="wpt_private">Private</label>
203	<input type="checkbox" name="fvonly" id="wpt_fvonly" value="1"<?php if ($webPageTestFirstRunOnlyByDefault) {?> checked="true"<?php } ?>/><label for="wpt_fvonly">First View Only</label>
204	<input type="submit" style="font-weight: bold" value="start test &gt;&gt;"/>
205	<?php if (count($pagetest) > 0) {?><a href="#pagetest-table">See test history below</a><?php } ?>
206	</form>
207<?php
208}
209
210if ($row && is_null($row['yslow_timestamp'])
211	&& is_null($row['pagespeed_timestamp'])
212	&& is_null($row['dynatrace_timestamp']))
213{
214	?>
215	<a name="graph"/><h2 style="clear: both">Measurements over time</h2>
216	<table width="100%" height="250px" style="border: 1px solid silver"><tr>
217	<td align="center" valign="middle">
218		<table cellpadding="3px">
219		<tr>
220		<td><img src="<?php echo assetURL('clock.png')?>"/></td>
221		<td style="font-size: larger">Data is being collected</td>
222		</tr>
223		<tr><td colspan="2" align="center"><div class="gbox"><div class="bar ccol"></div></td></tr>
224		</table>
225	</td>
226	</tr></table>
227<?php
228} else if (!$row) {
229	?><div style="padding: 2em">No data is collected for this URL</div><?php
230}
231
232if ($row && !(is_null($row['yslow_timestamp'])
233	&& is_null($row['pagespeed_timestamp'])
234	&& is_null($row['dynatrace_timestamp']))
235	)
236{
237	// Graph
238	?>
239	<script>
240	url = '<?php echo htmlentities($url)?>';
241	ydataversion = '<?php echo urlencode($row['yslow_timestamp'])?>';
242	psdataversion = '<?php echo urlencode($row['pagespeed_timestamp'])?>';
243	dtdataversion = '<?php echo urlencode($row['dynatrace_timestamp'])?>';
244	eventversion = '<?php echo urlencode($eventupdate)?>';
245	</script>
246
247	<a name="graph"/><h2 style="clear: both">Measurements over time</h2>
248	<div id="my-timeplot" style="height: 250px;"></div>
249	<div style="font-size: 0.9em">
250	<?php
251	if (!is_null($row['yslow_timestamp']))
252	{
253	?>
254	<span style="color: #D0A825">Page Size</span> (in bytes);
255	<span style="color: purple">Page Load time (YSlow)</span> (in ms);
256	<span style="color: #75CF74">Total Requests</span>;
257	<span class="yslow2">YSlow Grade</span> (0-100);
258	<?php
259	}
260
261	if (!is_null($row['pagespeed_timestamp']))
262	{
263	?>
264	<span style="color: #6F4428">Page Speed Grade</span> (0-100);
265	<span style="color: #EE4F00">Page Load time (Page Speed)</span> (in ms);
266	<?php
267	}
268
269	if (!is_null($row['dynatrace_timestamp']))
270	{
271	?>
272	<span style="color: #AB0617">dynaTrace rank</span> (0-100);
273	<?php
274	}
275
276	foreach ($metrics as $name => $metric)
277	{
278		?><span title="<?php echo htmlentities($metric['description'])?>" style="color: <?php echo array_key_exists('color', $metric) ? $metric['color'] : 'black' ?>"><?php echo htmlentities($metric['title'])?></span> (<a href="data_metric.php?metric=<?php echo urlencode($name);?>&url=<?php echo urlencode($url);?>">csv</a>);
279<?php
280	}
281	?>
282	</div>
283
284	<?php
285	$details = json_decode($row['yslow_details'], true);
286	?>
287	<script>
288	<?php
289	$comps = array();
290
291	if (is_array($details) && array_key_exists('g', $details)) {
292		foreach ($details['g'] as $n => $y) {
293			if (is_array($y) && array_key_exists('components', $y)) {
294				$comps['yslow_'.$n] = $y['components'];
295			}
296		}
297	}
298	?>
299	var details = <?php echo json_encode($comps)?>;
300	</script>
301
302	<?php 
303	// Breakdowns for each provider
304	foreach ($all_metrics as $provider_name => $provider) {
305		if (!is_null($row[$provider_name.'_timestamp']))
306		{
307		?>
308			<a name="<?php echo $provider_name ?>"/><h2 class="breakdowntitle"><?php echo $provider['title']?> breakdown</h2>
309			<table>
310			<?php
311			foreach ($provider['metrics'] as $section_name => $metrics)
312			{
313				?><tr><td colspan="6" class="sectionname"><b><?php echo $section_name ?></b></td></tr><?php
314
315				$odd = true;
316				
317				foreach ($metrics as $metric) {
318					if ($odd) { ?><tr><?php }
319
320					if (isset($metric[3])) {
321						?><td class="titlecol"><a target="_blank" href="<?php echo $metric[3]?>"><?php echo $metric[0]?></a></td><?php
322					}else{
323						?><td class="titlecol"><?php echo $metric[0]?></td><?php
324					}
325
326					$value = $row[$provider_name.'_'.$metric[1]];
327
328					if (is_null($value)) {
329						?><td colspan="3" class="na">n/a</td><?php	
330					} else {
331						if ($metric[2] == PERCENTS){
332							$pretty_score = prettyScore($value);
333						?>
334							<td class="value"><?php echo $pretty_score?> (<i><?php echo htmlentities($value)?></i>%)</td>
335							<td><span id="details_<?php echo $provider_name.'_'.$metric[1] ?>" class="details"></span></td>
336							<td><div class="gbox" title="Current <?php echo $provider['score_name']?>: <?php echo $pretty_score?> (<?php echo $value?>%)"><div class="bar c<?php echo scoreColorStep($value)?>" style="width: <?php echo $value+1?>px"/></div></td>
337						<?php
338						} else {
339							?><td colspan="3" class="value"><?php echo $value.$metric_types[$metric[2]]['units'] ?></td><?php	
340						}
341					}
342
343					if (!$odd) { ?></tr><?php }
344
345					$odd = !$odd;
346				}
347				?>
348			</tr>
349			<?php
350			}
351			?>
352		</table>	
353	<?php 
354		}
355	}
356}
357
358if (!is_null($row['yslow_timestamp'])) {
359?>
360	<a name="yslow-table"/><h2>YSlow measurements history (<a href="data.php?ver=<?php echo urlencode($row['yslow_timestamp'])?>&url=<?php echo urlencode($url)?>">csv</a>)</h2>
361	<div id="measurementstable"></div>
362	<?php 
363}
364
365if (!is_null($row['pagespeed_timestamp'])) {
366?>
367	<a name="pagespeed-table"/><h2>Page Speed measurements history (<a href="data_pagespeed.php?ver=<?php echo urlencode($row['pagespeed_timestamp'])?>&url=<?php echo urlencode($url)?>">csv</a>)</h2>
368	<div id="ps_measurementstable"></div>
369<?php 
370}
371
372if (!is_null($row['dynatrace_timestamp'])) {
373?>
374	<a name="dynatrace-table"/><h2>dynaTrace measurements history (<a href="data_dynatrace.php?ver=<?php echo urlencode($row['dynatrace_timestamp'])?>&url=<?php echo urlencode($url)?>">csv</a>)</h2>
375	<div id="dt_measurementstable"></div>
376<?php 
377}
378
379if (count($pagetest) > 0) {
380?>
381	<a name="pagetest-table"/><h2>WebPageTest data collected</h2>
382
383	<p>You can see latest <a href="<?php echo htmlentities($pagetest[0]['test_url']) ?>" target="_blank">PageTest report for <?php echo htmlentities($url)?></a> or check the archive:</p>
384
385	<table cellpadding="5" cellspacing="0" border="1">
386	<tr>
387	<th>Time</th>
388	<th>Location</th>
389	<th>PageTest</th>
390	</tr>
391<?php
392	foreach ($pagetest as $pagetestentry) {
393		$location = array_key_exists($pagetestentry['location'], $webPageTestLocationsById) ?
394			$webPageTestLocationsById[$pagetestentry['location']]['title'] :
395			$pagetestentry['location'];
396?>
397	<tr>
398	<td><?php echo htmlentities($pagetestentry['t'])?></td>
399	<td><?php echo htmlentities($location)?></td>
400	<td><a href="<?php echo htmlentities($pagetestentry['test_url'])?>" target="_blank">view PageTest report</a></td>
401	</tr>
402<?php
403	}
404?>
405	</table>
406<?php
407}
408
409if (count($har) > 0) {
410	$har_url = is_null($har[0]['link']) ?
411		$showslow_base.'details/har.php?id='.urlencode($har[0]['id']).'callback=onInputData'
412		: $har[0]['link'];
413?>
414	<a name="har-table"/><h2>HAR data collected</h2>
415
416	<p>You can see latest HAR data in the viewer here: <a href="<?php echo htmlentities($HARViewerBase)?>?inputUrl=<?php echo urlencode($har_url) ?>" target="_blank">HAR for <?php echo htmlentities($url)?></a>.</p>
417
418	<table cellpadding="5" cellspacing="0" border="1">
419	<tr><th>Time</th><th>HAR</th></tr>
420<?php
421	foreach ($har as $harentry) {
422		$har_url = is_null($harentry['link']) ?
423			$showslow_base.'details/har.php?id='.urlencode($harentry['id']).'callback=onInputData'
424			: $harentry['link'];
425?>
426	<tr><td><?php echo htmlentities($harentry['t'])?></td><td><a href="<?php echo htmlentities($HARViewerBase)?>?inputUrl=<?php echo urlencode($har_url) ?>" target="_blank">view in HAR viewer</a></td></tr>
427<?php
428	}
429?>
430	</table>
431<?php
432}
433
434require_once(dirname(dirname(__FILE__)).'/footer.php');