2014-12-07 20:21:12 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-01-17 19:53:33 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/db"
|
2014-12-07 20:21:12 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FolderStatistics struct {
|
2015-01-17 19:53:33 +00:00
|
|
|
LastFile LastFile
|
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 {
|
|
|
|
At time.Time
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFolderStatisticsReference(ldb *leveldb.DB, folder string) *FolderStatisticsReference {
|
|
|
|
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{}
|
|
|
|
}
|
|
|
|
return LastFile{
|
|
|
|
At: at,
|
|
|
|
Filename: file,
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FolderStatisticsReference) ReceivedFile(filename string) {
|
|
|
|
if debug {
|
2015-01-17 19:53:33 +00:00
|
|
|
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, filename)
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
s.ns.PutTime("lastFileAt", time.Now())
|
|
|
|
s.ns.PutString("lastFileName", filename)
|
2014-12-07 20:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
|
|
|
|
return FolderStatistics{
|
|
|
|
LastFile: s.GetLastFile(),
|
|
|
|
}
|
|
|
|
}
|