metrics.tx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <div id="backoffice">
  2. <script>
  3. var params = { period: 'hour', num_periods: 5 };
  4. addEventListener("DOMContentLoaded", (event => {
  5. // Fire off the XHR to fetch the JSON payload
  6. doChartXHR(params);
  7. }));
  8. addEventListener("chartXHRFinished", (event => {
  9. buildChart(event.payload);
  10. }));
  11. function doChartXHR(params) {
  12. const req = new XMLHttpRequest();
  13. req.addEventListener("load", respondToChartXHR);
  14. req.open("GET", location.protocol+'//'+location.host+"/api/requests_per"+location.search);
  15. req.send();
  16. };
  17. function respondToChartXHR() {
  18. var payload = this.responseText;
  19. console.log(payload);
  20. const ev = new Event("chartXHRFinished");
  21. ev.payload = JSON.parse(payload);
  22. dispatchEvent(ev);
  23. };
  24. function buildChart(payload) {
  25. const ctx = document.getElementById('request_chart');
  26. new Chart(ctx, {
  27. type: 'bar',
  28. data: {
  29. labels: payload.labels,
  30. datasets: [{
  31. label: '# Requests',
  32. data: payload.data,
  33. borderWidth: 1
  34. }]
  35. },
  36. options: {
  37. scales: {
  38. y: {
  39. beginAtZero: true
  40. }
  41. }
  42. }
  43. });
  44. };
  45. </script>
  46. <div>Keep in mind that this only measures direct hits to the application, which shouldn't happen aside from API calls or logged in users.</div>
  47. <br /><br />
  48. <form>
  49. Period:
  50. <select name=period class="cooltext" >
  51. <option value="second">second</option>
  52. <option value="minute">minute</option>
  53. <option value="hour" selected>hour</option>
  54. <option value="day">day</option>
  55. <option value="week">week</option>
  56. <option value="month">month</option>
  57. <option value="year">year</option>
  58. </select>
  59. Num Periods:
  60. <input name="num_periods" class="cooltext" value=5 />
  61. <input type="submit" value="Go" />
  62. </form>
  63. <div>
  64. <canvas id="request_chart"></canvas>
  65. </div>
  66. </div>