mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-06 05:17:49 +00:00
4d979a1ce9
This truncates times meant for API consumption to second precision, where fractions won't typically matter or add any value. Exception to this is timestamps on logs and events, and of course I'm not touching things like file metadata. I'm not 100% certain this is an exhaustive change, but it's the things I found by grepping and following the breadcrumbs from lib/api... I also considered general-but-ugly solutions, like having the API serializer itself do reflection magic or even regexps on returned objects, but decided against it because aurgh...
104 lines
2.5 KiB
Go
104 lines
2.5 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().Truncate(time.Second)); 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().Truncate(time.Second))
|
|
}
|
|
|
|
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
|
|
}
|