50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package collection
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// Repository 是采集模块需要的最小持久化接口。
|
|
type Repository interface {
|
|
Insert(context.Context, Record) error
|
|
UpsertRequest(context.Context, RequestRecord) error
|
|
ListRecent(context.Context, int) ([]Record, error)
|
|
QueryUsage(context.Context, UsageQuery) (UsagePage, error)
|
|
UsageDashboard(context.Context, time.Time, int) (UsageDashboard, error)
|
|
Close() error
|
|
}
|
|
|
|
// Service 负责保存观察和读取请求明细,不包含 SQL 实现。
|
|
type Service struct {
|
|
repository Repository
|
|
}
|
|
|
|
func NewService(repository Repository) *Service {
|
|
return &Service{repository: repository}
|
|
}
|
|
|
|
func (s *Service) Observe(ctx context.Context, record Record) error {
|
|
return s.repository.Insert(ctx, record)
|
|
}
|
|
|
|
func (s *Service) ObserveRequest(ctx context.Context, record RequestRecord) error {
|
|
return s.repository.UpsertRequest(ctx, record)
|
|
}
|
|
|
|
func (s *Service) Recent(ctx context.Context, limit int) ([]Record, error) {
|
|
return s.repository.ListRecent(ctx, limit)
|
|
}
|
|
|
|
func (s *Service) Query(ctx context.Context, query UsageQuery) (UsagePage, error) {
|
|
return s.repository.QueryUsage(ctx, query)
|
|
}
|
|
|
|
func (s *Service) Dashboard(ctx context.Context, today time.Time, days int) (UsageDashboard, error) {
|
|
return s.repository.UsageDashboard(ctx, today, days)
|
|
}
|
|
|
|
func (s *Service) Close() error {
|
|
return s.repository.Close()
|
|
}
|