Elasticsearch.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <main class="flex flex-col">
  3. <h1 class="w-full text-4xl text-center font-bold mt-5 p-3 border-b-4 border-gray-500">
  4. Elasticsearch Lab
  5. </h1>
  6. <!-- Page Section -->
  7. <section class="w-full h-full flex">
  8. <div class="w-1/2 bg-rose-500">
  9. code window
  10. <select @change="selectQuery" v-model="selected">
  11. <option value="default">Default</option>
  12. </select>
  13. <textarea class="w-full h-96" v-model="userQuery" />
  14. <button @click="query" class="ml-5 mt-3 p-3 text-center text-lg font-medium bg-pink-300 border-4 border-pink-500 rounded-xl">
  15. Send Query
  16. </button>
  17. </div>
  18. <div class="w-1/2 bg-yellow-300 overflow-x-scroll overflow-y-scroll">
  19. instructions &amp; output
  20. <pre v-html="output" />
  21. </div>
  22. </section>
  23. </main>
  24. </template>
  25. <script>
  26. /* Import modules. */
  27. // import Nito from 'nitojs'
  28. /* Import components. */
  29. // import UnderConstruction from '@/components/UnderConstruction'
  30. export default {
  31. components: {
  32. // UnderConstruction,
  33. },
  34. data: () => {
  35. return {
  36. requests: null,
  37. selected: null,
  38. output: null,
  39. userQuery: null,
  40. }
  41. },
  42. computed: {
  43. //
  44. },
  45. methods: {
  46. async query() {
  47. console.log('SEND QUERY')
  48. /* Clear output. */
  49. this.output = ''
  50. let request
  51. if (this.userQuery) {
  52. try {
  53. // NOTE: We remove "helpful" comment.
  54. const pos = this.userQuery.indexOf('{')
  55. let userQuery
  56. if (pos) {
  57. userQuery = this.userQuery.slice(pos)
  58. } else {
  59. userQuery = this.userQuery
  60. }
  61. request = JSON.parse(userQuery)
  62. } catch (err) {
  63. console.error(err)
  64. }
  65. } else {
  66. request = this.requests[0]
  67. }
  68. console.log('REQUEST', request)
  69. // const response = await fetch()
  70. const response = await Nito.Blockchain.Query.request(request)
  71. .catch(err => console.error(err))
  72. console.log('RESPONSE (body):', response)
  73. this.output = JSON.stringify(response, null, 2)
  74. },
  75. selectQuery() {
  76. console.log('SELECTED', this.selected)
  77. switch(this.selected) {
  78. case 'latest-block':
  79. return this.loadLatestBlock()
  80. default:
  81. return this.loadDefault()
  82. }
  83. },
  84. loadDefault() {
  85. this.userQuery = `/* Customize your query below. */
  86. GET /indexName/_search?track_total_hits=true
  87. `
  88. },
  89. loadTimeout() {
  90. this.userQuery = `/* Customize your query below. */
  91. GET geo/_search?track_total_hits=true
  92. {
  93. "timeout": "2s",
  94. "query": {
  95. "match_all": {}
  96. }
  97. }
  98. `
  99. },
  100. },
  101. created: async function () {
  102. this.requests = []
  103. let request
  104. request = {
  105. v: 3,
  106. q: {
  107. find: {},
  108. limit: 10
  109. }
  110. }
  111. this.requests.push(request)
  112. },
  113. mounted: function () {
  114. //
  115. },
  116. }
  117. </script>