2024-07-07 20:09:13 +00:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver implementations
|
|
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
2024-10-19 01:24:14 +00:00
|
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.55
|
2024-07-07 20:09:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
2024-07-16 20:58:06 +00:00
|
|
|
"time"
|
2024-07-07 20:09:13 +00:00
|
|
|
|
|
|
|
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
|
|
|
|
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/model"
|
|
|
|
"git.kmsign.ru/royalcat/tstor/src/sources/torrent"
|
2024-07-16 20:58:06 +00:00
|
|
|
tinfohash "github.com/anacrolix/torrent/types/infohash"
|
2024-07-07 20:09:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Torrents is the resolver for the torrents field.
|
|
|
|
func (r *torrentDaemonQueryResolver) Torrents(ctx context.Context, obj *model.TorrentDaemonQuery, filter *model.TorrentsFilter) ([]*model.Torrent, error) {
|
2024-10-19 01:24:14 +00:00
|
|
|
torrents, err := r.ATorrentDaemon.ListTorrents(ctx)
|
2024-07-07 20:09:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filterFuncs := []func(torrent *model.Torrent) bool{}
|
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
if filter.BytesCompleted != nil {
|
|
|
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
|
|
|
return filter.BytesCompleted.Include(torrent.BytesCompleted)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if filter.BytesMissing != nil {
|
|
|
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
|
|
|
return filter.BytesMissing.Include(torrent.BytesMissing)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if filter.PeersCount != nil {
|
|
|
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
|
|
|
return filter.PeersCount.Include(
|
|
|
|
int64(len(torrent.T.Torrent().PeerConns())),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if filter.Infohash != nil {
|
|
|
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
|
|
|
return filter.Infohash.Include(
|
|
|
|
torrent.Infohash,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if filter.Priority != nil {
|
|
|
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
|
|
|
return filter.Priority.Include(
|
|
|
|
torrent.Priority,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filterFunc := func(torrent *model.Torrent) bool {
|
|
|
|
for _, f := range filterFuncs {
|
|
|
|
if !f(torrent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := []*model.Torrent{}
|
|
|
|
for _, t := range torrents {
|
|
|
|
d, err := model.MapTorrent(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filterFunc(d) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tr = append(tr, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.SortStableFunc(torrents, func(t1, t2 *torrent.Controller) int {
|
|
|
|
return strings.Compare(t1.Name(), t2.Name())
|
|
|
|
})
|
|
|
|
|
|
|
|
return tr, nil
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:58:06 +00:00
|
|
|
// ClientStats is the resolver for the clientStats field.
|
|
|
|
func (r *torrentDaemonQueryResolver) ClientStats(ctx context.Context, obj *model.TorrentDaemonQuery) (*model.TorrentClientStats, error) {
|
2024-10-19 01:24:14 +00:00
|
|
|
stats := r.ATorrentDaemon.Stats()
|
2024-07-16 20:58:06 +00:00
|
|
|
return &model.TorrentClientStats{
|
2024-07-10 09:26:17 +00:00
|
|
|
BytesWritten: stats.BytesWritten.Int64(),
|
|
|
|
BytesRead: stats.BytesRead.Int64(),
|
|
|
|
BytesWrittenData: stats.BytesWrittenData.Int64(),
|
|
|
|
BytesReadData: stats.BytesReadData.Int64(),
|
|
|
|
BytesReadUsefulData: stats.BytesReadUsefulData.Int64(),
|
|
|
|
BytesReadUsefulIntendedData: stats.BytesReadUsefulIntendedData.Int64(),
|
|
|
|
ChunksWritten: stats.ChunksWritten.Int64(),
|
|
|
|
ChunksRead: stats.ChunksRead.Int64(),
|
|
|
|
ChunksReadUseful: stats.ChunksReadUseful.Int64(),
|
|
|
|
ChunksReadWasted: stats.ChunksReadWasted.Int64(),
|
|
|
|
MetadataChunksRead: stats.MetadataChunksRead.Int64(),
|
|
|
|
PiecesDirtiedGood: stats.PiecesDirtiedGood.Int64(),
|
|
|
|
PiecesDirtiedBad: stats.PiecesDirtiedBad.Int64(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:58:06 +00:00
|
|
|
// StatsHistory is the resolver for the statsHistory field.
|
|
|
|
func (r *torrentDaemonQueryResolver) StatsHistory(ctx context.Context, obj *model.TorrentDaemonQuery, since time.Time, infohash *string) ([]*model.TorrentStats, error) {
|
|
|
|
var stats []torrent.TorrentStats
|
|
|
|
if infohash == nil {
|
2024-10-19 01:24:14 +00:00
|
|
|
stats, err := r.ATorrentDaemon.StatsHistory(ctx, since)
|
2024-07-16 20:58:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.Apply(stats, model.MapTorrentStats), nil
|
|
|
|
} else if *infohash == "total" {
|
|
|
|
var err error
|
2024-10-19 01:24:14 +00:00
|
|
|
stats, err = r.ATorrentDaemon.TotalStatsHistory(ctx, since)
|
2024-07-16 20:58:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ih := tinfohash.FromHexString(*infohash)
|
|
|
|
var err error
|
2024-10-19 01:24:14 +00:00
|
|
|
stats, err = r.ATorrentDaemon.TorrentStatsHistory(ctx, since, ih)
|
2024-07-16 20:58:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return model.Apply(stats, model.MapTorrentStats), nil
|
|
|
|
}
|
|
|
|
|
2024-07-07 20:09:13 +00:00
|
|
|
// TorrentDaemonQuery returns graph.TorrentDaemonQueryResolver implementation.
|
|
|
|
func (r *Resolver) TorrentDaemonQuery() graph.TorrentDaemonQueryResolver {
|
|
|
|
return &torrentDaemonQueryResolver{r}
|
|
|
|
}
|
|
|
|
|
|
|
|
type torrentDaemonQueryResolver struct{ *Resolver }
|