mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 17:56:37 +08:00
[优化] 代码优化
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"openflare/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -26,6 +26,14 @@ type NodeMetricSnapshot struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (snapshot *NodeMetricSnapshot) GetID() uint {
|
||||
return snapshot.ID
|
||||
}
|
||||
|
||||
func (snapshot *NodeMetricSnapshot) GetTime() time.Time {
|
||||
return snapshot.CapturedAt
|
||||
}
|
||||
|
||||
func (snapshot *NodeMetricSnapshot) BeforeCreate(tx *gorm.DB) error {
|
||||
return assignObservabilityID(&snapshot.ID)
|
||||
}
|
||||
@@ -52,16 +60,7 @@ func ListNodeMetricSnapshots(nodeID string, since time.Time, limit int) (snapsho
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(rows, func(i int, j int) bool {
|
||||
if rows[i].CapturedAt.Equal(rows[j].CapturedAt) {
|
||||
return rows[i].ID > rows[j].ID
|
||||
}
|
||||
return rows[i].CapturedAt.After(rows[j].CapturedAt)
|
||||
})
|
||||
if limit > 0 && len(rows) > limit {
|
||||
rows = rows[:limit]
|
||||
}
|
||||
return rows, nil
|
||||
return utils.SortAndLimitRecords(rows, limit), nil
|
||||
}
|
||||
|
||||
func ListMetricSnapshotsSince(since time.Time) (snapshots []*NodeMetricSnapshot, err error) {
|
||||
@@ -79,13 +78,7 @@ func ListMetricSnapshotsSince(since time.Time) (snapshots []*NodeMetricSnapshot,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(rows, func(i int, j int) bool {
|
||||
if rows[i].CapturedAt.Equal(rows[j].CapturedAt) {
|
||||
return rows[i].ID > rows[j].ID
|
||||
}
|
||||
return rows[i].CapturedAt.After(rows[j].CapturedAt)
|
||||
})
|
||||
return rows, nil
|
||||
return utils.SortAndLimitRecords(rows, 0), nil
|
||||
}
|
||||
|
||||
func NodeMetricSnapshotExists(db *gorm.DB, nodeID string, capturedAt time.Time) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user