genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[BUGFIX] fix avg + improve status db connection

This commit is contained in:
Martin Geno 2017-04-04 07:37:39 +02:00
parent 66d6022d37
commit 95c694225d
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
2 changed files with 28 additions and 2 deletions

View File

@ -13,10 +13,15 @@ func status(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
var good []*models.Good var good []*models.Good
var count int64 var count int64
var avg int64 var avg float64
database.Read.Find(&good).Count(&count) //.Avg(&avg) 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{}{ lib.Write(w, map[string]interface{}{
"status": "running", "status": "running",
"database": map[string]interface{}{
"read": database.Read.DB().Ping() == nil,
"write": database.Write.DB().Ping() == nil,
},
"good": map[string]interface{}{ "good": map[string]interface{}{
"count": count, "count": count,
"avg": avg, "avg": avg,

View File

@ -4,6 +4,8 @@ import (
"net/http" "net/http"
"testing" "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" "github.com/genofire/hs_master-kss-monolith/test"
) )
@ -13,12 +15,31 @@ func TestStatus(t *testing.T) {
BindAPI(router) BindAPI(router)
session := test.NewSession(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) r, w := session.JSONRequest("GET", "/api/status", nil)
result := r.(map[string]interface{}) result := r.(map[string]interface{})
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal("running", result["status"]) 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{}) 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() test.Close()
} }