PageRenderTime 59ms CodeModel.GetById 53ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/beacon/metric/index.php

http://showslow.googlecode.com/
PHP | 91 lines | 82 code | 7 blank | 2 comment | 20 complexity | 4ddc4dfd2502c9f3cad9d4a0313134d3 MD5 | raw file
 1<?php 
 2require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
 3
 4if (array_key_exists('metric', $_REQUEST) && array_key_exists($_REQUEST['metric'], $metrics)
 5	&& array_key_exists('value', $_REQUEST) && is_numeric($_REQUEST['value']) !== false
 6	&& array_key_exists('u', $_REQUEST)
 7	)
 8{
 9	$url_id = getUrlId($_REQUEST['u']);
10
11	if (array_key_exists('timestamp', $_REQUEST) && $_REQUEST['timestamp']) {
12		# adding new entry
13		$query = sprintf("INSERT INTO metric (timestamp, url_id, metric_id, value) VALUES ('%s', '%d', '%d', '%f')",
14			mysql_real_escape_string($_REQUEST['timestamp']),
15			mysql_real_escape_string($url_id),
16			mysql_real_escape_string($metrics[$_REQUEST['metric']]['id']),
17			mysql_real_escape_string($_REQUEST['value'])
18		);
19	} else {
20		# adding new entry
21		$query = sprintf("INSERT INTO metric (url_id, metric_id, value) VALUES ('%d', '%d', '%f')",
22			mysql_real_escape_string($url_id),
23			mysql_real_escape_string($metrics[$_REQUEST['metric']]['id']),
24			mysql_real_escape_string($_REQUEST['value'])
25		);
26	}
27
28	if (!mysql_query($query))
29	{
30		beaconError(mysql_error());
31	}
32} else {
33	header('HTTP/1.0 400 Bad Request');
34
35	?><html>
36<head>
37<title>Bad Request: Custom Metric beacon</title>
38<style>
39i {
40        color: red;
41}
42</style>
43</head>
44<body>
45<h1>Bad Request: Custom Metric beacon</h1>
46<p>This is custom metric beacon for ShowSlow.</p>
47<p>You can use automated script to publish events using GET call to one of these URLs (for specific metric):</p>
48<b><pre>
49<?php
50if (count($metrics) > 0) {
51	foreach ($metrics as $name => $metric) {
52		echo $showslow_base?>beacon/metric/?metric=<i style="color: blue"><?php echo $name ?></i>&amp;u=<i>url</i>&amp;value=<i>integer_value</i>&amp;timestamp=<i>mysql_timestamp</i>
53<?php
54	}
55} else {
56	echo $showslow_base?>beacon/metric/?metric=<i>metricname</i>&amp;u=<i>url</i>&amp;value=<i>integer_value</i>&amp;timestamp=<i>mysql_timestamp</i><?php
57} ?>
58</pre></b>
59or use form below to manually enter metric values.
60<?php
61$nometrics = false;
62if (count($metrics) == 0) {
63	$nometrics = true;
64
65	?><p style="color: red">No custom metrics configured for this instance of ShowSlow.<br/>Add entries to <tt>$metrics</tt> array in configuration file to enable custom metric reporting.</p><?php
66}
67?>
68<h2>Add metric</h2>
69<form action="" method="GET">
70<table>
71<tr valign="top"><td>Time:</td><td><input type="text" name="timestamp" size="25" value="<?php echo date("Y-m-d H:i:s");?>"<?php if ($nometrics) {?> disabled="disabled"<?php }?>/><br/>Time in MySQL <a href="http://dev.mysql.com/doc/refman/5.1/en/datetime.html">timestamp format</a></td></tr>
72<tr><td>Metric:</td><td><select name="metric"<?php if ($nometrics) {?> disabled="disabled"<?php }?>>
73<option value="">-- pick metric --</option>
74<?php
75foreach ($metrics as $name => $metric) {
76	?><option value="<?php echo $name?>"><?php echo $metric['title']?></option><?php
77}
78?>
79</select></td></tr>
80<tr><td>URL:</td><td><input type="text" name="u" value="http://www.example.com/" size="80"<?php if ($nometrics) {?> disabled="disabled"<?php }?>/></td></tr>
81<tr><td>Value:</td><td><input type="text" name="value" size="80"<?php if ($nometrics) {?> disabled="disabled"<?php }?>/> (integer value)</td></tr>
82<tr><td></td><td><input type="submit" value="add"<?php if ($nometrics) {?> disabled="disabled"<?php }?>/></td></tr>
83
84</table>
85</form>
86</body></html>
87<?php
88	exit;
89}
90
91header('HTTP/1.0 204 Data accepted');