feat: add operational component status dashboard

This commit is contained in:
姚章浩
2026-08-13 18:33:52 +08:00
parent 6f24743be2
commit 5d68b22636
9 changed files with 509 additions and 4 deletions
@@ -0,0 +1,54 @@
package postgres
import (
"context"
"time"
)
const operationalStatusSQL = `
SELECT
(SELECT COUNT(*) FROM question_table_exports WHERE tenant_key = $1 AND status IN ('pending','exporting')),
(SELECT COUNT(*) FROM question_table_exports WHERE tenant_key = $1 AND status = 'export_failed'),
(SELECT MAX(exported_at) FROM question_table_exports WHERE tenant_key = $1 AND status = 'exported'),
(SELECT COUNT(*) FROM support_cases WHERE tenant_key = $1 AND faq_export_status IN ('pending','exporting')),
(SELECT COUNT(*) FROM support_cases WHERE tenant_key = $1 AND faq_export_status = 'export_failed'),
(SELECT MAX(faq_exported_at) FROM support_cases WHERE tenant_key = $1 AND faq_export_status = 'exported'),
(SELECT COUNT(*) FROM support_cases WHERE tenant_key = $1 AND status IN ('pending','notifying') AND faq_export_status = 'exported'),
(SELECT COUNT(*) FROM support_cases WHERE tenant_key = $1 AND status = 'notification_failed'),
(SELECT MAX(notified_at) FROM support_cases WHERE tenant_key = $1 AND status = 'notified')`
type OperationalMetrics struct {
QuestionPending, QuestionFailed int
FAQPending, FAQFailed int
SupportPending, SupportFailed int
LastQuestionSuccess time.Time
LastFAQSuccess time.Time
LastSupportSuccess time.Time
}
func (s *Store) Ping(ctx context.Context) error {
if s == nil || s.pool == nil {
return context.Canceled
}
return s.pool.Ping(ctx)
}
func (s *Store) ReadOperationalMetrics(ctx context.Context, tenant string) (OperationalMetrics, error) {
var value OperationalMetrics
var questionSuccess, faqSuccess, supportSuccess *time.Time
err := s.pool.QueryRow(ctx, operationalStatusSQL, tenant).Scan(
&value.QuestionPending, &value.QuestionFailed, &questionSuccess,
&value.FAQPending, &value.FAQFailed, &faqSuccess,
&value.SupportPending, &value.SupportFailed, &supportSuccess,
)
if questionSuccess != nil {
value.LastQuestionSuccess = *questionSuccess
}
if faqSuccess != nil {
value.LastFAQSuccess = *faqSuccess
}
if supportSuccess != nil {
value.LastSupportSuccess = *supportSuccess
}
return value, err
}
@@ -0,0 +1,29 @@
package postgres
import (
"strings"
"testing"
)
func TestOperationalStatusSQLIsTenantScopedAndReadOnly(t *testing.T) {
upper := strings.ToUpper(operationalStatusSQL)
for _, want := range []string{
"QUESTION_TABLE_EXPORTS",
"SUPPORT_CASES",
"TENANT_KEY = $1",
"FAQ_EXPORT_STATUS",
"NOTIFICATION_FAILED",
"MAX(EXPORTED_AT)",
"MAX(FAQ_EXPORTED_AT)",
"MAX(NOTIFIED_AT)",
} {
if !strings.Contains(upper, want) {
t.Errorf("status SQL missing %q", want)
}
}
for _, forbidden := range []string{"INSERT ", "UPDATE ", "DELETE ", "FOR UPDATE"} {
if strings.Contains(upper, forbidden) {
t.Errorf("status SQL must be read-only; found %q", forbidden)
}
}
}