From 5f14a5b943f77aab97085b87d68175988be8e030 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Thu, 23 Apr 2020 15:41:57 +0800 Subject: [PATCH] db: Allow empty service name in query --- db/ping.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/db/ping.go b/db/ping.go index ffc9f79..39cfba0 100644 --- a/db/ping.go +++ b/db/ping.go @@ -3,18 +3,15 @@ package db import ( "time" - "github.com/jinzhu/gorm" - "status/check" ) //PingEntry represents an entry of Ping that is persisted in the DB. type PingEntry struct { - gorm.Model - ServiceName string - Time time.Time - Ping int64 - Status check.Result + ServiceName string `json:"service_name"` + Time time.Time `json:"query_time"` + Ping int64 `json:"ping"` + Status check.Result `json:"status"` } func (p PingEntry) AddToDB() { @@ -24,8 +21,12 @@ func (p PingEntry) AddToDB() { db.Create(&p) } -func GetFromDB(serviceName string, from, to time.Time) []*PingEntry { - entries := make([]*PingEntry, 0) - db.Where("service_name = ? AND time BETWEEN ? AND ?", serviceName, from, to).Find(&entries) +func GetFromDB(serviceName string, from, to time.Time) []PingEntry { + entries := make([]PingEntry, 0) + if serviceName == "" { + db.Where("time BETWEEN ? AND ?", from, to).Find(&entries) + } else { + db.Where("service_name = ? AND time BETWEEN ? AND ?", serviceName, from, to).Find(&entries) + } return entries }