diff --git a/http/status.go b/http/status.go index 0d40d1c..5f0631a 100644 --- a/http/status.go +++ b/http/status.go @@ -13,10 +13,15 @@ func status(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) var good []*models.Good var count int64 - var avg int64 + var avg float64 database.Read.Find(&good).Count(&count) //.Avg(&avg) + database.Read.Raw("SELECT avg(g.gcount) as avg FROM (select count(*) as gcount FROM good g GROUP BY g.product_id) g").Row().Scan(&avg) lib.Write(w, map[string]interface{}{ "status": "running", + "database": map[string]interface{}{ + "read": database.Read.DB().Ping() == nil, + "write": database.Write.DB().Ping() == nil, + }, "good": map[string]interface{}{ "count": count, "avg": avg, diff --git a/http/status_test.go b/http/status_test.go index c7b6d6e..91517a7 100644 --- a/http/status_test.go +++ b/http/status_test.go @@ -4,6 +4,8 @@ import ( "net/http" "testing" + "github.com/genofire/hs_master-kss-monolith/lib/database" + "github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/test" ) @@ -13,12 +15,31 @@ func TestStatus(t *testing.T) { BindAPI(router) session := test.NewSession(router) + database.Write.Create(&models.Good{ + ProductID: 3, + Comment: "regal 1", + }) + database.Write.Create(&models.Good{ + ProductID: 3, + Comment: "regal 2", + }) + database.Write.Create(&models.Good{ + ProductID: 1, + Comment: "regal 10", + }) + r, w := session.JSONRequest("GET", "/api/status", nil) result := r.(map[string]interface{}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal("running", result["status"]) + + db := result["database"].(map[string]interface{}) + assertion.Equal(true, db["read"]) + assertion.Equal(true, db["write"]) + good := result["good"].(map[string]interface{}) - assertion.Equal(float64(0), good["count"]) + assertion.Equal(float64(3), good["count"]) + assertion.Equal(float64(1.5), good["avg"]) test.Close() }