/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
  2. require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
  3. if (array_key_exists('metric', $_REQUEST) && array_key_exists($_REQUEST['metric'], $metrics)
  4. && array_key_exists('value', $_REQUEST) && is_numeric($_REQUEST['value']) !== false
  5. && array_key_exists('u', $_REQUEST)
  6. )
  7. {
  8. $url_id = getUrlId($_REQUEST['u']);
  9. if (array_key_exists('timestamp', $_REQUEST) && $_REQUEST['timestamp']) {
  10. # adding new entry
  11. $query = sprintf("INSERT INTO metric (timestamp, url_id, metric_id, value) VALUES ('%s', '%d', '%d', '%f')",
  12. mysql_real_escape_string($_REQUEST['timestamp']),
  13. mysql_real_escape_string($url_id),
  14. mysql_real_escape_string($metrics[$_REQUEST['metric']]['id']),
  15. mysql_real_escape_string($_REQUEST['value'])
  16. );
  17. } else {
  18. # adding new entry
  19. $query = sprintf("INSERT INTO metric (url_id, metric_id, value) VALUES ('%d', '%d', '%f')",
  20. mysql_real_escape_string($url_id),
  21. mysql_real_escape_string($metrics[$_REQUEST['metric']]['id']),
  22. mysql_real_escape_string($_REQUEST['value'])
  23. );
  24. }
  25. if (!mysql_query($query))
  26. {
  27. beaconError(mysql_error());
  28. }
  29. } else {
  30. header('HTTP/1.0 400 Bad Request');
  31. ?><html>
  32. <head>
  33. <title>Bad Request: Custom Metric beacon</title>
  34. <style>
  35. i {
  36. color: red;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <h1>Bad Request: Custom Metric beacon</h1>
  42. <p>This is custom metric beacon for ShowSlow.</p>
  43. <p>You can use automated script to publish events using GET call to one of these URLs (for specific metric):</p>
  44. <b><pre>
  45. <?php
  46. if (count($metrics) > 0) {
  47. foreach ($metrics as $name => $metric) {
  48. 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>
  49. <?php
  50. }
  51. } else {
  52. 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
  53. } ?>
  54. </pre></b>
  55. or use form below to manually enter metric values.
  56. <?php
  57. $nometrics = false;
  58. if (count($metrics) == 0) {
  59. $nometrics = true;
  60. ?><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
  61. }
  62. ?>
  63. <h2>Add metric</h2>
  64. <form action="" method="GET">
  65. <table>
  66. <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>
  67. <tr><td>Metric:</td><td><select name="metric"<?php if ($nometrics) {?> disabled="disabled"<?php }?>>
  68. <option value="">-- pick metric --</option>
  69. <?php
  70. foreach ($metrics as $name => $metric) {
  71. ?><option value="<?php echo $name?>"><?php echo $metric['title']?></option><?php
  72. }
  73. ?>
  74. </select></td></tr>
  75. <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>
  76. <tr><td>Value:</td><td><input type="text" name="value" size="80"<?php if ($nometrics) {?> disabled="disabled"<?php }?>/> (integer value)</td></tr>
  77. <tr><td></td><td><input type="submit" value="add"<?php if ($nometrics) {?> disabled="disabled"<?php }?>/></td></tr>
  78. </table>
  79. </form>
  80. </body></html>
  81. <?php
  82. exit;
  83. }
  84. header('HTTP/1.0 204 Data accepted');