mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 19:47:47 +08:00
feat: activity page
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"polaris/ent"
|
||||
"polaris/log"
|
||||
"strconv"
|
||||
|
||||
@@ -8,10 +9,27 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Activity struct {
|
||||
*ent.History
|
||||
InBackgroud bool `json:"in_backgroud"`
|
||||
}
|
||||
|
||||
func (s *Server) GetAllActivities(c *gin.Context) (interface{}, error) {
|
||||
his := s.db.GetHistories()
|
||||
var activities = make([]Activity, 0, len(his))
|
||||
for _, h := range his {
|
||||
a := Activity{
|
||||
History: h,
|
||||
}
|
||||
for id, task := range s.tasks {
|
||||
if h.ID == id && task.Processing {
|
||||
a.InBackgroud = true
|
||||
}
|
||||
}
|
||||
activities = append(activities, a)
|
||||
}
|
||||
|
||||
return his, nil
|
||||
return activities, nil
|
||||
}
|
||||
|
||||
func (s *Server) RemoveActivity(c *gin.Context) (interface{}, error) {
|
||||
|
||||
@@ -131,7 +131,7 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "save record")
|
||||
}
|
||||
s.tasks[history.ID] = torrent
|
||||
s.tasks[history.ID] = &Task{Torrent: torrent}
|
||||
|
||||
log.Infof("success add %s to download task", r1.Name)
|
||||
return &r1.Name, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"path/filepath"
|
||||
"polaris/db"
|
||||
"polaris/log"
|
||||
"polaris/pkg"
|
||||
"polaris/pkg/storage"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -28,9 +29,14 @@ func (s *Server) checkTasks() {
|
||||
log.Infof("task no longer exists: %v", id)
|
||||
continue
|
||||
}
|
||||
if t.Processing {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("task (%s) percentage done: %d%%", t.Name(), t.Progress())
|
||||
if t.Progress() == 100 {
|
||||
log.Infof("task is done: %v", t.Name())
|
||||
t.Processing = true
|
||||
go func() {
|
||||
if err := s.moveCompletedTask(id); err != nil {
|
||||
log.Infof("post tasks for id %v fail: %v", id, err)
|
||||
@@ -43,9 +49,6 @@ func (s *Server) checkTasks() {
|
||||
func (s *Server) moveCompletedTask(id int) error {
|
||||
torrent := s.tasks[id]
|
||||
r := s.db.GetHistory(id)
|
||||
s.db.SetHistoryComplete(r.ID)
|
||||
|
||||
delete(s.tasks, r.ID)
|
||||
|
||||
series := s.db.GetSeriesDetails(r.SeriesID)
|
||||
st := s.db.GetStorage(series.StorageID)
|
||||
@@ -68,5 +71,12 @@ func (s *Server) moveCompletedTask(id int) error {
|
||||
}
|
||||
log.Infof("move downloaded files to target dir success, file: %v, target dir: %v", torrent.Name(), r.TargetDir)
|
||||
torrent.Remove()
|
||||
delete(s.tasks, r.ID)
|
||||
s.db.SetHistoryComplete(r.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
Processing bool
|
||||
pkg.Torrent
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"polaris/db"
|
||||
"polaris/log"
|
||||
"polaris/pkg"
|
||||
"polaris/pkg/tmdb"
|
||||
"polaris/pkg/transmission"
|
||||
"polaris/ui"
|
||||
@@ -21,7 +20,7 @@ func NewServer(db *db.Client) *Server {
|
||||
r: r,
|
||||
db: db,
|
||||
cron: cron.New(),
|
||||
tasks: make(map[int]pkg.Torrent),
|
||||
tasks: make(map[int]*Task),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +29,7 @@ type Server struct {
|
||||
db *db.Client
|
||||
cron *cron.Cron
|
||||
language string
|
||||
tasks map[int]pkg.Torrent
|
||||
tasks map[int]*Task
|
||||
}
|
||||
|
||||
func (s *Server) Serve() error {
|
||||
@@ -118,6 +117,6 @@ func (s *Server) reloadTasks() {
|
||||
log.Errorf("relaod task %s failed: %v", t.SourceTitle, err)
|
||||
continue
|
||||
}
|
||||
s.tasks[t.ID] = torrent
|
||||
s.tasks[t.ID] = &Task{Torrent: torrent}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user