logs.svelte 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <script lang="typescript">
  2. import type { PollOptions, PollResult, LogEvent } from "src/routes/logs/poll";
  3. let logEvents: LogEvent[] = [];
  4. async function pollEvents() {
  5. const params: PollOptions = {
  6. limit: "50",
  7. };
  8. const query = Object.entries(params).reduce(
  9. (prev, [key, val]) =>
  10. `${prev}&${encodeURIComponent(key)}=${encodeURIComponent(val)}`,
  11. ""
  12. );
  13. const response = await fetch(`/logs/poll?${query}`, {
  14. method: "get",
  15. credentials: "include",
  16. headers: {
  17. Accept: "application/json",
  18. "Content-Type": "application/json",
  19. },
  20. });
  21. const result = (await response.json()) as PollResult;
  22. console.log(result);
  23. if (result.ok) {
  24. logEvents = result.events;
  25. }
  26. }
  27. </script>
  28. <style>
  29. </style>
  30. <div class="viewport text-white">
  31. <h1 class="text-3xl pb-4">Event logs</h1>
  32. <form class="py-2" on:submit|preventDefault={pollEvents}>
  33. <input
  34. class="bg-blue-600 py-2 px-3 text-white cursor-pointer rounded-sm"
  35. type="submit"
  36. value="Query" />
  37. </form>
  38. <table class="py-3">
  39. <thead>
  40. <tr>
  41. <th>Source</th>
  42. <th>Timestamp</th>
  43. <th>Level</th>
  44. <th>Message</th>
  45. </tr>
  46. </thead>
  47. <tbody>
  48. {#each logEvents as logEvent (logEvent._id)}
  49. <tr>
  50. <td>{logEvent.label}</td>
  51. <td>{logEvent.timestamp}</td>
  52. <td>{logEvent.level}</td>
  53. <td>{logEvent.message}</td>
  54. </tr>
  55. {/each}
  56. </tbody>
  57. </table>
  58. </div>