Files
dobotai-platform/internal/store/postgres/operational_status.go
2026-08-13 18:33:52 +08:00

55 lines
2.1 KiB
Go

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
}