syncthing/lib/stats/folder.go
Jakob Borg e82a7e3dfa
all: Propagate errors from NamespacedKV (#6203)
As foretold by the prophecy, "once the database refactor is merged, then
shall appear a request to propagate errors from the store known
throughout the land as the NamedspacedKV, and it shall be good".
2019-11-30 13:03:24 +01:00

104 lines
2.4 KiB
Go

// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package stats
import (
"time"
"github.com/syncthing/syncthing/lib/db"
)
type FolderStatistics struct {
LastFile LastFile `json:"lastFile"`
LastScan time.Time `json:"lastScan"`
}
type FolderStatisticsReference struct {
ns *db.NamespacedKV
folder string
}
type LastFile struct {
At time.Time `json:"at"`
Filename string `json:"filename"`
Deleted bool `json:"deleted"`
}
func NewFolderStatisticsReference(ldb *db.Lowlevel, folder string) *FolderStatisticsReference {
return &FolderStatisticsReference{
ns: db.NewFolderStatisticsNamespace(ldb, folder),
folder: folder,
}
}
func (s *FolderStatisticsReference) GetLastFile() (LastFile, error) {
at, ok, err := s.ns.Time("lastFileAt")
if err != nil {
return LastFile{}, err
} else if !ok {
return LastFile{}, nil
}
file, ok, err := s.ns.String("lastFileName")
if err != nil {
return LastFile{}, err
} else if !ok {
return LastFile{}, nil
}
deleted, _, err := s.ns.Bool("lastFileDeleted")
if err != nil {
return LastFile{}, err
}
return LastFile{
At: at,
Filename: file,
Deleted: deleted,
}, nil
}
func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) error {
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
if err := s.ns.PutTime("lastFileAt", time.Now()); err != nil {
return err
}
if err := s.ns.PutString("lastFileName", file); err != nil {
return err
}
if err := s.ns.PutBool("lastFileDeleted", deleted); err != nil {
return err
}
return nil
}
func (s *FolderStatisticsReference) ScanCompleted() error {
return s.ns.PutTime("lastScan", time.Now())
}
func (s *FolderStatisticsReference) GetLastScanTime() (time.Time, error) {
lastScan, ok, err := s.ns.Time("lastScan")
if err != nil {
return time.Time{}, err
} else if !ok {
return time.Time{}, nil
}
return lastScan, nil
}
func (s *FolderStatisticsReference) GetStatistics() (FolderStatistics, error) {
lastFile, err := s.GetLastFile()
if err != nil {
return FolderStatistics{}, err
}
lastScanTime, err := s.GetLastScanTime()
if err != nil {
return FolderStatistics{}, err
}
return FolderStatistics{
LastFile: lastFile,
LastScan: lastScanTime,
}, nil
}