Database UI

/views/vdatabase/Detail.html (7.6 KB)

  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
{% import (
"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller/cutil"
"{{{ .Package }}}/app/lib/database"
"{{{ .Package }}}/app/util"
"{{{ .Package }}}/views/components"
"{{{ .Package }}}/views/layout"
) %}

{% code type Detail struct {
layout.Basic
Mode string
Svc *database.Service
Recent database.DebugStatements
Sizes database.TableSizes
SQL string
Columns []string
Results [][]any
Timing int
Commit bool
} %}

{% func (p *Detail) Body(as *app.State, ps *cutil.PageState) %}
<div class="card">
<h3>{%= components.SVGRefIcon(`database`, ps) %}{%s p.Svc.Key %}</h3>
<div class="mt">
{%- if p.Svc.Tracing() == "" -%}
<em>tracing is disabled</em>
{%- else -%}
<em>tracing is enabled in [{%s p.Svc.Tracing() %}] mode</em>
{%- endif -%}
</div>
<div class="mt">
<a href="#modal-settings"><button>Tracing Settings</button></a>
{%= settingsModal(p.Svc) %}
{%- if p.Svc.Tracing() != "" -%}
<a href="/admin/database/{%s p.Svc.Key %}/recent"><button>Recent Activity</button></a>
{%- endif -%}
<a href="/admin/database/{%s p.Svc.Key %}/tables"><button>Tables</button></a>
<a href="/admin/database/{%s p.Svc.Key %}/analyze"><button>Analyze</button></a>
<a href="/admin/database/{%s p.Svc.Key %}/sql"><button>SQL</button></a>
</div>
</div>
{%- switch p.Mode -%}
{%- case "recent" -%}
{%= recentStatements(p.Recent, p.Svc, as, ps) %}
{%- case "tables" -%}
{%= tableSizes(p.Svc.Key, p.Sizes, as, ps) %}
{%- case "sql" -%}
{%= sqlEditor(p.SQL, p.Svc, p.Commit, p.Columns, p.Results, p.Timing, as, ps) %}
{%- endswitch -%}
{% endfunc %}

{% func recentStatements(recent database.DebugStatements, svc *database.Service, as *app.State, ps *cutil.PageState) %}
<div class="card">
<h3>Recent Activity</h3>
{%- if len(recent) == 0 -%}
{%- if svc.Tracing() == "" -%}
<em>Tracing is not enabled for this database</em>
{%- else -%}
<em>No recent statements</em>
{%- endif -%}
{%- else -%}
<div class="overflow full-width">
<table>
<thead>
<tr>
<th>SQL</th>
<th>Values</th>
<th>Count</th>
<th>Message</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
{%- for _, s := range recent -%}
<tr>
<td>
<a href="?idx={%d s.Index %}">{%s s.SQLTrimmed(100) %}</a>
</td>
<td>{%d len(s.Values) %}</td>
<td>{%d s.Count %}</td>
<td>{%s s.Message %}</td>
<td>{%s util.MicrosToMillis(s.Timing) %}</td>
</tr>
{%- endfor -%}
</tbody>
</table>
</div>
{%- endif -%}
</div>
{% endfunc %}

{% func tableSizes(key string, sizes database.TableSizes, as *app.State, ps *cutil.PageState) %}
<div class="card">
<h3>Table Sizes</h3>
<div class="overflow full-width">
<table>
<thead>
<tr>{{{ if .HasModule "postgres" }}}
<th>Schema</th>{{{ end }}}
<th>Name</th>
<th title="(estimated)">Rows*</th>{{{ if .HasModule "postgres" }}}
<th>Total</th>
<th>Index</th>
<th>Toast</th>
<th>Table</th>{{{ end }}}
</tr>
</thead>
<tbody>
{%- for _, size := range sizes -%}
<tr>{{{ if .HasModule "postgres" }}}
<td>{%s size.Schema %}</td>{{{ end }}}
<td><a href="/admin/database/{%s key %}/tables/{%s size.Schema %}/{%s size.Name %}">{%s size.Name %}</a></td>
<td>{%s size.Rows %}</td>{{{ if .HasModule "postgres" }}}
<td>{%s size.Total.String %}</td>
<td>{%s size.Index.String %}</td>
<td>{%s size.Toast.String %}</td>
<td>{%s size.Table.String %}</td>{{{ end }}}
</tr>
{%- endfor -%}
</tbody>
</table>
</div>
</div>
{% endfunc %}

{% func sqlEditor(sql string, svc *database.Service, commit bool, columns []string, results [][]any, timing int, as *app.State, ps *cutil.PageState) %}
<div class="card">
<h3>SQL Editor</h3>
<form method="post" action="/admin/database/{%s svc.Key %}/sql">
<div class="mt expanded">
<textarea name="sql" rows="12" placeholder="SQL statement">{%s sql %}</textarea>
</div>
{%- if svc.ReadOnly -%}
<input type="hidden" name="commit" value="false" />
{%- else -%}
<div class="mt">
<label><input type="checkbox" name="commit" value="true" {% if commit %}checked="checked"{% endif %}/> Commit Changes</label>
</div>
{%- endif -%}
<div class="mt">
<button type="submit" name="action" value="run">Run</button>
<button type="submit" name="action" value="analyze">Analyze</button>
</div>
</form>
</div>
{%- if results != nil -%}
<div class="card">
<div class="right">{%s util.MicrosToMillis(timing) %}</div>
<h3>Results</h3>
{%- if len(results) == 0 -%}
<em>No rows returned</em>
{%- else -%}
<div class="overflow full-width">
<table class="mt expanded">
<thead>
<tr>
{%- for _, c := range columns -%}
<th>{%s c %}</th>
{%- endfor -%}
</tr>
</thead>
<tbody>
{%- for _, row := range results -%}
<tr>
{%- for _, x := range row -%}
<td>{%v x %}</td>
{%- endfor -%}
</tr>
{%- endfor -%}
</tbody>
</table>
</div>
{%- endif -%}
</div>
{%- endif -%}
{% endfunc %}

{% func settingsModal(svc *database.Service) %}
<div id="modal-settings" class="modal" style="display: none;">
<a class="backdrop" href="#"></a>
<div class="modal-content">
<div class="modal-header">
<a href="#" class="modal-close">×</a>
<h2>Tracing Settings</h2>
</div>
<div class="modal-body">
<form action="/admin/database/{%s svc.Key %}/enable">
<div class="overflow full-width">
<table>
<tbody>
{%- code trc := svc.Tracing() %}
<tr>
<td><label><input type="radio" name="tracing" value=""{% if trc == `` %} checked="checked"{% endif %}> No Tracing</label></td>
<td><em>Fastest configuration, no tracing overhead</em></td>
</tr>
<tr>
<td><label><input type="radio" name="tracing" value="statement"{% if trc == `statement` %} checked="checked"{% endif %}> Save Queries</label></td>
<td><em>Save most recent 100 SQL statements with timing information</em></td>
</tr>
<tr>
<td><label><input type="radio" name="tracing" value="values"{% if trc == `values` %} checked="checked"{% endif %}> Save Results</label></td>
<td><em>Saves SQL, timing, and the results of the query</em></td>
</tr>
<tr>
<td><label><input type="radio" name="tracing" value="analyze"{% if trc == `analyze` %} checked="checked"{% endif %}> Analyze Queries</label></td>
<td><em>In addition to the above, runs an explain plan on each query</em></td>
</tr>
<tr>
<td colspan="2"><button>Submit</button></td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</div>
</div>
{% endfunc %}