feat(ui): add message cache management page and dashboard enhancements
- Introduced MessageCachePage for browsing and managing cached webhook events. - Enhanced DashboardPage to display runtime stats and message cache information. - Added new API types for message cache events and system stats. - Integrated SwaggerPage for API documentation and live request testing.
This commit is contained in:
@@ -10,10 +10,123 @@ import (
|
||||
"git.warky.dev/wdevs/whatshooked/pkg/storage"
|
||||
)
|
||||
|
||||
type accountRuntimeStatus struct {
|
||||
AccountID string `json:"account_id"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Connected bool `json:"connected"`
|
||||
QRAvailable bool `json:"qr_available"`
|
||||
}
|
||||
|
||||
type accountConfigWithStatus struct {
|
||||
config.WhatsAppConfig
|
||||
Status string `json:"status"`
|
||||
Connected bool `json:"connected"`
|
||||
QRAvailable bool `json:"qr_available"`
|
||||
}
|
||||
|
||||
func (h *Handlers) getAccountStatusMapFromDB() map[string]accountRuntimeStatus {
|
||||
result := map[string]accountRuntimeStatus{}
|
||||
|
||||
db := storage.GetDB()
|
||||
if db == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
type statusRow struct {
|
||||
ID string `bun:"id"`
|
||||
AccountType string `bun:"account_type"`
|
||||
Status string `bun:"status"`
|
||||
}
|
||||
|
||||
rows := make([]statusRow, 0)
|
||||
err := db.NewSelect().
|
||||
Table("whatsapp_account").
|
||||
Column("id", "account_type", "status").
|
||||
Scan(context.Background(), &rows)
|
||||
if err != nil {
|
||||
logging.Warn("Failed to load account statuses from database", "error", err)
|
||||
return result
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
accountID := row.ID
|
||||
status := row.Status
|
||||
if status == "" {
|
||||
status = "disconnected"
|
||||
}
|
||||
result[accountID] = accountRuntimeStatus{
|
||||
AccountID: accountID,
|
||||
Type: row.AccountType,
|
||||
Status: status,
|
||||
Connected: status == "connected",
|
||||
QRAvailable: status == "pairing",
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Accounts returns the list of all configured WhatsApp accounts
|
||||
func (h *Handlers) Accounts(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
writeJSON(w, h.config.WhatsApp)
|
||||
|
||||
statusByAccountID := h.getAccountStatusMapFromDB()
|
||||
accounts := make([]accountConfigWithStatus, 0, len(h.config.WhatsApp))
|
||||
for _, account := range h.config.WhatsApp {
|
||||
status := accountRuntimeStatus{
|
||||
AccountID: account.ID,
|
||||
Type: account.Type,
|
||||
Status: "disconnected",
|
||||
}
|
||||
if account.Disabled {
|
||||
status.Status = "disconnected"
|
||||
status.Connected = false
|
||||
status.QRAvailable = false
|
||||
} else if fromDB, exists := statusByAccountID[account.ID]; exists {
|
||||
status = fromDB
|
||||
}
|
||||
|
||||
accounts = append(accounts, accountConfigWithStatus{
|
||||
WhatsAppConfig: account,
|
||||
Status: status.Status,
|
||||
Connected: status.Connected,
|
||||
QRAvailable: status.QRAvailable,
|
||||
})
|
||||
}
|
||||
|
||||
writeJSON(w, accounts)
|
||||
}
|
||||
|
||||
// AccountStatuses returns status values persisted in the database.
|
||||
// GET /api/accounts/status
|
||||
func (h *Handlers) AccountStatuses(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
statusByAccountID := h.getAccountStatusMapFromDB()
|
||||
statuses := make([]accountRuntimeStatus, 0, len(h.config.WhatsApp))
|
||||
for _, account := range h.config.WhatsApp {
|
||||
status := accountRuntimeStatus{
|
||||
AccountID: account.ID,
|
||||
Type: account.Type,
|
||||
Status: "disconnected",
|
||||
}
|
||||
if account.Disabled {
|
||||
status.Status = "disconnected"
|
||||
status.Connected = false
|
||||
status.QRAvailable = false
|
||||
} else if fromDB, exists := statusByAccountID[account.ID]; exists {
|
||||
status = fromDB
|
||||
}
|
||||
statuses = append(statuses, status)
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]interface{}{
|
||||
"statuses": statuses,
|
||||
})
|
||||
}
|
||||
|
||||
// AddAccount adds a new WhatsApp account to the system
|
||||
@@ -35,6 +148,13 @@ func (h *Handlers) AddAccount(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if db := storage.GetDB(); db != nil {
|
||||
repo := storage.NewWhatsAppAccountRepository(db)
|
||||
if err := repo.UpdateStatus(context.Background(), account.ID, "connecting"); err != nil {
|
||||
logging.Warn("Failed to set account status to connecting", "account_id", account.ID, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Update config
|
||||
h.config.WhatsApp = append(h.config.WhatsApp, account)
|
||||
if h.configPath != "" {
|
||||
@@ -70,6 +190,13 @@ func (h *Handlers) RemoveAccount(w http.ResponseWriter, r *http.Request) {
|
||||
// Continue with removal even if disconnect fails
|
||||
}
|
||||
|
||||
if db := storage.GetDB(); db != nil {
|
||||
repo := storage.NewWhatsAppAccountRepository(db)
|
||||
if err := repo.UpdateStatus(context.Background(), req.ID, "disconnected"); err != nil {
|
||||
logging.Warn("Failed to set account status to disconnected after removal", "account_id", req.ID, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove from config
|
||||
found := false
|
||||
newAccounts := make([]config.WhatsAppConfig, 0)
|
||||
@@ -137,6 +264,13 @@ func (h *Handlers) DisableAccount(w http.ResponseWriter, r *http.Request) {
|
||||
// Mark as disabled
|
||||
h.config.WhatsApp[i].Disabled = true
|
||||
logging.Info("Account disabled", "account_id", req.ID)
|
||||
|
||||
if db := storage.GetDB(); db != nil {
|
||||
repo := storage.NewWhatsAppAccountRepository(db)
|
||||
if err := repo.UpdateStatus(context.Background(), req.ID, "disconnected"); err != nil {
|
||||
logging.Warn("Failed to set account status to disconnected", "account_id", req.ID, "error", err)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -207,6 +341,13 @@ func (h *Handlers) EnableAccount(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if db := storage.GetDB(); db != nil {
|
||||
repo := storage.NewWhatsAppAccountRepository(db)
|
||||
if err := repo.UpdateStatus(context.Background(), req.ID, "connecting"); err != nil {
|
||||
logging.Warn("Failed to set account status to connecting", "account_id", req.ID, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
logging.Info("Account enabled and connected", "account_id", req.ID)
|
||||
|
||||
// Save config
|
||||
@@ -277,6 +418,15 @@ func (h *Handlers) UpdateAccount(w http.ResponseWriter, r *http.Request) {
|
||||
if err := accountRepo.UpdateConfig(context.Background(), updates.ID, updates.PhoneNumber, cfgJSON, !updates.Disabled); err != nil {
|
||||
logging.Warn("Failed to sync updated account config to database", "account_id", updates.ID, "error", err)
|
||||
}
|
||||
if updates.Disabled {
|
||||
if err := accountRepo.UpdateStatus(context.Background(), updates.ID, "disconnected"); err != nil {
|
||||
logging.Warn("Failed to set account status to disconnected after update", "account_id", updates.ID, "error", err)
|
||||
}
|
||||
} else if oldConfig.Disabled {
|
||||
if err := accountRepo.UpdateStatus(context.Background(), updates.ID, "connecting"); err != nil {
|
||||
logging.Warn("Failed to set account status to connecting after update", "account_id", updates.ID, "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the account was enabled and settings changed, reconnect it
|
||||
|
||||
@@ -25,17 +25,60 @@ func (h *Handlers) GetCachedEvents(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Optional event_type filter
|
||||
eventType := r.URL.Query().Get("event_type")
|
||||
limit := 0
|
||||
offset := 0
|
||||
limitProvided := false
|
||||
offsetProvided := false
|
||||
|
||||
var cachedEvents interface{}
|
||||
if eventType != "" {
|
||||
cachedEvents = cache.ListByEventType(events.EventType(eventType))
|
||||
} else {
|
||||
cachedEvents = cache.List()
|
||||
limitParam := r.URL.Query().Get("limit")
|
||||
if limitParam != "" {
|
||||
parsedLimit, err := strconv.Atoi(limitParam)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid limit parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if parsedLimit <= 0 {
|
||||
http.Error(w, "Limit must be greater than 0", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
limit = parsedLimit
|
||||
limitProvided = true
|
||||
}
|
||||
|
||||
offsetParam := r.URL.Query().Get("offset")
|
||||
if offsetParam != "" {
|
||||
parsedOffset, err := strconv.Atoi(offsetParam)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid offset parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if parsedOffset < 0 {
|
||||
http.Error(w, "Offset must be greater than or equal to 0", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
offset = parsedOffset
|
||||
offsetProvided = true
|
||||
}
|
||||
|
||||
// If offset is provided without limit, use a sensible page size.
|
||||
if offsetProvided && !limitProvided {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
cachedEvents, filteredCount := cache.ListPaged(events.EventType(eventType), limit, offset)
|
||||
if !limitProvided && !offsetProvided {
|
||||
// Backward-compatible response values when pagination is not requested.
|
||||
limit = len(cachedEvents)
|
||||
offset = 0
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]interface{}{
|
||||
"cached_events": cachedEvents,
|
||||
"count": cache.Count(),
|
||||
"filtered_count": filteredCount,
|
||||
"returned_count": len(cachedEvents),
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user