LineResult.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <!--
  2. *
  3. * EliasDB - Data mining frontend example
  4. *
  5. * Copyright 2020 Matthias Ladkau. All rights reserved.
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public
  8. * License, v. 2.0. If a copy of the MPL was not distributed with this
  9. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. Graph which renders a result set as a line chart.
  12. -->
  13. <template>
  14. <div class="small">
  15. <line-chart :chart-data="linechartdata" :options="linechartoptions"></line-chart>
  16. <div class="chat-msg-window" v-for="msg in messages" v-bind:key="msg.key">
  17. <div>{{msg.result}}</div>
  18. </div>
  19. </div>
  20. </template>
  21. <script lang="ts">
  22. import Vue from "vue";
  23. import LineChart from "./LineChart.vue";
  24. import { EliasDBGraphQLClient } from "../lib/eliasdb-graphql";
  25. interface Message {
  26. key: string;
  27. result: string;
  28. }
  29. export default Vue.extend({
  30. props: ["name", "last"],
  31. data() {
  32. return {
  33. client: new EliasDBGraphQLClient(),
  34. messages: [] as Message[],
  35. linechartdata: null as any,
  36. linechartoptions: {
  37. responsive: true,
  38. maintainAspectRatio: false
  39. }
  40. };
  41. },
  42. mounted: async function() {
  43. // Ensure channel node exists
  44. let results: any[] = [];
  45. try {
  46. const response = await this.client.req(`
  47. {
  48. PingResult(ascending: "key", last:${this.last}) {
  49. key
  50. result
  51. success
  52. url
  53. }
  54. }`);
  55. results = JSON.parse(response).data.PingResult;
  56. console.log("Results:", results);
  57. } catch (e) {
  58. console.error("Could not query results:", e);
  59. }
  60. let labels: string[] = [];
  61. let data: number[] = [];
  62. results.forEach(r => {
  63. const timestamp = new Date();
  64. timestamp.setTime(parseInt(r["key"]) * 1000);
  65. const secs = parseFloat("0." + r["result"].split(".")[1]);
  66. if (!isNaN(secs)) {
  67. labels.push(timestamp.toISOString());
  68. data.push(secs);
  69. }
  70. });
  71. this.linechartdata = {
  72. labels: labels,
  73. datasets: [
  74. {
  75. label: this.name,
  76. backgroundColor: "#f87979",
  77. data: data,
  78. fill: false
  79. }
  80. ]
  81. };
  82. },
  83. components: {
  84. LineChart
  85. }
  86. });
  87. </script>
  88. <style>
  89. .small {
  90. margin: 50px auto;
  91. }
  92. </style>