2019-07-16 19:56:20 +08:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2019-07-23 03:11:46 +08:00
|
|
|
"encoding/json"
|
2019-07-16 19:56:20 +08:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
2019-07-23 03:11:46 +08:00
|
|
|
// TransferSnapshot represents state of an account at point in time.
|
|
|
|
type TransferSnapshot struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
Checked bool `json:"checked"`
|
|
|
|
StartedAt time.Time `json:"started_at"`
|
|
|
|
CompletedAt time.Time `json:"completed_at,omitempty"`
|
|
|
|
Error error `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (as TransferSnapshot) MarshalJSON() ([]byte, error) {
|
|
|
|
err := ""
|
|
|
|
if as.Error != nil {
|
|
|
|
err = as.Error.Error()
|
|
|
|
}
|
|
|
|
type Alias TransferSnapshot
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Alias
|
|
|
|
}{
|
|
|
|
Error: err,
|
|
|
|
Alias: (Alias)(as),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:56:20 +08:00
|
|
|
// Transfer keeps track of initiated transfers and provides access to
|
|
|
|
// accounting functions.
|
|
|
|
// Transfer needs to be closed on completion.
|
|
|
|
type Transfer struct {
|
2019-07-23 03:11:46 +08:00
|
|
|
stats *StatsInfo
|
|
|
|
remote string
|
|
|
|
size int64
|
|
|
|
checking bool
|
2019-07-16 19:56:20 +08:00
|
|
|
|
2019-07-23 03:11:46 +08:00
|
|
|
// Protects all bellow
|
2019-07-16 19:56:20 +08:00
|
|
|
mu sync.Mutex
|
2019-07-23 03:11:46 +08:00
|
|
|
acc *Account
|
|
|
|
err error
|
2019-07-16 19:56:20 +08:00
|
|
|
startedAt time.Time
|
|
|
|
completedAt time.Time
|
|
|
|
}
|
|
|
|
|
2019-07-23 03:11:46 +08:00
|
|
|
// newCheckingTransfer instantiates new checking of the object.
|
|
|
|
func newCheckingTransfer(stats *StatsInfo, obj fs.Object) *Transfer {
|
|
|
|
return newTransferRemoteSize(stats, obj.Remote(), obj.Size(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTransfer instantiates new transfer.
|
2019-07-16 19:56:20 +08:00
|
|
|
func newTransfer(stats *StatsInfo, obj fs.Object) *Transfer {
|
2019-07-23 03:11:46 +08:00
|
|
|
return newTransferRemoteSize(stats, obj.Remote(), obj.Size(), false)
|
2019-07-16 19:56:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-23 03:11:46 +08:00
|
|
|
func newTransferRemoteSize(stats *StatsInfo, remote string, size int64, checking bool) *Transfer {
|
2019-07-16 19:56:20 +08:00
|
|
|
tr := &Transfer{
|
|
|
|
stats: stats,
|
|
|
|
remote: remote,
|
|
|
|
size: size,
|
|
|
|
startedAt: time.Now(),
|
2019-07-23 03:11:46 +08:00
|
|
|
checking: checking,
|
2019-07-16 19:56:20 +08:00
|
|
|
}
|
|
|
|
stats.AddTransfer(tr)
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done ends the transfer.
|
|
|
|
// Must be called after transfer is finished to run proper cleanups.
|
|
|
|
func (tr *Transfer) Done(err error) {
|
2019-07-23 03:11:46 +08:00
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
|
|
|
|
2019-07-16 19:56:20 +08:00
|
|
|
if err != nil {
|
|
|
|
tr.stats.Error(err)
|
2019-07-23 03:11:46 +08:00
|
|
|
tr.err = err
|
2019-07-16 19:56:20 +08:00
|
|
|
}
|
|
|
|
if tr.acc != nil {
|
|
|
|
if err := tr.acc.Close(); err != nil {
|
|
|
|
fs.LogLevelPrintf(fs.Config.StatsLogLevel, nil, "can't close account: %+v\n", err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 03:11:46 +08:00
|
|
|
if tr.checking {
|
|
|
|
tr.stats.DoneChecking(tr.remote)
|
|
|
|
} else {
|
|
|
|
tr.stats.DoneTransferring(tr.remote, err == nil)
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:56:20 +08:00
|
|
|
tr.completedAt = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account returns reader that knows how to keep track of transfer progress.
|
|
|
|
func (tr *Transfer) Account(in io.ReadCloser) *Account {
|
2019-07-23 03:11:46 +08:00
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
|
|
|
|
|
|
|
if tr.acc == nil {
|
|
|
|
tr.acc = newAccountSizeName(tr.stats, in, tr.size, tr.remote)
|
|
|
|
|
2019-07-16 19:56:20 +08:00
|
|
|
}
|
2019-07-23 03:11:46 +08:00
|
|
|
return tr.acc
|
2019-07-16 19:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TimeRange returns the time transfer started and ended at. If not completed
|
|
|
|
// it will return zero time for end time.
|
|
|
|
func (tr *Transfer) TimeRange() (time.Time, time.Time) {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
|
|
|
return tr.startedAt, tr.completedAt
|
|
|
|
}
|
2019-07-23 03:11:46 +08:00
|
|
|
|
|
|
|
// IsDone returns true if transfer is completed.
|
|
|
|
func (tr *Transfer) IsDone() bool {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
|
|
|
return !tr.completedAt.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot produces stats for this account at point in time.
|
|
|
|
func (tr *Transfer) Snapshot() TransferSnapshot {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
|
|
|
|
|
|
|
var s, b int64 = tr.size, 0
|
|
|
|
if tr.acc != nil {
|
|
|
|
b, s = tr.acc.progress()
|
|
|
|
}
|
|
|
|
return TransferSnapshot{
|
|
|
|
Name: tr.remote,
|
|
|
|
Checked: tr.checking,
|
|
|
|
Size: s,
|
|
|
|
Bytes: b,
|
|
|
|
StartedAt: tr.startedAt,
|
|
|
|
CompletedAt: tr.completedAt,
|
|
|
|
Error: tr.err,
|
|
|
|
}
|
|
|
|
}
|