-
Notifications
You must be signed in to change notification settings - Fork 7
/
Debugbar.vue
339 lines (299 loc) · 10.3 KB
/
Debugbar.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<script setup lang="ts">
import { createConsumer } from "@rails/actioncable"
import { computed, onMounted, reactive, ref } from "vue"
import {
CodeBracketIcon,
XCircleIcon,
ArrowDownLeftIcon,
TrashIcon,
PauseIcon,
PlayIcon,
CircleStackIcon,
ArrowsUpDownIcon,
CpuChipIcon,
} from "@heroicons/vue/16/solid"
import TabButton from "@/components/TabButton.vue"
import ModelsPanel from "@/components/panels/ModelsPanel.vue"
import QueriesPanel from "@/components/queries/QueriesPanel.vue"
import JobsPanel from "@/components/panels/JobsPanel.vue"
import LogsPanel from "@/components/panels/LogsPanel.vue"
import MessagesPanel from "@/components/panels/MessagesPanel.vue"
import { useRequestsStore } from "@/stores/RequestsStore.ts"
import { useConfigStore } from "@/stores/configStore.ts"
import CachePanel from "@/components/panels/CachePanel.vue"
import RequestPanel from "@/components/panels/RequestPanel.vue"
import JsonPanel from "@/components/panels/JsonPanel.vue"
import RubyLogo from "@/components/ui/RubyLogo.vue"
import Timing from "@/components/ui/Timing.vue"
let requestsStore = useRequestsStore()
let configStore = useConfigStore()
const header = ref(null)
const state = reactive({
activeTab: "",
minimized: false,
isResizing: false,
isPolling: configStore.config.mode === "poll",
height: configStore.config.height,
})
const isActive = computed(() => {
return state.activeTab != ""
})
const devMode = computed(() => {
return import.meta.env.DEV
})
const routeAlias = computed(() => {
return requestsStore.currentRequest?.meta.params.controller + "#" + requestsStore.currentRequest?.meta.params.action
})
let debugbarChannel = null
if (configStore.config.mode === "ws") {
debugbarChannel = createConsumer(configStore.config.actionCableUrl).subscriptions.create(
{ channel: configStore.config.cable.channelName },
{
connected() {
console.log("🟢 Connected to channel")
debugbarChannel.send({ ids: [] })
},
disconnected() {
console.log("🔴 Disconnected from channel")
},
received(data) {
if (data.length == 0) {
return
}
const ids = requestsStore.addRequests(data)
if (!isActive.value) {
requestsStore.setCurrentRequestById(ids[ids.length - 1])
}
setTimeout(() => {
debugbarChannel.send({ ids: ids })
}, 50)
},
}
)
} else if (configStore.config.mode === "poll") {
console.log(
`Using debugbar in "polling mode". Consider using "ws" mode for better performance (requires ActionCable).`
)
setInterval(() => {
if (!state.isPolling) {
return
}
fetch(configStore.config.pollUrl)
.then((response) => response.json())
.then((data) => {
if (data.length == 0) {
return
}
console.log(data)
const ids = requestsStore.addRequests(data)
if (!isActive.value) {
requestsStore.setCurrentRequestById(ids[ids.length - 1])
}
fetch(configStore.config.pollUrl + "/confirm", {
// mode: "no-cors",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ ids: ids }),
})
})
}, configStore.config.poll.interval)
} else {
console.log(`Using debugbar in "offline mode", ideal for demos using fixtures.`)
}
const clearRequests = () => {
console.log("Clearing requests")
state.activeTab = ""
requestsStore.clearRequests()
debugbarChannel?.send({ clear: true })
state.isPolling = true
}
const togglePolling = () => {
state.isPolling = !state.isPolling
}
// Resizing the debugbar
onMounted(() => {
window.onresize = function () {
if (window.innerHeight < state.height) {
state.height = window.innerHeight - header.value.clientHeight
}
}
document.onmousemove = function (e) {
if (!state.isResizing) {
return
}
state.height = window.innerHeight - e.clientY - header.value.clientHeight
}
document.onmouseup = function (_e) {
state.isResizing = false
}
})
const setActiveTab = (tab) => {
if (state.activeTab == tab) {
state.activeTab = "" // Close if you click on active tab
} else {
if (window.innerHeight < state.height) {
state.height = window.innerHeight - header.value.clientHeight * 2
}
state.activeTab = tab
}
}
</script>
<template>
<div
v-if="state.minimized"
@click="state.minimized = false"
class="z-[9999] text-stone-900 fixed left-0 bottom-0 bg-transparent cursor-pointer"
>
<div class="p-1 pt-1.5">
<ruby-logo />
</div>
</div>
<div
v-if="!state.minimized && requestsStore.currentRequest == null"
class="z-[9999] text-stone-900 fixed left-0 bottom-0 w-full"
>
<div class="h-0.5 bg-red-700 cursor-row-resize" />
<div class="flex items-center justify-between bg-stone-100 border-b border-stone-200">
<div class="px-5 py-1.5 italic">No request yet</div>
<div class="px-3">
<button @click="state.minimized = true" title="Hide in the corner">
<arrow-down-left-icon class="size-4" />
</button>
</div>
</div>
</div>
<div
v-if="!state.minimized && requestsStore.currentRequest"
class="z-[9999] text-stone-900 fixed left-0 bottom-0 w-full"
>
<div id="draggable-bar" @mousedown="state.isResizing = true" class="h-0.5 bg-red-700 cursor-row-resize" />
<div
id="debubgbar-header"
ref="header"
class="flex px-1 items-center justify-between bg-stone-100 border-b-2 border-stone-300"
>
<!-- Left -->
<div>
<div class="flex">
<div class="p-1 pt-1.5">
<ruby-logo />
</div>
<tab-button
v-for="(v, k) in requestsStore.currentRequest.dataForTabs"
key="k"
:label="v.label"
:count="v?.count"
:is-active="k === state.activeTab"
:disabled="v.count == 0"
@click="setActiveTab(k)"
>{{ v.label }}</tab-button
>
<button
v-if="devMode"
@click="setActiveTab('debug')"
class="px-3 py-1.5 text-stone-600"
:class="{ 'bg-stone-300': state.activeTab == 'debug' }"
>
<CodeBracketIcon class="size-4" />
</button>
</div>
</div>
<!-- Right -->
<div class="flex items-center space-x-3 pr-1">
<div class="flex space-x-2">
<timing :duration-ms="requestsStore.currentRequest.meta.db_runtime">
<circle-stack-icon class="text-stone-600 size-3" />
</timing>
<timing :duration-ms="requestsStore.currentRequest.meta.cpu_time">
<cpu-chip-icon class="text-stone-600 size-3" />
</timing>
<timing
:duration-ms="requestsStore.currentRequest.meta.duration"
:too-slow-threshold="1000"
:slow-threshold="750"
class="font-bold"
>
<arrows-up-down-icon class="text-stone-800 size-4" />
</timing>
</div>
<div @click="setActiveTab('request')" class="flex space-x-2 cursor-pointer">
<span class="text-sm text-stone-600 font-medium tracking-wide">
{{ routeAlias }}
</span>
<span
class="px-1 py-0.5 rounded text-xs"
:class="{
'bg-green-600 text-white': requestsStore.currentRequest.meta.status < 300,
'bg-amber-600 text-white':
requestsStore.currentRequest.meta.status >= 400 && requestsStore.currentRequest.meta.status < 500,
'bg-red-600 text-white': requestsStore.currentRequest.meta.status >= 500,
}"
>{{ requestsStore.currentRequest.meta.status }}</span
>
</div>
<select
class="px-2 py-1.5 bg-white border border-stone-200 rounded w-[330px] text-sm"
name="current_request_id"
@change="
(event) => {
const target = event.target as HTMLSelectElement
requestsStore.setCurrentRequestById(target.value)
}
"
>
<option
v-for="r in requestsStore.requests"
:selected="requestsStore.currentRequest.id == r.id"
v-text="r.pathWithVerb"
:value="r.id"
/>
</select>
<div class="flex items-center pl-1 space-x-2">
<button
v-if="configStore.config.mode == 'poll'"
@click="togglePolling"
:title="state.isPolling ? 'Pause polling' : 'Resume polling'"
>
<pause-icon v-if="state.isPolling" class="size-4" />
<play-icon v-if="!state.isPolling" class="size-4" />
</button>
<button @click="clearRequests" title="Clear all requests (frontend and backend)">
<trash-icon class="size-3" />
</button>
<button @click="state.minimized = true" title="Hide in the corner">
<arrow-down-left-icon class="size-4" />
</button>
<button :disabled="!isActive" @click="state.activeTab = ''" title="Close">
<x-circle-icon class="size-4" />
</button>
</div>
</div>
</div>
<div
ref="body"
id="debugbar-body"
class="bg-white overflow-scroll"
v-if="state.activeTab != ''"
:style="`height: ${state.height}px`"
>
<request-panel v-if="state.activeTab == 'request'" :request="requestsStore.currentRequest" />
<messages-panel v-if="state.activeTab == 'messages'" :messages="requestsStore.currentRequest?.messages" />
<models-panel
v-if="state.activeTab == 'models'"
:models="requestsStore.currentRequest?.models"
:count="requestsStore.currentRequest?.modelsCount"
/>
<queries-panel v-if="state.activeTab == 'queries'" :current-request="requestsStore.currentRequest" />
<jobs-panel v-if="state.activeTab == 'jobs'" :jobs="requestsStore.currentRequest?.jobs" />
<cache-panel v-if="state.activeTab == 'cache'" :cache="requestsStore.currentRequest?.cache" />
<logs-panel v-if="state.activeTab == 'logs'" :logs="requestsStore.currentRequest?.logs" />
<json-panel
v-if="devMode && state.activeTab == 'debug'"
:current-request="requestsStore.currentRequest"
class="px-3 py-2"
/>
</div>
</div>
</template>