feat: 重做管理后台的导航、表单和登录页
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
@@ -49,6 +50,12 @@ FROM endpoints`).Scan(&total, &selfCount, &disabled, &online)
|
||||
return
|
||||
}
|
||||
|
||||
daily, err := messageDaily(ctx, h.db.Read, 14)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, "internal", "内部错误")
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteOK(w, map[string]any{
|
||||
"version": h.version,
|
||||
"endpoints_total": total,
|
||||
@@ -59,9 +66,45 @@ FROM endpoints`).Scan(&total, &selfCount, &disabled, &online)
|
||||
"messages_pending": pending,
|
||||
"messages_scheduled": scheduled,
|
||||
"uptime_ms": time.Since(h.startedAt).Milliseconds(),
|
||||
"messages_daily": daily,
|
||||
})
|
||||
}
|
||||
|
||||
func messageDaily(ctx context.Context, db *sql.DB, days int) ([]map[string]any, error) {
|
||||
if days < 1 {
|
||||
days = 14
|
||||
}
|
||||
now := time.Now()
|
||||
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -(days - 1))
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT date(created_at / 1000, 'unixepoch', 'localtime') AS d, COUNT(*)
|
||||
FROM messages
|
||||
WHERE created_at >= ?
|
||||
GROUP BY d`, start.UnixMilli())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
counts := map[string]int{}
|
||||
for rows.Next() {
|
||||
var day string
|
||||
var n int
|
||||
if err := rows.Scan(&day, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts[day] = n
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]map[string]any, 0, days)
|
||||
for i := 0; i < days; i++ {
|
||||
day := start.AddDate(0, 0, i).Format("2006-01-02")
|
||||
out = append(out, map[string]any{"date": day, "count": counts[day]})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (h *Handler) handleSettingsGet(w http.ResponseWriter, _ *http.Request) {
|
||||
cfg := h.cfg
|
||||
httpx.WriteOK(w, map[string]any{
|
||||
|
||||
Reference in New Issue
Block a user