Initial commit: SRDB - High-performance LSM-Tree database
- Core engine with MemTable, SST, WAL - B+Tree indexing for SST files - Leveled compaction strategy - Multi-table database management - Schema validation and secondary indexes - Query builder with complex conditions - Web UI with HTMX for data visualization - Command-line tools for diagnostics
This commit is contained in:
552
webui/htmx.go
Normal file
552
webui/htmx.go
Normal file
@@ -0,0 +1,552 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// HTML 渲染辅助函数
|
||||
|
||||
// renderTablesHTML 渲染表列表 HTML
|
||||
func renderTablesHTML(tables []TableListItem) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
for _, table := range tables {
|
||||
buf.WriteString(`<div class="table-item" data-table="`)
|
||||
buf.WriteString(html.EscapeString(table.Name))
|
||||
buf.WriteString(`">`)
|
||||
buf.WriteString(`<div class="table-header" onclick="selectTable('`)
|
||||
buf.WriteString(html.EscapeString(table.Name))
|
||||
buf.WriteString(`')">`)
|
||||
|
||||
// 左侧:展开图标和表名
|
||||
buf.WriteString(`<div class="table-header-left">`)
|
||||
buf.WriteString(`<span class="expand-icon" onclick="event.stopPropagation(); toggleExpand('`)
|
||||
buf.WriteString(html.EscapeString(table.Name))
|
||||
buf.WriteString(`')">▶</span>`)
|
||||
buf.WriteString(`<span class="table-name">`)
|
||||
buf.WriteString(html.EscapeString(table.Name))
|
||||
buf.WriteString(`</span></div>`)
|
||||
|
||||
// 右侧:字段数量
|
||||
buf.WriteString(`<span class="table-count">`)
|
||||
buf.WriteString(formatCount(int64(len(table.Fields))))
|
||||
buf.WriteString(` fields</span>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// Schema 字段列表(默认隐藏)
|
||||
if len(table.Fields) > 0 {
|
||||
buf.WriteString(`<div class="schema-fields">`)
|
||||
for _, field := range table.Fields {
|
||||
buf.WriteString(`<div class="field-item">`)
|
||||
buf.WriteString(`<span class="field-name">`)
|
||||
buf.WriteString(html.EscapeString(field.Name))
|
||||
buf.WriteString(`</span>`)
|
||||
buf.WriteString(`<span class="field-type">`)
|
||||
buf.WriteString(html.EscapeString(field.Type))
|
||||
buf.WriteString(`</span>`)
|
||||
if field.Indexed {
|
||||
buf.WriteString(`<span class="field-indexed">●indexed</span>`)
|
||||
}
|
||||
buf.WriteString(`</div>`)
|
||||
}
|
||||
buf.WriteString(`</div>`)
|
||||
}
|
||||
|
||||
buf.WriteString(`</div>`)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// renderDataViewHTML 渲染数据视图 HTML
|
||||
func renderDataViewHTML(tableName string, schema SchemaInfo, tableData TableDataResponse) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
// 标题
|
||||
buf.WriteString(`<h2>`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`</h2>`)
|
||||
|
||||
// 视图切换标签
|
||||
buf.WriteString(`<div class="view-tabs">`)
|
||||
buf.WriteString(`<button class="view-tab active" onclick="switchView('`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`', 'data')">Data</button>`)
|
||||
buf.WriteString(`<button class="view-tab" onclick="switchView('`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`', 'manifest')">Manifest / LSM-Tree</button>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// Schema 部分
|
||||
if len(schema.Fields) > 0 {
|
||||
buf.WriteString(`<div class="schema-section">`)
|
||||
buf.WriteString(`<h3>Schema <span style="font-size: 12px; font-weight: 400; color: var(--text-secondary);">(点击字段卡片选择要显示的列)</span></h3>`)
|
||||
buf.WriteString(`<div class="schema-grid">`)
|
||||
for _, field := range schema.Fields {
|
||||
buf.WriteString(`<div class="schema-field-card selected" data-column="`)
|
||||
buf.WriteString(html.EscapeString(field.Name))
|
||||
buf.WriteString(`" onclick="toggleColumn('`)
|
||||
buf.WriteString(html.EscapeString(field.Name))
|
||||
buf.WriteString(`')">`)
|
||||
buf.WriteString(`<div class="field-item">`)
|
||||
buf.WriteString(`<span class="field-name">`)
|
||||
buf.WriteString(html.EscapeString(field.Name))
|
||||
buf.WriteString(`</span>`)
|
||||
buf.WriteString(`<span class="field-type">`)
|
||||
buf.WriteString(html.EscapeString(field.Type))
|
||||
buf.WriteString(`</span>`)
|
||||
if field.Indexed {
|
||||
buf.WriteString(`<span class="field-indexed">●indexed</span>`)
|
||||
}
|
||||
buf.WriteString(`</div>`)
|
||||
buf.WriteString(`<div class="field-comment">`)
|
||||
if field.Comment != "" {
|
||||
buf.WriteString(html.EscapeString(field.Comment))
|
||||
}
|
||||
buf.WriteString(`</div>`)
|
||||
buf.WriteString(`</div>`)
|
||||
}
|
||||
buf.WriteString(`</div>`)
|
||||
buf.WriteString(`</div>`)
|
||||
}
|
||||
|
||||
// 数据表格
|
||||
buf.WriteString(`<h3>Data (`)
|
||||
buf.WriteString(formatCount(tableData.TotalRows))
|
||||
buf.WriteString(` rows)</h3>`)
|
||||
|
||||
if len(tableData.Data) == 0 {
|
||||
buf.WriteString(`<div class="empty"><p>No data available</p></div>`)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// 获取列并排序:_seq 第1列,_time 倒数第2列
|
||||
columns := []string{}
|
||||
otherColumns := []string{}
|
||||
hasSeq := false
|
||||
hasTime := false
|
||||
|
||||
if len(tableData.Data) > 0 {
|
||||
for key := range tableData.Data[0] {
|
||||
if !strings.HasSuffix(key, "_truncated") {
|
||||
if key == "_seq" {
|
||||
hasSeq = true
|
||||
} else if key == "_time" {
|
||||
hasTime = true
|
||||
} else {
|
||||
otherColumns = append(otherColumns, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按顺序组装:_seq, 其他列, _time
|
||||
if hasSeq {
|
||||
columns = append(columns, "_seq")
|
||||
}
|
||||
columns = append(columns, otherColumns...)
|
||||
if hasTime {
|
||||
columns = append(columns, "_time")
|
||||
}
|
||||
|
||||
// 表格
|
||||
buf.WriteString(`<div class="table-wrapper">`)
|
||||
buf.WriteString(`<table class="data-table">`)
|
||||
buf.WriteString(`<thead><tr>`)
|
||||
for _, col := range columns {
|
||||
buf.WriteString(`<th data-column="`)
|
||||
buf.WriteString(html.EscapeString(col))
|
||||
buf.WriteString(`" title="`)
|
||||
buf.WriteString(html.EscapeString(col))
|
||||
buf.WriteString(`">`)
|
||||
buf.WriteString(html.EscapeString(col))
|
||||
buf.WriteString(`</th>`)
|
||||
}
|
||||
buf.WriteString(`<th style="width: 80px;">Actions</th>`)
|
||||
buf.WriteString(`</tr></thead>`)
|
||||
|
||||
buf.WriteString(`<tbody>`)
|
||||
for _, row := range tableData.Data {
|
||||
buf.WriteString(`<tr>`)
|
||||
for _, col := range columns {
|
||||
value := row[col]
|
||||
buf.WriteString(`<td data-column="`)
|
||||
buf.WriteString(html.EscapeString(col))
|
||||
buf.WriteString(`" onclick="showCellContent('`)
|
||||
buf.WriteString(escapeJSString(fmt.Sprintf("%v", value)))
|
||||
buf.WriteString(`')" title="Click to view full content">`)
|
||||
buf.WriteString(html.EscapeString(fmt.Sprintf("%v", value)))
|
||||
|
||||
// 检查是否被截断
|
||||
if truncated, ok := row[col+"_truncated"]; ok && truncated == true {
|
||||
buf.WriteString(`<span class="truncated-icon" title="This field is truncated">✂️</span>`)
|
||||
}
|
||||
|
||||
buf.WriteString(`</td>`)
|
||||
}
|
||||
|
||||
// Actions 列
|
||||
buf.WriteString(`<td style="text-align: center;">`)
|
||||
buf.WriteString(`<button class="row-detail-btn" onclick="showRowDetail('`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`', `)
|
||||
buf.WriteString(fmt.Sprintf("%v", row["_seq"]))
|
||||
buf.WriteString(`)" title="View full row data">Detail</button>`)
|
||||
buf.WriteString(`</td>`)
|
||||
|
||||
buf.WriteString(`</tr>`)
|
||||
}
|
||||
buf.WriteString(`</tbody>`)
|
||||
buf.WriteString(`</table>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// 分页
|
||||
buf.WriteString(renderPagination(tableData))
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// renderManifestViewHTML 渲染 Manifest 视图 HTML
|
||||
func renderManifestViewHTML(tableName string, manifest ManifestResponse) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
// 标题
|
||||
buf.WriteString(`<h2>`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`</h2>`)
|
||||
|
||||
// 视图切换标签
|
||||
buf.WriteString(`<div class="view-tabs">`)
|
||||
buf.WriteString(`<button class="view-tab" onclick="switchView('`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`', 'data')">Data</button>`)
|
||||
buf.WriteString(`<button class="view-tab active" onclick="switchView('`)
|
||||
buf.WriteString(html.EscapeString(tableName))
|
||||
buf.WriteString(`', 'manifest')">Manifest / LSM-Tree</button>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// 标题和控制按钮
|
||||
buf.WriteString(`<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;">`)
|
||||
buf.WriteString(`<h3>LSM-Tree Structure</h3>`)
|
||||
buf.WriteString(`<div class="control-buttons">`)
|
||||
buf.WriteString(`<button>📖 Expand All</button>`)
|
||||
buf.WriteString(`<button>📕 Collapse All</button>`)
|
||||
buf.WriteString(`</div>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// 统计卡片
|
||||
totalLevels := len(manifest.Levels)
|
||||
totalFiles := 0
|
||||
totalSize := int64(0)
|
||||
for _, level := range manifest.Levels {
|
||||
totalFiles += level.FileCount
|
||||
totalSize += level.TotalSize
|
||||
}
|
||||
|
||||
buf.WriteString(`<div class="manifest-stats">`)
|
||||
|
||||
// Active Levels
|
||||
buf.WriteString(`<div class="stat-card">`)
|
||||
buf.WriteString(`<div class="stat-label">Active Levels</div>`)
|
||||
buf.WriteString(`<div class="stat-value">`)
|
||||
buf.WriteString(fmt.Sprintf("%d", totalLevels))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
// Total Files
|
||||
buf.WriteString(`<div class="stat-card">`)
|
||||
buf.WriteString(`<div class="stat-label">Total Files</div>`)
|
||||
buf.WriteString(`<div class="stat-value">`)
|
||||
buf.WriteString(fmt.Sprintf("%d", totalFiles))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
// Total Size
|
||||
buf.WriteString(`<div class="stat-card">`)
|
||||
buf.WriteString(`<div class="stat-label">Total Size</div>`)
|
||||
buf.WriteString(`<div class="stat-value">`)
|
||||
buf.WriteString(formatBytes(totalSize))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
// Next File Number
|
||||
buf.WriteString(`<div class="stat-card">`)
|
||||
buf.WriteString(`<div class="stat-label">Next File Number</div>`)
|
||||
buf.WriteString(`<div class="stat-value">`)
|
||||
buf.WriteString(fmt.Sprintf("%d", manifest.NextFileNumber))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
// Last Sequence
|
||||
buf.WriteString(`<div class="stat-card">`)
|
||||
buf.WriteString(`<div class="stat-label">Last Sequence</div>`)
|
||||
buf.WriteString(`<div class="stat-value">`)
|
||||
buf.WriteString(fmt.Sprintf("%d", manifest.LastSequence))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
// Total Compactions
|
||||
buf.WriteString(`<div class="stat-card">`)
|
||||
buf.WriteString(`<div class="stat-label">Total Compactions</div>`)
|
||||
buf.WriteString(`<div class="stat-value">`)
|
||||
totalCompactions := 0
|
||||
if manifest.CompactionStats != nil {
|
||||
if tc, ok := manifest.CompactionStats["total_compactions"]; ok {
|
||||
if tcInt, ok := tc.(float64); ok {
|
||||
totalCompactions = int(tcInt)
|
||||
}
|
||||
}
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf("%d", totalCompactions))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// 渲染所有层级(L0-L6)
|
||||
for i := 0; i <= 6; i++ {
|
||||
var level *LevelInfo
|
||||
for j := range manifest.Levels {
|
||||
if manifest.Levels[j].Level == i {
|
||||
level = &manifest.Levels[j]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if level == nil {
|
||||
// 创建空层级
|
||||
level = &LevelInfo{
|
||||
Level: i,
|
||||
FileCount: 0,
|
||||
TotalSize: 0,
|
||||
Score: 0,
|
||||
Files: []FileInfo{},
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteString(renderLevelCard(*level))
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// renderLevelCard 渲染层级卡片
|
||||
func renderLevelCard(level LevelInfo) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
scoreClass := "normal"
|
||||
if level.Score >= 1.0 {
|
||||
scoreClass = "critical"
|
||||
} else if level.Score >= 0.8 {
|
||||
scoreClass = "warning"
|
||||
}
|
||||
|
||||
buf.WriteString(`<div class="level-card" data-level="`)
|
||||
buf.WriteString(fmt.Sprintf("%d", level.Level))
|
||||
buf.WriteString(`">`)
|
||||
buf.WriteString(`<div class="level-header" onclick="toggleLevel(`)
|
||||
buf.WriteString(fmt.Sprintf("%d", level.Level))
|
||||
buf.WriteString(`)">`)
|
||||
|
||||
// 左侧:展开图标和标题
|
||||
buf.WriteString(`<div style="display: flex; align-items: center; gap: 10px;">`)
|
||||
buf.WriteString(`<span class="expand-icon">▶</span>`)
|
||||
buf.WriteString(`<div class="level-title">Level `)
|
||||
buf.WriteString(fmt.Sprintf("%d", level.Level))
|
||||
buf.WriteString(`</div></div>`)
|
||||
|
||||
// 右侧:统计信息
|
||||
buf.WriteString(`<div class="level-stats">`)
|
||||
buf.WriteString(`<span>`)
|
||||
buf.WriteString(fmt.Sprintf("%d", level.FileCount))
|
||||
buf.WriteString(` files</span>`)
|
||||
buf.WriteString(`<span>`)
|
||||
buf.WriteString(formatBytes(level.TotalSize))
|
||||
buf.WriteString(`</span>`)
|
||||
buf.WriteString(`<span class="score-badge `)
|
||||
buf.WriteString(scoreClass)
|
||||
buf.WriteString(`">Score: `)
|
||||
buf.WriteString(fmt.Sprintf("%.2f", level.Score))
|
||||
buf.WriteString(`</span>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
// 文件列表(默认隐藏)
|
||||
buf.WriteString(`<div class="file-list">`)
|
||||
if len(level.Files) == 0 {
|
||||
buf.WriteString(`<div class="empty-files">No files in this level</div>`)
|
||||
} else {
|
||||
for _, file := range level.Files {
|
||||
buf.WriteString(`<div class="file-card">`)
|
||||
buf.WriteString(`<div class="file-header">`)
|
||||
buf.WriteString(`<span>File #`)
|
||||
buf.WriteString(fmt.Sprintf("%d", file.FileNumber))
|
||||
buf.WriteString(`</span>`)
|
||||
buf.WriteString(`<b>`)
|
||||
buf.WriteString(formatBytes(file.FileSize))
|
||||
buf.WriteString(`</b>`)
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
buf.WriteString(`<div class="file-detail">`)
|
||||
buf.WriteString(`<span>Key Range:</span>`)
|
||||
buf.WriteString(`<span>`)
|
||||
buf.WriteString(fmt.Sprintf("%d - %d", file.MinKey, file.MaxKey))
|
||||
buf.WriteString(`</span></div>`)
|
||||
|
||||
buf.WriteString(`<div class="file-detail">`)
|
||||
buf.WriteString(`<span>Rows:</span>`)
|
||||
buf.WriteString(`<span>`)
|
||||
buf.WriteString(formatCount(file.RowCount))
|
||||
buf.WriteString(`</span></div>`)
|
||||
|
||||
buf.WriteString(`</div>`)
|
||||
}
|
||||
}
|
||||
buf.WriteString(`</div>`)
|
||||
|
||||
buf.WriteString(`</div>`)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// renderPagination 渲染分页 HTML
|
||||
func renderPagination(data TableDataResponse) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
buf.WriteString(`<div class="pagination">`)
|
||||
|
||||
// 页大小选择器
|
||||
buf.WriteString(`<select onchange="changePageSize(this.value)">`)
|
||||
for _, size := range []int{10, 20, 50, 100} {
|
||||
buf.WriteString(`<option value="`)
|
||||
buf.WriteString(fmt.Sprintf("%d", size))
|
||||
buf.WriteString(`"`)
|
||||
if int64(size) == data.PageSize {
|
||||
buf.WriteString(` selected`)
|
||||
}
|
||||
buf.WriteString(`>`)
|
||||
buf.WriteString(fmt.Sprintf("%d", size))
|
||||
buf.WriteString(` / page</option>`)
|
||||
}
|
||||
buf.WriteString(`</select>`)
|
||||
|
||||
// 上一页按钮
|
||||
buf.WriteString(`<button onclick="changePage(-1)"`)
|
||||
if data.Page <= 1 {
|
||||
buf.WriteString(` disabled`)
|
||||
}
|
||||
buf.WriteString(`>Previous</button>`)
|
||||
|
||||
// 页码信息
|
||||
buf.WriteString(`<span>Page `)
|
||||
buf.WriteString(fmt.Sprintf("%d", data.Page))
|
||||
buf.WriteString(` of `)
|
||||
buf.WriteString(fmt.Sprintf("%d", data.TotalPages))
|
||||
buf.WriteString(` (`)
|
||||
buf.WriteString(formatCount(data.TotalRows))
|
||||
buf.WriteString(` rows)</span>`)
|
||||
|
||||
// 跳转输入框
|
||||
buf.WriteString(`<input type="number" min="1" max="`)
|
||||
buf.WriteString(fmt.Sprintf("%d", data.TotalPages))
|
||||
buf.WriteString(`" placeholder="Jump to" onkeydown="if(event.key==='Enter') jumpToPage(this.value)">`)
|
||||
|
||||
// Go 按钮
|
||||
buf.WriteString(`<button onclick="jumpToPage(this.previousElementSibling.value)">Go</button>`)
|
||||
|
||||
// 下一页按钮
|
||||
buf.WriteString(`<button onclick="changePage(1)"`)
|
||||
if data.Page >= data.TotalPages {
|
||||
buf.WriteString(` disabled`)
|
||||
}
|
||||
buf.WriteString(`>Next</button>`)
|
||||
|
||||
buf.WriteString(`</div>`)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// formatBytes 格式化字节数
|
||||
func formatBytes(bytes int64) string {
|
||||
if bytes == 0 {
|
||||
return "0 B"
|
||||
}
|
||||
const k = 1024
|
||||
sizes := []string{"B", "KB", "MB", "GB", "TB"}
|
||||
i := 0
|
||||
size := float64(bytes)
|
||||
for size >= k && i < len(sizes)-1 {
|
||||
size /= k
|
||||
i++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %s", size, sizes[i])
|
||||
}
|
||||
|
||||
// formatCount 格式化数量(K/M)
|
||||
func formatCount(count int64) string {
|
||||
if count >= 1000000 {
|
||||
return fmt.Sprintf("%.1fM", float64(count)/1000000)
|
||||
}
|
||||
if count >= 1000 {
|
||||
return fmt.Sprintf("%.1fK", float64(count)/1000)
|
||||
}
|
||||
return fmt.Sprintf("%d", count)
|
||||
}
|
||||
|
||||
// escapeJSString 转义 JavaScript 字符串
|
||||
func escapeJSString(s string) string {
|
||||
s = strings.ReplaceAll(s, `\`, `\\`)
|
||||
s = strings.ReplaceAll(s, `'`, `\'`)
|
||||
s = strings.ReplaceAll(s, `"`, `\"`)
|
||||
s = strings.ReplaceAll(s, "\n", `\n`)
|
||||
s = strings.ReplaceAll(s, "\r", `\r`)
|
||||
s = strings.ReplaceAll(s, "\t", `\t`)
|
||||
return s
|
||||
}
|
||||
|
||||
// 数据结构定义
|
||||
type TableListItem struct {
|
||||
Name string `json:"name"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Fields []FieldInfo `json:"fields"`
|
||||
}
|
||||
|
||||
type FieldInfo struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Indexed bool `json:"indexed"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
type SchemaInfo struct {
|
||||
Name string `json:"name"`
|
||||
Fields []FieldInfo `json:"fields"`
|
||||
}
|
||||
|
||||
type TableDataResponse struct {
|
||||
Data []map[string]any `json:"data"`
|
||||
Page int64 `json:"page"`
|
||||
PageSize int64 `json:"pageSize"`
|
||||
TotalRows int64 `json:"totalRows"`
|
||||
TotalPages int64 `json:"totalPages"`
|
||||
}
|
||||
|
||||
type ManifestResponse struct {
|
||||
Levels []LevelInfo `json:"levels"`
|
||||
NextFileNumber int64 `json:"next_file_number"`
|
||||
LastSequence int64 `json:"last_sequence"`
|
||||
CompactionStats map[string]any `json:"compaction_stats"`
|
||||
}
|
||||
|
||||
type LevelInfo struct {
|
||||
Level int `json:"level"`
|
||||
FileCount int `json:"file_count"`
|
||||
TotalSize int64 `json:"total_size"`
|
||||
Score float64 `json:"score"`
|
||||
Files []FileInfo `json:"files"`
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
FileNumber int64 `json:"file_number"`
|
||||
Level int `json:"level"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
MinKey int64 `json:"min_key"`
|
||||
MaxKey int64 `json:"max_key"`
|
||||
RowCount int64 `json:"row_count"`
|
||||
}
|
||||
903
webui/static/css/styles.css
Normal file
903
webui/static/css/styles.css
Normal file
@@ -0,0 +1,903 @@
|
||||
/* SRDB WebUI - Modern Design */
|
||||
|
||||
:root {
|
||||
/* 主色调 - 优雅的紫蓝色 */
|
||||
--primary: #6366f1;
|
||||
--primary-dark: #4f46e5;
|
||||
--primary-light: #818cf8;
|
||||
--primary-bg: rgba(99, 102, 241, 0.1);
|
||||
|
||||
/* 背景色 */
|
||||
--bg-main: #0f0f1a;
|
||||
--bg-surface: #1a1a2e;
|
||||
--bg-elevated: #222236;
|
||||
--bg-hover: #2a2a3e;
|
||||
|
||||
/* 文字颜色 */
|
||||
--text-primary: #ffffff;
|
||||
--text-secondary: #a0a0b0;
|
||||
--text-tertiary: #6b6b7b;
|
||||
|
||||
/* 边框和分隔线 */
|
||||
--border-color: rgba(255, 255, 255, 0.1);
|
||||
--border-hover: rgba(255, 255, 255, 0.2);
|
||||
|
||||
/* 状态颜色 */
|
||||
--success: #10b981;
|
||||
--warning: #f59e0b;
|
||||
--danger: #ef4444;
|
||||
--info: #3b82f6;
|
||||
|
||||
/* 阴影 */
|
||||
--shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
|
||||
--shadow-md:
|
||||
0 4px 6px -1px rgba(0, 0, 0, 0.4), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
|
||||
--shadow-lg:
|
||||
0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.3);
|
||||
--shadow-xl:
|
||||
0 20px 25px -5px rgba(0, 0, 0, 0.5), 0 10px 10px -5px rgba(0, 0, 0, 0.3);
|
||||
|
||||
/* 圆角 */
|
||||
--radius-sm: 6px;
|
||||
--radius-md: 8px;
|
||||
--radius-lg: 12px;
|
||||
--radius-xl: 16px;
|
||||
|
||||
/* 过渡 */
|
||||
--transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family:
|
||||
"Inter",
|
||||
-apple-system,
|
||||
BlinkMacSystemFont,
|
||||
"Segoe UI",
|
||||
Roboto,
|
||||
sans-serif;
|
||||
background: var(--bg-main);
|
||||
color: var(--text-primary);
|
||||
line-height: 1.6;
|
||||
font-size: 14px;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
/* 布局 */
|
||||
.container {
|
||||
display: flex;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* 侧边栏 */
|
||||
.sidebar {
|
||||
width: 280px;
|
||||
background: var(--bg-surface);
|
||||
border-right: 1px solid var(--border-color);
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
padding: 16px 12px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.sidebar::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
}
|
||||
|
||||
.sidebar::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.sidebar::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.sidebar::-webkit-scrollbar-thumb:hover {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
}
|
||||
|
||||
.sidebar h1 {
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
letter-spacing: -0.02em;
|
||||
background: linear-gradient(135deg, var(--primary-light), var(--primary));
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
/* 主内容区 */
|
||||
.main {
|
||||
flex: 1;
|
||||
padding: 20px;
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
background: var(--bg-main);
|
||||
}
|
||||
|
||||
.main h2 {
|
||||
font-size: 24px;
|
||||
font-weight: 700;
|
||||
margin-bottom: 16px;
|
||||
background: linear-gradient(
|
||||
135deg,
|
||||
var(--text-primary) 0%,
|
||||
var(--primary-light) 100%
|
||||
);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
|
||||
.main h3 {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 12px;
|
||||
margin-top: 20px;
|
||||
color: var(--text-primary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.main h3::before {
|
||||
content: "";
|
||||
width: 3px;
|
||||
height: 18px;
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.main::-webkit-scrollbar {
|
||||
width: 8px;
|
||||
}
|
||||
|
||||
.main::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.main::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.main::-webkit-scrollbar-thumb:hover {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
}
|
||||
|
||||
/* 表列表卡片 */
|
||||
.table-item {
|
||||
margin-bottom: 6px;
|
||||
border-radius: var(--radius-md);
|
||||
overflow: hidden;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.table-header {
|
||||
padding: 10px 12px;
|
||||
background: var(--bg-elevated);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-md);
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.table-header:hover {
|
||||
background: var(--bg-hover);
|
||||
border-color: var(--border-hover);
|
||||
/*transform: translateX(2px);*/
|
||||
}
|
||||
|
||||
.table-header.selected,
|
||||
.table-item.selected .table-header {
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-dark));
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 3px var(--primary-bg);
|
||||
}
|
||||
|
||||
.table-header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.expand-icon {
|
||||
font-size: 10px;
|
||||
color: var(--text-secondary);
|
||||
transition: var(--transition);
|
||||
user-select: none;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.expand-icon.expanded {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
.table-name {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.table-count {
|
||||
font-size: 11px;
|
||||
font-weight: 500;
|
||||
padding: 3px 8px;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
color: var(--text-secondary);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.table-item.selected .table-count {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
color: white;
|
||||
}
|
||||
|
||||
/* Schema 字段列表 */
|
||||
.schema-fields {
|
||||
display: none;
|
||||
margin-top: 8px;
|
||||
padding: 10px 12px;
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
border-left: 2px solid var(--primary);
|
||||
border-radius: var(--radius-md);
|
||||
gap: 6px;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.field-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
padding: 4px 0;
|
||||
}
|
||||
|
||||
.field-name {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: var(--text-primary);
|
||||
min-width: 90px;
|
||||
}
|
||||
|
||||
.field-type {
|
||||
font-size: 12px;
|
||||
font-family: "SF Mono", Monaco, monospace;
|
||||
color: var(--primary-light);
|
||||
background: rgba(99, 102, 241, 0.15);
|
||||
padding: 2px 8px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.field-indexed {
|
||||
font-size: 10px;
|
||||
font-weight: 600;
|
||||
color: var(--success);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
}
|
||||
|
||||
.field-comment {
|
||||
font-size: 12px;
|
||||
color: #999;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
/* 视图切换标签 */
|
||||
.view-tabs {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin-bottom: 20px;
|
||||
padding: 4px;
|
||||
background: var(--bg-surface);
|
||||
border-radius: var(--radius-lg);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: var(--shadow-sm);
|
||||
}
|
||||
|
||||
.view-tab {
|
||||
padding: 10px 20px;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: var(--radius-md);
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary);
|
||||
transition: var(--transition);
|
||||
position: relative;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.view-tab:hover {
|
||||
color: var(--text-primary);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.view-tab.active {
|
||||
color: white;
|
||||
background: linear-gradient(
|
||||
135deg,
|
||||
var(--primary) 0%,
|
||||
var(--primary-dark) 100%
|
||||
);
|
||||
box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
|
||||
}
|
||||
|
||||
/* Schema 展示 */
|
||||
/*.schema-section {
|
||||
background: linear-gradient(
|
||||
135deg,
|
||||
rgba(99, 102, 241, 0.05) 0%,
|
||||
rgba(99, 102, 241, 0.02) 100%
|
||||
);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 18px;
|
||||
margin-bottom: 20px;
|
||||
box-shadow: var(--shadow-sm);
|
||||
}*/
|
||||
|
||||
.schema-section h3 {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 14px;
|
||||
margin-top: 0;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.schema-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.schema-field-card {
|
||||
background: var(--bg-elevated);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-md);
|
||||
padding: 12px;
|
||||
transition: var(--transition);
|
||||
cursor: pointer;
|
||||
position: relative;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.schema-field-card.selected {
|
||||
opacity: 1;
|
||||
border-color: var(--primary);
|
||||
background: linear-gradient(
|
||||
135deg,
|
||||
rgba(99, 102, 241, 0.1) 0%,
|
||||
rgba(99, 102, 241, 0.05) 100%
|
||||
);
|
||||
}
|
||||
|
||||
/*.schema-field-card::after {
|
||||
content: "✓";
|
||||
position: absolute;
|
||||
bottom: 8px;
|
||||
right: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
color: var(--primary);
|
||||
opacity: 0;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.schema-field-card.selected::after {
|
||||
opacity: 1;
|
||||
}*/
|
||||
|
||||
.schema-field-card:hover {
|
||||
border-color: var(--primary-light);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: var(--shadow-md);
|
||||
}
|
||||
|
||||
/* 数据表格 */
|
||||
.table-wrapper {
|
||||
overflow-x: auto;
|
||||
margin-bottom: 16px;
|
||||
border-radius: var(--radius-lg);
|
||||
border: 1px solid var(--border-color);
|
||||
background: var(--bg-surface);
|
||||
}
|
||||
|
||||
.data-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.data-table th {
|
||||
background: var(--bg-elevated);
|
||||
padding: 10px 12px;
|
||||
text-align: left;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.data-table td {
|
||||
padding: 10px 12px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
font-size: 13px;
|
||||
color: var(--text-primary);
|
||||
max-width: 400px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.data-table tbody tr {
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.data-table tbody tr:hover {
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
}
|
||||
|
||||
.data-table tbody tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
/* 分页 */
|
||||
.pagination {
|
||||
margin-top: 16px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.pagination button,
|
||||
.pagination select,
|
||||
.pagination input {
|
||||
padding: 8px 12px;
|
||||
background: var(--bg-surface);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-md);
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.pagination button:hover:not(:disabled) {
|
||||
background: var(--primary);
|
||||
border-color: var(--primary);
|
||||
box-shadow: var(--shadow-md);
|
||||
}
|
||||
|
||||
.pagination button:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.pagination input[type="number"] {
|
||||
width: 80px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.pagination select {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* Manifest / LSM-Tree */
|
||||
.level-card {
|
||||
background: var(--bg-surface);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 14px;
|
||||
margin-bottom: 12px;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.level-card:hover {
|
||||
border-color: var(--border-hover);
|
||||
box-shadow: var(--shadow-md);
|
||||
}
|
||||
|
||||
.level-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.level-title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
background: linear-gradient(
|
||||
135deg,
|
||||
var(--text-primary),
|
||||
var(--text-secondary)
|
||||
);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
}
|
||||
|
||||
.level-stats {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.score-badge {
|
||||
padding: 4px 12px;
|
||||
border-radius: 12px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.score-badge.normal {
|
||||
background: rgba(16, 185, 129, 0.15);
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.score-badge.warning {
|
||||
background: rgba(245, 158, 11, 0.15);
|
||||
color: var(--warning);
|
||||
}
|
||||
|
||||
.score-badge.critical {
|
||||
background: rgba(239, 68, 68, 0.15);
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.file-list {
|
||||
display: none;
|
||||
margin-top: 12px;
|
||||
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
|
||||
gap: 10px;
|
||||
padding-top: 8px;
|
||||
/*border-top: 1px solid var(--border-color);*/
|
||||
}
|
||||
|
||||
.file-card {
|
||||
background: var(--bg-elevated);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-md);
|
||||
padding: 12px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.file-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
font-weight: 600;
|
||||
margin-bottom: 12px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.file-detail {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
/*padding: 4px 0;*/
|
||||
color: var(--text-secondary);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
/* Modal */
|
||||
.modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.8);
|
||||
backdrop-filter: blur(8px);
|
||||
z-index: 1000;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
animation: fadeIn 0.2s ease-out;
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background: var(--bg-surface);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-xl);
|
||||
max-width: 90%;
|
||||
max-height: 85%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
box-shadow: var(--shadow-xl);
|
||||
animation: slideUp 0.3s ease-out;
|
||||
}
|
||||
|
||||
@keyframes slideUp {
|
||||
from {
|
||||
transform: translateY(20px);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: translateY(0);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
padding: 16px 20px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.modal-header h3 {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.modal-close {
|
||||
background: transparent;
|
||||
color: var(--text-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 50%;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.modal-close svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
transition: inherit;
|
||||
}
|
||||
|
||||
.modal-close:hover {
|
||||
background: rgba(239, 68, 68, 0.1);
|
||||
border-color: var(--danger);
|
||||
color: var(--danger);
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
.modal-body {
|
||||
padding: 16px;
|
||||
overflow: auto;
|
||||
font-family: "SF Mono", Monaco, monospace;
|
||||
font-size: 12px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.modal-body pre {
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
margin: 0;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
/* 按钮 */
|
||||
button {
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.row-detail-btn {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 6px 12px;
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.row-detail-btn:hover {
|
||||
background: var(--primary-dark);
|
||||
box-shadow: var(--shadow-md);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
/* 空状态和加载 */
|
||||
.loading,
|
||||
.empty,
|
||||
.error {
|
||||
text-align: center;
|
||||
padding: 60px 30px;
|
||||
}
|
||||
|
||||
.empty h2 {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 10px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.empty p {
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.error {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
/* Manifest stats */
|
||||
.manifest-stats {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
background: var(--bg-surface);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 14px;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.stat-card:hover {
|
||||
border-color: var(--border-hover);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: var(--shadow-md);
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: var(--text-secondary);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 28px;
|
||||
font-weight: 700;
|
||||
background: linear-gradient(135deg, var(--primary-light), var(--primary));
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
}
|
||||
|
||||
/* 响应式设计 */
|
||||
@media (max-width: 768px) {
|
||||
.container {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
width: 100%;
|
||||
border-right: none;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
max-height: 40vh;
|
||||
}
|
||||
|
||||
.main {
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.schema-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.manifest-stats {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
/* 列选择器 */
|
||||
.column-selector {
|
||||
margin-bottom: 16px;
|
||||
padding: 14px;
|
||||
background: var(--bg-surface);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-lg);
|
||||
}
|
||||
|
||||
.columns-container {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.column-checkbox {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 8px 14px;
|
||||
background: var(--bg-elevated);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-md);
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.column-checkbox:hover {
|
||||
border-color: var(--border-hover);
|
||||
}
|
||||
|
||||
.column-checkbox.selected {
|
||||
background: var(--primary-bg);
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.column-checkbox input {
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
/* 工具按钮 */
|
||||
.control-buttons {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.control-buttons button {
|
||||
padding: 6px 14px;
|
||||
background: var(--bg-surface);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius-md);
|
||||
color: var(--text-primary);
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.control-buttons button:hover {
|
||||
background: var(--bg-hover);
|
||||
border-color: var(--border-hover);
|
||||
}
|
||||
|
||||
/* 单元格大小指示器 */
|
||||
.cell-size {
|
||||
font-size: 11px;
|
||||
color: var(--text-tertiary);
|
||||
font-style: italic;
|
||||
margin-left: 6px;
|
||||
}
|
||||
|
||||
/* 截断指示器 */
|
||||
.truncated-icon {
|
||||
color: var(--warning);
|
||||
margin-left: 6px;
|
||||
}
|
||||
69
webui/static/index.html
Normal file
69
webui/static/index.html
Normal file
@@ -0,0 +1,69 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>SRDB Web UI</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||
<link
|
||||
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
<link rel="stylesheet" href="/static/css/styles.css" />
|
||||
<script src="https://npm.onmicrosoft.cn/htmx.org@2.0.7/dist/htmx.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<!-- 左侧表列表 -->
|
||||
<div class="sidebar" id="sidebar">
|
||||
<h1>Tables</h1>
|
||||
<div
|
||||
id="table-list"
|
||||
hx-get="/api/tables-html"
|
||||
hx-trigger="load"
|
||||
>
|
||||
Loading tables...
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 右侧主内容区 -->
|
||||
<div class="main" id="main-content">
|
||||
<div class="empty">
|
||||
<h2>Select a table to view data</h2>
|
||||
<p>Choose a table from the sidebar to get started</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Modal -->
|
||||
<div id="modal" class="modal" style="display: none">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h3 id="modal-title">Content</h3>
|
||||
<button class="modal-close" onclick="closeModal()">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<line x1="18" y1="6" x2="6" y2="18"></line>
|
||||
<line x1="6" y1="6" x2="18" y2="18"></line>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<pre id="modal-body-content"></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/static/js/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
199
webui/static/js/app.js
Normal file
199
webui/static/js/app.js
Normal file
@@ -0,0 +1,199 @@
|
||||
// SRDB WebUI - htmx 版本
|
||||
|
||||
// 全局状态
|
||||
window.srdbState = {
|
||||
selectedTable: null,
|
||||
currentPage: 1,
|
||||
pageSize: 20,
|
||||
selectedColumns: [],
|
||||
expandedTables: new Set(),
|
||||
expandedLevels: new Set([0, 1]),
|
||||
};
|
||||
|
||||
// 选择表格
|
||||
function selectTable(tableName) {
|
||||
window.srdbState.selectedTable = tableName;
|
||||
window.srdbState.currentPage = 1;
|
||||
|
||||
// 高亮选中的表
|
||||
document.querySelectorAll(".table-item").forEach((el) => {
|
||||
el.classList.toggle("selected", el.dataset.table === tableName);
|
||||
});
|
||||
|
||||
// 加载表数据
|
||||
loadTableData(tableName);
|
||||
}
|
||||
|
||||
// 加载表数据
|
||||
function loadTableData(tableName) {
|
||||
const mainContent = document.getElementById("main-content");
|
||||
mainContent.innerHTML = '<div class="loading">Loading...</div>';
|
||||
|
||||
fetch(
|
||||
`/api/tables-view/${tableName}?page=${window.srdbState.currentPage}&pageSize=${window.srdbState.pageSize}`,
|
||||
)
|
||||
.then((res) => res.text())
|
||||
.then((html) => {
|
||||
mainContent.innerHTML = html;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Failed to load table data:", err);
|
||||
mainContent.innerHTML =
|
||||
'<div class="error">Failed to load table data</div>';
|
||||
});
|
||||
}
|
||||
|
||||
// 切换视图 (Data / Manifest)
|
||||
function switchView(tableName, mode) {
|
||||
const mainContent = document.getElementById("main-content");
|
||||
mainContent.innerHTML = '<div class="loading">Loading...</div>';
|
||||
|
||||
const endpoint =
|
||||
mode === "manifest"
|
||||
? `/api/tables-view/${tableName}/manifest`
|
||||
: `/api/tables-view/${tableName}?page=${window.srdbState.currentPage}&pageSize=${window.srdbState.pageSize}`;
|
||||
|
||||
fetch(endpoint)
|
||||
.then((res) => res.text())
|
||||
.then((html) => {
|
||||
mainContent.innerHTML = html;
|
||||
});
|
||||
}
|
||||
|
||||
// 分页
|
||||
function changePage(delta) {
|
||||
window.srdbState.currentPage += delta;
|
||||
if (window.srdbState.selectedTable) {
|
||||
loadTableData(window.srdbState.selectedTable);
|
||||
}
|
||||
}
|
||||
|
||||
function jumpToPage(page) {
|
||||
window.srdbState.currentPage = parseInt(page);
|
||||
if (window.srdbState.selectedTable) {
|
||||
loadTableData(window.srdbState.selectedTable);
|
||||
}
|
||||
}
|
||||
|
||||
function changePageSize(newSize) {
|
||||
window.srdbState.pageSize = parseInt(newSize);
|
||||
window.srdbState.currentPage = 1;
|
||||
if (window.srdbState.selectedTable) {
|
||||
loadTableData(window.srdbState.selectedTable);
|
||||
}
|
||||
}
|
||||
|
||||
// Modal 相关
|
||||
function showModal(title, content) {
|
||||
document.getElementById("modal-title").textContent = title;
|
||||
document.getElementById("modal-body-content").textContent = content;
|
||||
document.getElementById("modal").style.display = "flex";
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
document.getElementById("modal").style.display = "none";
|
||||
}
|
||||
|
||||
function showCellContent(content) {
|
||||
showModal("Cell Content", content);
|
||||
}
|
||||
|
||||
function showRowDetail(tableName, seq) {
|
||||
fetch(`/api/tables/${tableName}/data/${seq}`)
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
const formatted = JSON.stringify(data, null, 2);
|
||||
showModal(`Row Detail (Seq: ${seq})`, formatted);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Failed to load row detail:", err);
|
||||
alert("Failed to load row detail");
|
||||
});
|
||||
}
|
||||
|
||||
// 折叠展开
|
||||
function toggleExpand(tableName) {
|
||||
const item = document.querySelector(`[data-table="${tableName}"]`);
|
||||
const fieldsDiv = item.querySelector(".schema-fields");
|
||||
const icon = item.querySelector(".expand-icon");
|
||||
|
||||
if (window.srdbState.expandedTables.has(tableName)) {
|
||||
window.srdbState.expandedTables.delete(tableName);
|
||||
fieldsDiv.style.display = "none";
|
||||
icon.classList.remove("expanded");
|
||||
} else {
|
||||
window.srdbState.expandedTables.add(tableName);
|
||||
fieldsDiv.style.display = "block";
|
||||
icon.classList.add("expanded");
|
||||
}
|
||||
}
|
||||
|
||||
function toggleLevel(level) {
|
||||
const levelCard = document.querySelector(`[data-level="${level}"]`);
|
||||
const fileList = levelCard.querySelector(".file-list");
|
||||
const icon = levelCard.querySelector(".expand-icon");
|
||||
|
||||
if (window.srdbState.expandedLevels.has(level)) {
|
||||
window.srdbState.expandedLevels.delete(level);
|
||||
fileList.style.display = "none";
|
||||
icon.classList.remove("expanded");
|
||||
} else {
|
||||
window.srdbState.expandedLevels.add(level);
|
||||
fileList.style.display = "grid";
|
||||
icon.classList.add("expanded");
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化工具
|
||||
function formatBytes(bytes) {
|
||||
if (bytes === 0) return "0 B";
|
||||
const k = 1024;
|
||||
const sizes = ["B", "KB", "MB", "GB"];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return (bytes / Math.pow(k, i)).toFixed(2) + " " + sizes[i];
|
||||
}
|
||||
|
||||
function formatCount(count) {
|
||||
if (count >= 1000000) return (count / 1000000).toFixed(1) + "M";
|
||||
if (count >= 1000) return (count / 1000).toFixed(1) + "K";
|
||||
return count.toString();
|
||||
}
|
||||
|
||||
// 点击 modal 外部关闭
|
||||
document.addEventListener("click", (e) => {
|
||||
const modal = document.getElementById("modal");
|
||||
if (e.target === modal) {
|
||||
closeModal();
|
||||
}
|
||||
});
|
||||
|
||||
// ESC 键关闭 modal
|
||||
document.addEventListener("keydown", (e) => {
|
||||
if (e.key === "Escape") {
|
||||
closeModal();
|
||||
}
|
||||
});
|
||||
|
||||
// 切换列显示
|
||||
function toggleColumn(columnName) {
|
||||
// 切换 schema-field-card 的选中状态
|
||||
const card = document.querySelector(
|
||||
`.schema-field-card[data-column="${columnName}"]`,
|
||||
);
|
||||
if (!card) return;
|
||||
|
||||
card.classList.toggle("selected");
|
||||
const isSelected = card.classList.contains("selected");
|
||||
|
||||
// 切换表格列的显示/隐藏
|
||||
const headers = document.querySelectorAll(`th[data-column="${columnName}"]`);
|
||||
const cells = document.querySelectorAll(`td[data-column="${columnName}"]`);
|
||||
|
||||
headers.forEach((header) => {
|
||||
header.style.display = isSelected ? "" : "none";
|
||||
});
|
||||
|
||||
cells.forEach((cell) => {
|
||||
cell.style.display = isSelected ? "" : "none";
|
||||
});
|
||||
}
|
||||
730
webui/webui.go
Normal file
730
webui/webui.go
Normal file
@@ -0,0 +1,730 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.tczkiot.com/srdb"
|
||||
"code.tczkiot.com/srdb/sst"
|
||||
)
|
||||
|
||||
//go:embed static
|
||||
var staticFS embed.FS
|
||||
|
||||
// WebUI Web 界面处理器
|
||||
type WebUI struct {
|
||||
db *srdb.Database
|
||||
handler http.Handler
|
||||
}
|
||||
|
||||
// NewWebUI 创建 WebUI 实例
|
||||
func NewWebUI(db *srdb.Database) *WebUI {
|
||||
ui := &WebUI{db: db}
|
||||
ui.handler = ui.setupHandler()
|
||||
return ui
|
||||
}
|
||||
|
||||
// setupHandler 设置 HTTP Handler
|
||||
func (ui *WebUI) setupHandler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// API endpoints - JSON
|
||||
mux.HandleFunc("/api/tables", ui.handleListTables)
|
||||
mux.HandleFunc("/api/tables/", ui.handleTableAPI)
|
||||
|
||||
// API endpoints - HTML (for htmx)
|
||||
mux.HandleFunc("/api/tables-html", ui.handleTablesHTML)
|
||||
mux.HandleFunc("/api/tables-view/", ui.handleTableViewHTML)
|
||||
|
||||
// Debug endpoint - list embedded files
|
||||
mux.HandleFunc("/debug/files", ui.handleDebugFiles)
|
||||
|
||||
// 静态文件服务
|
||||
staticFiles, _ := fs.Sub(staticFS, "static")
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFiles))))
|
||||
|
||||
// 首页
|
||||
mux.HandleFunc("/", ui.handleIndex)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
// Handler 返回 HTTP Handler
|
||||
func (ui *WebUI) Handler() http.Handler {
|
||||
return ui.handler
|
||||
}
|
||||
|
||||
// ServeHTTP 实现 http.Handler 接口
|
||||
func (ui *WebUI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ui.handler.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// handleListTables 处理获取表列表请求
|
||||
func (ui *WebUI) handleListTables(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
type FieldInfo struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Indexed bool `json:"indexed"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
type TableListItem struct {
|
||||
Name string `json:"name"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Fields []FieldInfo `json:"fields"`
|
||||
}
|
||||
|
||||
allTables := ui.db.GetAllTablesInfo()
|
||||
tables := make([]TableListItem, 0, len(allTables))
|
||||
for name, table := range allTables {
|
||||
schema := table.GetSchema()
|
||||
fields := make([]FieldInfo, 0, len(schema.Fields))
|
||||
for _, field := range schema.Fields {
|
||||
fields = append(fields, FieldInfo{
|
||||
Name: field.Name,
|
||||
Type: field.Type.String(),
|
||||
Indexed: field.Indexed,
|
||||
Comment: field.Comment,
|
||||
})
|
||||
}
|
||||
|
||||
tables = append(tables, TableListItem{
|
||||
Name: name,
|
||||
CreatedAt: table.GetCreatedAt(),
|
||||
Fields: fields,
|
||||
})
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(tables)
|
||||
}
|
||||
|
||||
// handleTableAPI 处理表相关的 API 请求
|
||||
func (ui *WebUI) handleTableAPI(w http.ResponseWriter, r *http.Request) {
|
||||
// 解析路径: /api/tables/{name}/schema 或 /api/tables/{name}/data
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/tables/")
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
if len(parts) < 2 {
|
||||
http.Error(w, "Invalid path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tableName := parts[0]
|
||||
action := parts[1]
|
||||
|
||||
switch action {
|
||||
case "schema":
|
||||
ui.handleTableSchema(w, r, tableName)
|
||||
case "data":
|
||||
// 检查是否是单条数据查询: /api/tables/{name}/data/{seq}
|
||||
if len(parts) >= 3 {
|
||||
ui.handleTableDataBySeq(w, r, tableName, parts[2])
|
||||
} else {
|
||||
ui.handleTableData(w, r, tableName)
|
||||
}
|
||||
case "manifest":
|
||||
ui.handleTableManifest(w, r, tableName)
|
||||
default:
|
||||
http.Error(w, "Unknown action", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
// handleTableSchema 处理获取表 schema 请求
|
||||
func (ui *WebUI) handleTableSchema(w http.ResponseWriter, r *http.Request, tableName string) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
table, err := ui.db.GetTable(tableName)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
schema := table.GetSchema()
|
||||
|
||||
type FieldInfo struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Indexed bool `json:"indexed"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
fields := make([]FieldInfo, 0, len(schema.Fields))
|
||||
for _, field := range schema.Fields {
|
||||
fields = append(fields, FieldInfo{
|
||||
Name: field.Name,
|
||||
Type: field.Type.String(),
|
||||
Indexed: field.Indexed,
|
||||
Comment: field.Comment,
|
||||
})
|
||||
}
|
||||
|
||||
response := map[string]any{
|
||||
"name": schema.Name,
|
||||
"fields": fields,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// handleTableManifest 处理获取表 manifest 信息请求
|
||||
func (ui *WebUI) handleTableManifest(w http.ResponseWriter, r *http.Request, tableName string) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
table, err := ui.db.GetTable(tableName)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
engine := table.GetEngine()
|
||||
versionSet := engine.GetVersionSet()
|
||||
version := versionSet.GetCurrent()
|
||||
|
||||
// 构建每层的信息
|
||||
type FileInfo struct {
|
||||
FileNumber int64 `json:"file_number"`
|
||||
Level int `json:"level"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
MinKey int64 `json:"min_key"`
|
||||
MaxKey int64 `json:"max_key"`
|
||||
RowCount int64 `json:"row_count"`
|
||||
}
|
||||
|
||||
type LevelInfo struct {
|
||||
Level int `json:"level"`
|
||||
FileCount int `json:"file_count"`
|
||||
TotalSize int64 `json:"total_size"`
|
||||
Score float64 `json:"score"`
|
||||
Files []FileInfo `json:"files"`
|
||||
}
|
||||
|
||||
// 获取 Compaction Manager 和 Picker
|
||||
compactionMgr := engine.GetCompactionManager()
|
||||
picker := compactionMgr.GetPicker()
|
||||
|
||||
levels := make([]LevelInfo, 0)
|
||||
for level := 0; level < 7; level++ {
|
||||
files := version.GetLevel(level)
|
||||
if len(files) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
totalSize := int64(0)
|
||||
fileInfos := make([]FileInfo, 0, len(files))
|
||||
for _, f := range files {
|
||||
totalSize += f.FileSize
|
||||
fileInfos = append(fileInfos, FileInfo{
|
||||
FileNumber: f.FileNumber,
|
||||
Level: f.Level,
|
||||
FileSize: f.FileSize,
|
||||
MinKey: f.MinKey,
|
||||
MaxKey: f.MaxKey,
|
||||
RowCount: f.RowCount,
|
||||
})
|
||||
}
|
||||
|
||||
score := picker.GetLevelScore(version, level)
|
||||
|
||||
levels = append(levels, LevelInfo{
|
||||
Level: level,
|
||||
FileCount: len(files),
|
||||
TotalSize: totalSize,
|
||||
Score: score,
|
||||
Files: fileInfos,
|
||||
})
|
||||
}
|
||||
|
||||
// 获取 Compaction 统计
|
||||
stats := compactionMgr.GetStats()
|
||||
|
||||
response := map[string]any{
|
||||
"levels": levels,
|
||||
"next_file_number": versionSet.GetNextFileNumber(),
|
||||
"last_sequence": versionSet.GetLastSequence(),
|
||||
"compaction_stats": stats,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// handleTableDataBySeq 处理获取单条数据请求
|
||||
func (ui *WebUI) handleTableDataBySeq(w http.ResponseWriter, r *http.Request, tableName string, seqStr string) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
table, err := ui.db.GetTable(tableName)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析 seq
|
||||
seq, err := strconv.ParseInt(seqStr, 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid seq parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
row, err := table.Get(seq)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Row not found: %v", err), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// 构造响应(不进行剪裁,返回完整数据)
|
||||
rowData := make(map[string]interface{})
|
||||
rowData["_seq"] = row.Seq
|
||||
rowData["_time"] = row.Time
|
||||
for k, v := range row.Data {
|
||||
rowData[k] = v
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(rowData)
|
||||
}
|
||||
|
||||
// handleTableData 处理获取表数据请求(分页)
|
||||
func (ui *WebUI) handleTableData(w http.ResponseWriter, r *http.Request, tableName string) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
table, err := ui.db.GetTable(tableName)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析分页参数
|
||||
pageStr := r.URL.Query().Get("page")
|
||||
pageSizeStr := r.URL.Query().Get("pageSize")
|
||||
selectParam := r.URL.Query().Get("select") // 要选择的字段,逗号分隔
|
||||
|
||||
page := 1
|
||||
pageSize := 20
|
||||
|
||||
if pageStr != "" {
|
||||
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
|
||||
page = p
|
||||
}
|
||||
}
|
||||
|
||||
if pageSizeStr != "" {
|
||||
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 && ps <= 1000 {
|
||||
pageSize = ps
|
||||
}
|
||||
}
|
||||
|
||||
// 解析要选择的字段
|
||||
var selectedFields []string
|
||||
if selectParam != "" {
|
||||
selectedFields = strings.Split(selectParam, ",")
|
||||
}
|
||||
|
||||
// 获取 schema 用于字段类型判断
|
||||
tableSchema := table.GetSchema()
|
||||
|
||||
// 使用 Query API 获取所有数据(高效)
|
||||
queryRows, err := table.Query().Rows()
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to query table: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer queryRows.Close()
|
||||
|
||||
// 收集所有 rows 到内存中用于分页(对于大数据集,后续可以优化为流式处理)
|
||||
allRows := make([]*sst.Row, 0)
|
||||
for queryRows.Next() {
|
||||
row := queryRows.Row()
|
||||
// Row 是 query.Row 类型,需要获取其内部的 sst.Row
|
||||
// 直接构造 sst.Row
|
||||
sstRow := &sst.Row{
|
||||
Seq: row.Data()["_seq"].(int64),
|
||||
Time: row.Data()["_time"].(int64),
|
||||
Data: make(map[string]any),
|
||||
}
|
||||
// 复制其他字段
|
||||
for k, v := range row.Data() {
|
||||
if k != "_seq" && k != "_time" {
|
||||
sstRow.Data[k] = v
|
||||
}
|
||||
}
|
||||
allRows = append(allRows, sstRow)
|
||||
}
|
||||
|
||||
// 计算分页
|
||||
totalRows := int64(len(allRows))
|
||||
offset := (page - 1) * pageSize
|
||||
end := offset + pageSize
|
||||
if end > int(totalRows) {
|
||||
end = int(totalRows)
|
||||
}
|
||||
|
||||
// 获取当前页数据
|
||||
rows := make([]*sst.Row, 0, pageSize)
|
||||
if offset < int(totalRows) {
|
||||
rows = allRows[offset:end]
|
||||
}
|
||||
|
||||
// 构造响应,对 string 字段进行剪裁
|
||||
const maxStringLength = 100 // 最大字符串长度(按字符计数,非字节)
|
||||
data := make([]map[string]any, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
rowData := make(map[string]any)
|
||||
|
||||
// 如果指定了字段,只返回选定的字段
|
||||
if len(selectedFields) > 0 {
|
||||
for _, field := range selectedFields {
|
||||
field = strings.TrimSpace(field)
|
||||
if field == "_seq" {
|
||||
rowData["_seq"] = row.Seq
|
||||
} else if field == "_time" {
|
||||
rowData["_time"] = row.Time
|
||||
} else if v, ok := row.Data[field]; ok {
|
||||
// 检查字段类型
|
||||
fieldDef, err := tableSchema.GetField(field)
|
||||
if err == nil && fieldDef.Type == srdb.FieldTypeString {
|
||||
// 对字符串字段进行剪裁
|
||||
if str, ok := v.(string); ok {
|
||||
runes := []rune(str)
|
||||
if len(runes) > maxStringLength {
|
||||
rowData[field] = string(runes[:maxStringLength]) + "..."
|
||||
rowData[field+"_truncated"] = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
rowData[field] = v
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 返回所有字段
|
||||
rowData["_seq"] = row.Seq
|
||||
rowData["_time"] = row.Time
|
||||
for k, v := range row.Data {
|
||||
// 检查字段类型
|
||||
field, err := tableSchema.GetField(k)
|
||||
if err == nil && field.Type == srdb.FieldTypeString {
|
||||
// 对字符串字段进行剪裁(按 rune 截取,避免 CJK 等多字节字符乱码)
|
||||
if str, ok := v.(string); ok {
|
||||
runes := []rune(str)
|
||||
if len(runes) > maxStringLength {
|
||||
rowData[k] = string(runes[:maxStringLength]) + "..."
|
||||
rowData[k+"_truncated"] = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
rowData[k] = v
|
||||
}
|
||||
}
|
||||
data = append(data, rowData)
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"data": data,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
"totalRows": totalRows,
|
||||
"totalPages": (totalRows + int64(pageSize) - 1) / int64(pageSize),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// handleDebugFiles 列出所有嵌入的文件(调试用)
|
||||
func (ui *WebUI) handleDebugFiles(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
fmt.Fprintln(w, "Embedded files in staticFS:")
|
||||
fs.WalkDir(staticFS, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "ERROR walking %s: %v\n", path, err)
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
fmt.Fprintf(w, "[DIR] %s/\n", path)
|
||||
} else {
|
||||
info, _ := d.Info()
|
||||
fmt.Fprintf(w, "[FILE] %s (%d bytes)\n", path, info.Size())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// handleIndex 处理首页请求
|
||||
func (ui *WebUI) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// 读取 index.html
|
||||
content, err := staticFS.ReadFile("static/index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to load page", http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Write(content)
|
||||
}
|
||||
|
||||
// handleTablesHTML 处理获取表列表 HTML 请求(for htmx)
|
||||
func (ui *WebUI) handleTablesHTML(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
allTables := ui.db.GetAllTablesInfo()
|
||||
tables := make([]TableListItem, 0, len(allTables))
|
||||
for name, table := range allTables {
|
||||
schema := table.GetSchema()
|
||||
fields := make([]FieldInfo, 0, len(schema.Fields))
|
||||
for _, field := range schema.Fields {
|
||||
fields = append(fields, FieldInfo{
|
||||
Name: field.Name,
|
||||
Type: field.Type.String(),
|
||||
Indexed: field.Indexed,
|
||||
Comment: field.Comment,
|
||||
})
|
||||
}
|
||||
|
||||
tables = append(tables, TableListItem{
|
||||
Name: name,
|
||||
CreatedAt: table.GetCreatedAt(),
|
||||
Fields: fields,
|
||||
})
|
||||
}
|
||||
|
||||
html := renderTablesHTML(tables)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
|
||||
// handleTableViewHTML 处理获取表视图 HTML 请求(for htmx)
|
||||
func (ui *WebUI) handleTableViewHTML(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析路径: /api/tables-view/{name} 或 /api/tables-view/{name}/manifest
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/tables-view/")
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
if len(parts) < 1 || parts[0] == "" {
|
||||
http.Error(w, "Invalid path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tableName := parts[0]
|
||||
isManifest := len(parts) >= 2 && parts[1] == "manifest"
|
||||
|
||||
table, err := ui.db.GetTable(tableName)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if isManifest {
|
||||
// 返回 Manifest 视图 HTML
|
||||
ui.renderManifestHTML(w, r, tableName, table)
|
||||
} else {
|
||||
// 返回 Data 视图 HTML
|
||||
ui.renderDataHTML(w, r, tableName, table)
|
||||
}
|
||||
}
|
||||
|
||||
// renderDataHTML 渲染数据视图 HTML
|
||||
func (ui *WebUI) renderDataHTML(w http.ResponseWriter, r *http.Request, tableName string, table *srdb.Table) {
|
||||
// 解析分页参数
|
||||
pageStr := r.URL.Query().Get("page")
|
||||
pageSizeStr := r.URL.Query().Get("pageSize")
|
||||
|
||||
page := 1
|
||||
pageSize := 20
|
||||
|
||||
if pageStr != "" {
|
||||
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
|
||||
page = p
|
||||
}
|
||||
}
|
||||
|
||||
if pageSizeStr != "" {
|
||||
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 && ps <= 1000 {
|
||||
pageSize = ps
|
||||
}
|
||||
}
|
||||
|
||||
// 获取 schema
|
||||
tableSchema := table.GetSchema()
|
||||
schemaInfo := SchemaInfo{
|
||||
Name: tableSchema.Name,
|
||||
Fields: make([]FieldInfo, 0, len(tableSchema.Fields)),
|
||||
}
|
||||
for _, field := range tableSchema.Fields {
|
||||
schemaInfo.Fields = append(schemaInfo.Fields, FieldInfo{
|
||||
Name: field.Name,
|
||||
Type: field.Type.String(),
|
||||
Indexed: field.Indexed,
|
||||
Comment: field.Comment,
|
||||
})
|
||||
}
|
||||
|
||||
// 使用 Query API 获取所有数据
|
||||
queryRows, err := table.Query().Rows()
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to query table: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer queryRows.Close()
|
||||
|
||||
// 收集所有 rows
|
||||
allRows := make([]*sst.Row, 0)
|
||||
for queryRows.Next() {
|
||||
row := queryRows.Row()
|
||||
sstRow := &sst.Row{
|
||||
Seq: row.Data()["_seq"].(int64),
|
||||
Time: row.Data()["_time"].(int64),
|
||||
Data: make(map[string]any),
|
||||
}
|
||||
for k, v := range row.Data() {
|
||||
if k != "_seq" && k != "_time" {
|
||||
sstRow.Data[k] = v
|
||||
}
|
||||
}
|
||||
allRows = append(allRows, sstRow)
|
||||
}
|
||||
|
||||
// 计算分页
|
||||
totalRows := int64(len(allRows))
|
||||
offset := (page - 1) * pageSize
|
||||
end := offset + pageSize
|
||||
if end > int(totalRows) {
|
||||
end = int(totalRows)
|
||||
}
|
||||
|
||||
// 获取当前页数据
|
||||
rows := make([]*sst.Row, 0, pageSize)
|
||||
if offset < int(totalRows) {
|
||||
rows = allRows[offset:end]
|
||||
}
|
||||
|
||||
// 构造 TableDataResponse
|
||||
const maxStringLength = 100
|
||||
data := make([]map[string]any, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
rowData := make(map[string]any)
|
||||
rowData["_seq"] = row.Seq
|
||||
rowData["_time"] = row.Time
|
||||
for k, v := range row.Data {
|
||||
field, err := tableSchema.GetField(k)
|
||||
if err == nil && field.Type == srdb.FieldTypeString {
|
||||
if str, ok := v.(string); ok {
|
||||
runes := []rune(str)
|
||||
if len(runes) > maxStringLength {
|
||||
rowData[k] = string(runes[:maxStringLength]) + "..."
|
||||
rowData[k+"_truncated"] = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
rowData[k] = v
|
||||
}
|
||||
data = append(data, rowData)
|
||||
}
|
||||
|
||||
tableData := TableDataResponse{
|
||||
Data: data,
|
||||
Page: int64(page),
|
||||
PageSize: int64(pageSize),
|
||||
TotalRows: totalRows,
|
||||
TotalPages: (totalRows + int64(pageSize) - 1) / int64(pageSize),
|
||||
}
|
||||
|
||||
html := renderDataViewHTML(tableName, schemaInfo, tableData)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
|
||||
// renderManifestHTML 渲染 Manifest 视图 HTML
|
||||
func (ui *WebUI) renderManifestHTML(w http.ResponseWriter, r *http.Request, tableName string, table *srdb.Table) {
|
||||
engine := table.GetEngine()
|
||||
versionSet := engine.GetVersionSet()
|
||||
version := versionSet.GetCurrent()
|
||||
|
||||
// 获取 Compaction Manager 和 Picker
|
||||
compactionMgr := engine.GetCompactionManager()
|
||||
picker := compactionMgr.GetPicker()
|
||||
|
||||
levels := make([]LevelInfo, 0)
|
||||
for level := 0; level < 7; level++ {
|
||||
files := version.GetLevel(level)
|
||||
|
||||
totalSize := int64(0)
|
||||
fileInfos := make([]FileInfo, 0, len(files))
|
||||
for _, f := range files {
|
||||
totalSize += f.FileSize
|
||||
fileInfos = append(fileInfos, FileInfo{
|
||||
FileNumber: f.FileNumber,
|
||||
Level: f.Level,
|
||||
FileSize: f.FileSize,
|
||||
MinKey: f.MinKey,
|
||||
MaxKey: f.MaxKey,
|
||||
RowCount: f.RowCount,
|
||||
})
|
||||
}
|
||||
|
||||
score := 0.0
|
||||
if len(files) > 0 {
|
||||
score = picker.GetLevelScore(version, level)
|
||||
}
|
||||
|
||||
levels = append(levels, LevelInfo{
|
||||
Level: level,
|
||||
FileCount: len(files),
|
||||
TotalSize: totalSize,
|
||||
Score: score,
|
||||
Files: fileInfos,
|
||||
})
|
||||
}
|
||||
|
||||
stats := compactionMgr.GetStats()
|
||||
|
||||
manifest := ManifestResponse{
|
||||
Levels: levels,
|
||||
NextFileNumber: versionSet.GetNextFileNumber(),
|
||||
LastSequence: versionSet.GetLastSequence(),
|
||||
CompactionStats: stats,
|
||||
}
|
||||
|
||||
html := renderManifestViewHTML(tableName, manifest)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
Reference in New Issue
Block a user