feat(platform): summarize service status on dashboard
This commit is contained in:
@@ -19,6 +19,9 @@ func TestHandlerDashboardSummary(t *testing.T) {
|
||||
if !strings.Contains(rec.Body.String(), "onlineVehicles") {
|
||||
t.Fatalf("response missing onlineVehicles: %s", rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "serviceStatuses") || !strings.Contains(rec.Body.String(), "degraded") {
|
||||
t.Fatalf("response missing vehicle service status distribution: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicles(t *testing.T) {
|
||||
|
||||
@@ -44,6 +44,12 @@ func (m *MockStore) DashboardSummary(context.Context) (DashboardSummary, error)
|
||||
{Protocol: "JT808", Online: 73, Total: 318},
|
||||
{Protocol: "YUTONG_MQTT", Online: 1, Total: 8},
|
||||
},
|
||||
ServiceStatuses: []ServiceStatusStat{
|
||||
{Status: "healthy", Title: "服务正常", Count: 2},
|
||||
{Status: "degraded", Title: "部分来源离线", Count: 1},
|
||||
{Status: "offline", Title: "车辆离线", Count: 1},
|
||||
{Status: "identity_required", Title: "身份未绑定", Count: 0},
|
||||
},
|
||||
LinkHealth: []LinkHealth{
|
||||
{Name: "Redis realtime", Status: "ok", Detail: "在线状态 1 分钟 TTL 正常"},
|
||||
{Name: "TDengine raw_frames", Status: "ok", Detail: "最近 5 分钟有写入"},
|
||||
|
||||
@@ -8,13 +8,14 @@ type Page[T any] struct {
|
||||
}
|
||||
|
||||
type DashboardSummary struct {
|
||||
OnlineVehicles int `json:"onlineVehicles"`
|
||||
ActiveToday int `json:"activeToday"`
|
||||
FrameToday int `json:"frameToday"`
|
||||
IssueVehicles int `json:"issueVehicles"`
|
||||
KafkaLag *int `json:"kafkaLag"`
|
||||
Protocols []ProtocolStat `json:"protocols"`
|
||||
LinkHealth []LinkHealth `json:"linkHealth"`
|
||||
OnlineVehicles int `json:"onlineVehicles"`
|
||||
ActiveToday int `json:"activeToday"`
|
||||
FrameToday int `json:"frameToday"`
|
||||
IssueVehicles int `json:"issueVehicles"`
|
||||
KafkaLag *int `json:"kafkaLag"`
|
||||
Protocols []ProtocolStat `json:"protocols"`
|
||||
ServiceStatuses []ServiceStatusStat `json:"serviceStatuses"`
|
||||
LinkHealth []LinkHealth `json:"linkHealth"`
|
||||
}
|
||||
|
||||
type ProtocolStat struct {
|
||||
@@ -23,6 +24,12 @@ type ProtocolStat struct {
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
type ServiceStatusStat struct {
|
||||
Status string `json:"status"`
|
||||
Title string `json:"title"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type LinkHealth struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
|
||||
@@ -60,6 +60,11 @@ func (s *ProductionStore) DashboardSummary(ctx context.Context) (DashboardSummar
|
||||
return DashboardSummary{}, err
|
||||
}
|
||||
summary.Protocols = protocols
|
||||
serviceStatuses, err := s.serviceStatusStats(ctx)
|
||||
if err != nil {
|
||||
return DashboardSummary{}, err
|
||||
}
|
||||
summary.ServiceStatuses = serviceStatuses
|
||||
health, err := s.OpsHealth(ctx)
|
||||
if err != nil {
|
||||
return DashboardSummary{}, err
|
||||
@@ -69,6 +74,27 @@ func (s *ProductionStore) DashboardSummary(ctx context.Context) (DashboardSummar
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
func (s *ProductionStore) serviceStatusStats(ctx context.Context) ([]ServiceStatusStat, error) {
|
||||
definitions := []ServiceStatusStat{
|
||||
{Status: "healthy", Title: "服务正常"},
|
||||
{Status: "degraded", Title: "部分来源离线"},
|
||||
{Status: "offline", Title: "车辆离线"},
|
||||
{Status: "identity_required", Title: "身份未绑定"},
|
||||
}
|
||||
out := make([]ServiceStatusStat, 0, len(definitions))
|
||||
for _, definition := range definitions {
|
||||
query := url.Values{"serviceStatus": {definition.Status}, "limit": {"1"}}
|
||||
built := buildVehicleCoverageSQL(query)
|
||||
count := 0
|
||||
if err := s.db.QueryRowContext(ctx, built.CountText, built.CountArgs...).Scan(&count); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definition.Count = count
|
||||
out = append(out, definition)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *ProductionStore) frameToday(ctx context.Context, now time.Time) (int, error) {
|
||||
if s.tdengine == nil {
|
||||
return 0, nil
|
||||
|
||||
Reference in New Issue
Block a user