metrics.tx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. <form>
  47. Period:
  48. <select name=period class="cooltext" >
  49. <option value="second">second</option>
  50. <option value="minute">minute</option>
  51. <option value="hour" selected>hour</option>
  52. <option value="day">day</option>
  53. <option value="week">week</option>
  54. <option value="month">month</option>
  55. <option value="year">year</option>
  56. </select>
  57. Num Periods:
  58. <input name="num_periods" class="cooltext" value=5 />
  59. <input type="submit" value="Go" />
  60. </form>
  61. <div>
  62. <canvas id="request_chart"></canvas>
  63. </div>
  64. </div>