2014-12-07 20:21:12 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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 http://mozilla.org/MPL/2.0/.
|
2014-12-07 20:21:12 +00:00
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2014-12-07 20:21:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FolderStatistics struct {
|
2016-06-02 19:26:52 +00:00
|
|
|
LastFile LastFile `json:"lastFile"`
|
|
|
|
LastScan time.Time `json:"lastScan"`
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FolderStatisticsReference struct {
|
2015-01-17 19:53:33 +00:00
|
|
|
ns *db.NamespacedKV
|
2014-12-07 20:21:12 +00:00
|
|
|
folder string
|
|
|
|
}
|
|
|
|
|
2015-01-17 19:53:33 +00:00
|
|
|
type LastFile struct {
|
2015-03-10 22:45:43 +00:00
|
|
|
At time.Time `json:"at"`
|
|
|
|
Filename string `json:"filename"`
|
2015-06-16 11:12:34 +00:00
|
|
|
Deleted bool `json:"deleted"`
|
2015-01-17 19:53:33 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func NewFolderStatisticsReference(ldb *db.Instance, folder string) *FolderStatisticsReference {
|
2015-01-17 19:53:33 +00:00
|
|
|
prefix := string(db.KeyTypeFolderStatistic) + folder
|
2014-12-07 20:21:12 +00:00
|
|
|
return &FolderStatisticsReference{
|
2015-01-17 19:53:33 +00:00
|
|
|
ns: db.NewNamespacedKV(ldb, prefix),
|
2014-12-07 20:21:12 +00:00
|
|
|
folder: folder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 19:53:33 +00:00
|
|
|
func (s *FolderStatisticsReference) GetLastFile() LastFile {
|
|
|
|
at, ok := s.ns.Time("lastFileAt")
|
|
|
|
if !ok {
|
|
|
|
return LastFile{}
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
file, ok := s.ns.String("lastFileName")
|
|
|
|
if !ok {
|
|
|
|
return LastFile{}
|
|
|
|
}
|
2015-06-16 11:12:34 +00:00
|
|
|
deleted, ok := s.ns.Bool("lastFileDeleted")
|
2015-01-17 19:53:33 +00:00
|
|
|
return LastFile{
|
|
|
|
At: at,
|
|
|
|
Filename: file,
|
2015-06-16 11:12:34 +00:00
|
|
|
Deleted: deleted,
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 11:22:59 +00:00
|
|
|
func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
|
2015-01-17 19:53:33 +00:00
|
|
|
s.ns.PutTime("lastFileAt", time.Now())
|
2015-09-04 11:22:59 +00:00
|
|
|
s.ns.PutString("lastFileName", file)
|
|
|
|
s.ns.PutBool("lastFileDeleted", deleted)
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 19:26:52 +00:00
|
|
|
func (s *FolderStatisticsReference) ScanCompleted() {
|
|
|
|
s.ns.PutTime("lastScan", time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FolderStatisticsReference) GetLastScanTime() time.Time {
|
|
|
|
lastScan, ok := s.ns.Time("lastScan")
|
|
|
|
if !ok {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
return lastScan
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:21:12 +00:00
|
|
|
func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
|
|
|
|
return FolderStatistics{
|
|
|
|
LastFile: s.GetLastFile(),
|
2016-06-02 19:26:52 +00:00
|
|
|
LastScan: s.GetLastScanTime(),
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
|
|
|
}
|