作者 邓超

es kibana

@@ -30,8 +30,18 @@ class Kibana extends Base { @@ -30,8 +30,18 @@ class Kibana extends Base {
30 30
31 public function result(){ 31 public function result(){
32 $json = app()->request('json'); 32 $json = app()->request('json');
  33 + try {
33 return es()->getClient()->search($json); 34 return es()->getClient()->search($json);
  35 + } catch (\Exception $e) {
  36 + try {
  37 + return json_decode('{'.explode('{',$e->getMessage(),2)[1],true);
  38 + } catch (\Throwable $ee) {
  39 + return [$e->getMessage()];
34 } 40 }
  41 + }
  42 +
  43 + }
  44 +
35 45
36 } 46 }
37 47
1 <template> 1 <template>
2 <div class="main"> 2 <div class="main">
3 - <div id="edit">  
4 - 3 + <div class="header">
  4 + <div class="item" style="width: 20px;background-color:#a1dcf8;text-align: center;" title="新建栏">
  5 + <svg @click="add"
  6 + style="width: 1em;height: 1em;vertical-align: middle;fill: currentColor;overflow: hidden;"
  7 + viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2498">
  8 + <path d="M576 448h384v128H576v384H448V576H64V448h384V64h128z" fill="#4A4A4A" p-id="2499"></path>
  9 + </svg>
  10 + </div>
  11 + <div v-for="(item,index) in items" :key="item.id">
  12 + <div
  13 + :class="{item:1,active:index===useKey}"
  14 + @click="load(index)"
  15 + :key="item.id">
  16 + <el-popover placement="bottom" trigger="contextmenu">
  17 + <template #reference> <span>{{item.name}}</span></template>
  18 + <template #default>
  19 + <div style="display: flex;width: 300px;">
  20 + <el-input v-model="item.name" style="min-width: 200px;"></el-input>
  21 + <el-button v-if="index>0" type="danger" style="margin-left: 20px;" @click="del(index)">删除</el-button>
  22 + </div>
  23 + </template>
  24 + </el-popover>
  25 + </div>
5 </div> 26 </div>
6 - <div id="result">  
7 27
8 </div> 28 </div>
9 - <el-button class="submit" @click="submit" :loading="loading">提交</el-button> 29 + <div>
  30 + <kibanaitem ref="kibana"></kibanaitem>
  31 + </div>
10 </div> 32 </div>
11 </template> 33 </template>
12 34
@@ -17,66 +39,104 @@ export default { @@ -17,66 +39,104 @@ export default {
17 name: "kibana", 39 name: "kibana",
18 data() { 40 data() {
19 return { 41 return {
20 - loading:false,  
21 - editor: null,  
22 - result: null 42 + useKey: 0,
  43 + items:[]
23 } 44 }
24 }, 45 },
25 created() {}, 46 created() {},
26 methods: { 47 methods: {
27 - submit(){  
28 - const data = {  
29 - json:this.editor.get()  
30 - };  
31 - console.log(data)  
32 - this.loading = true;  
33 - ajax().post('/kibana.result',data).then(res=>{  
34 - console.log(res)  
35 - this.loading = false;  
36 - this.result.set(res); 48 + save(){
  49 +
  50 + let q = this.$refs.kibana.getQuery();
  51 + let r = this.$refs.kibana.getResult();
  52 + if(q){
  53 + this.items[this.useKey].query = JSON.parse(JSON.stringify(q));
  54 + }
  55 + if(r){
  56 + this.items[this.useKey].result = JSON.parse(JSON.stringify(r));
  57 + }
  58 +
  59 + window.localStorage.setItem('kibana',JSON.stringify(this.items));
  60 +
  61 + window.localStorage.setItem('kibana_key',this.useKey);
  62 + },
  63 + del(index){
  64 + if(this.items.length===1) return;
  65 + this.items.splice(index,1);
  66 + },
  67 + add(){
  68 + this.items.push({
  69 + id: parseInt((Math.random()*100000)+''),
  70 + name:"default",
  71 + query:{},
  72 + result:{}
37 }) 73 })
  74 + },
  75 + load(val){
  76 +
  77 + if(this.useKey!==val){
  78 + this.save();
  79 +
  80 + this.useKey = val;
  81 + }
  82 +
  83 + this.$refs.kibana.setQuery(JSON.parse(JSON.stringify(this.items[val].query)));
  84 + this.$refs.kibana.setResult(JSON.parse(JSON.stringify(this.items[val].result)));
38 } 85 }
39 }, 86 },
40 mounted() { 87 mounted() {
41 88
42 - this.editor = new JSONEditor(document.getElementById("edit"), {  
43 - mode:"code",  
44 - enableSort: false,  
45 - enableTransform: false,  
46 - }); 89 + this.items = JSON.parse(window.localStorage.getItem('kibana')) || [];
  90 +
  91 + let index = parseInt(window.localStorage.getItem('kibana_key'));
  92 + if(typeof this.items[index] === "undefined"){
  93 + this.useKey = 0;
  94 + }else {
  95 + this.useKey = index;
  96 + }
  97 +
  98 + if (this.items.length === 0) this.add();
  99 +
  100 + this.load(this.useKey);
47 101
48 - this.result = new JSONEditor(document.getElementById("result"), {  
49 - mode:"code",  
50 - enableSort: false,  
51 - enableTransform: false,  
52 - editable : false,  
53 - mainMenuBar : false  
54 - }); 102 + setInterval(() => {
  103 + this.save();
  104 + },1000)
55 105
  106 +
  107 + },
  108 + //加载组件
  109 + components: {
  110 + kibanaitem:"@component(kibanaItem)"
56 } 111 }
57 } 112 }
58 </script> 113 </script>
59 114
60 <style scoped> 115 <style scoped>
61 .main{ 116 .main{
62 - display: flex;  
63 position: fixed; 117 position: fixed;
64 top: 0; 118 top: 0;
65 left: 0; 119 left: 0;
66 width: 100%; 120 width: 100%;
67 height: 100%; 121 height: 100%;
68 } 122 }
69 -#edit{  
70 - width: 50%; 123 +.header{
  124 + display: flex;
  125 + height: 32px;
  126 + background-color: #d0dbf0;
71 } 127 }
72 -#result{  
73 - width: 50%; 128 +.item{
  129 + height: 22px;
  130 + line-height: 22px;
  131 + padding: 5px 10px;
  132 + cursor: pointer;
74 } 133 }
75 -.submit{  
76 - position: fixed;  
77 - top: 2px;  
78 - left: calc(50% - 82px);  
79 - z-index: 9999;  
80 - width: 80px; 134 +.item.active{
  135 + background-color: #fff;
  136 +}
  137 +</style>
  138 +<style>
  139 +.el-popper.el-tooltip.el-popover{
  140 + width: auto !important;
81 } 141 }
82 </style> 142 </style>
  1 +<template>
  2 + <div class="main">
  3 + <div id="edit">
  4 +
  5 + </div>
  6 + <div id="result">
  7 +
  8 + </div>
  9 + <el-button class="submit" @click="submit" :loading="loading">提交</el-button>
  10 + </div>
  11 +</template>
  12 +
  13 +<script>
  14 +export default {
  15 + name: "item",
  16 + data() {
  17 + return {
  18 + loading:false,
  19 + editor: null,
  20 + result: null
  21 + }
  22 + },
  23 + created() {},
  24 + methods: {
  25 + setQuery(query){
  26 + console.log(query)
  27 + try {
  28 + this.editor.set(query);
  29 + }catch (e) {
  30 + console.log(query,e)
  31 + }
  32 +
  33 + },
  34 + setResult(result){
  35 + console.log(result)
  36 + try {
  37 + this.result.set(result);
  38 + }catch (e) {
  39 + console.log(result,e)
  40 + }
  41 + },
  42 + getQuery(){
  43 + try {
  44 + return this.editor.get();
  45 + }catch (e) {
  46 + return false;
  47 + }
  48 +
  49 + },
  50 + getResult(){
  51 + try {
  52 + return this.result.get();
  53 + }catch (e) {
  54 + return false;
  55 + }
  56 + },
  57 + submit(){
  58 + const data = {
  59 + json:this.editor.get()
  60 + };
  61 + console.log(data)
  62 + this.loading = true;
  63 + ajax().post('/kibana.result',data).then(res=>{
  64 + console.log(res)
  65 + this.loading = false;
  66 + this.result.set(res);
  67 + })
  68 + }
  69 + },
  70 + mounted() {
  71 +
  72 + this.editor = new JSONEditor(document.getElementById("edit"), {
  73 + mode:"code",
  74 + enableSort: false,
  75 + enableTransform: false,
  76 + });
  77 +
  78 + this.result = new JSONEditor(document.getElementById("result"), {
  79 + mode:"code",
  80 + enableSort: false,
  81 + enableTransform: false,
  82 + editable : false,
  83 + mainMenuBar : false
  84 + });
  85 +
  86 + }
  87 +}
  88 +</script>
  89 +
  90 +<style scoped>
  91 +.main{
  92 + display: flex;
  93 + position: relative;
  94 + top: 0;
  95 + left: 0;
  96 + width: 100%;
  97 + height: 100vh;
  98 +}
  99 +#edit{
  100 + width: 50%;
  101 +}
  102 +#result{
  103 + width: 50%;
  104 +}
  105 +.submit{
  106 + position: absolute;
  107 + top: 2px;
  108 + left: calc(50% - 82px);
  109 + z-index: 9999;
  110 + width: 80px;
  111 +}
  112 +</style>