feat: downloader task stats (#339)

This commit is contained in:
Tracey Lesley authored and GitHub committed 2024-01-22 11:43:12 +08:00
1 parent 38ff711ea9
commit 6d136604db
7 files changed
+43 -13

No files matched your search

+13 -2
View File
@@ -35,7 +35,8 @@ const (
)
var (
ErrTaskNotFound = errors.New("task not found")
ErrTaskNotFound = errors.New("task not found")
ErrUnSupportedProtocol = errors.New("unsupported protocol")
)
type Listener func(event *Event)
@@ -211,7 +212,7 @@ func (d *Downloader) parseFb(url string) (fetcher.FetcherBuilder, error) {
if ok {
return fetchBuilder, nil
}
return nil, errors.New("unsupported protocol")
return nil, ErrUnSupportedProtocol
}
func (d *Downloader) setupFetcher(fetcher fetcher.Fetcher) {
@@ -508,6 +509,15 @@ func (d *Downloader) DeleteByStatues(statues []base.Status, force bool) (err err
return
}
func (d *Downloader) Stats(id string) (sr any, err error) {
task := d.GetTask(id)
if task == nil {
return sr, ErrTaskNotFound
}
sr = task.fetcher.Stats()
return
}
func (d *Downloader) doDelete(task *Task, force bool) (err error) {
err = func() error {
if task.fetcher != nil {
@@ -754,6 +764,7 @@ func (d *Downloader) doCreate(fetcher fetcher.Fetcher, opts *base.Options) (task
task := NewTask()
task.fetcherBuilder = fb
task.fetcher = fetcher
task.Protocol = fetcher.Name()
task.Meta = fetcher.Meta()
task.Progress = &Progress{}
initTask(task)
+2
View File
@@ -7,6 +7,7 @@ import (
"github.com/GopeedLab/gopeed/internal/protocol/http"
"github.com/GopeedLab/gopeed/pkg/base"
"github.com/GopeedLab/gopeed/pkg/util"
gonanoid "github.com/matoous/go-nanoid/v2"
"net/url"
"sync"
@@ -22,6 +23,7 @@ type Task struct {
ID string `json:"id"`
Meta *fetcher.FetcherMeta `json:"meta"`
Status base.Status `json:"status"`
Protocol string `json:"protocol"`
Progress *Progress `json:"progress"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`