watcher/service/tracer_svc.go

46 lines
1009 B
Go
Raw Permalink Normal View History

2024-12-02 19:07:39 +08:00
package service
import (
"time"
2024-12-02 19:07:39 +08:00
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
"github.com/gofiber/fiber/v3"
"gorm.io/gorm"
)
type Tracer struct {
storage *storage.Storage
}
func NewTracer(storage *storage.Storage) *Tracer {
return &Tracer{
storage: storage,
}
}
func (s *Tracer) Registry(r fiber.Router) {
api := r.Group("/api/v2")
api.Get("tracers", s.List)
}
func (s *Tracer) List(c fiber.Ctx) error {
var q storage.GetProviderArticleQuery
if err := c.Bind().Query(&q); err != nil {
return err
}
q.TraceAtsNotNull = true
var query = storage.NewPageListQuery(&q)
if err := c.Bind().Query(query); err != nil {
return err
}
articles, total, err := s.storage.ProviderArticle().List(*query, func(db *gorm.DB) *gorm.DB {
db = db.Preload("HistoryAts", "created_at > ?", time.Now().Add(-12*time.Hour))
2024-12-02 19:07:39 +08:00
return db
})
if err != nil {
return err
}
return c.JSON(web.NewResponse(web.NewListResponse(total, articles)))
}