syncthing/internal/stats/folder.go

75 lines
1.9 KiB
Go
Raw Normal View History

// 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"
"github.com/syndtr/goleveldb/leveldb"
)
type FolderStatistics struct {
2015-01-17 19:53:33 +00:00
LastFile LastFile
}
type FolderStatisticsReference struct {
2015-01-17 19:53:33 +00:00
ns *db.NamespacedKV
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
return &FolderStatisticsReference{
2015-01-17 19:53:33 +00:00
ns: db.NewNamespacedKV(ldb, prefix),
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{}
}
2015-01-17 19:53:33 +00:00
file, ok := s.ns.String("lastFileName")
if !ok {
return LastFile{}
}
return LastFile{
At: at,
Filename: file,
}
}
func (s *FolderStatisticsReference) ReceivedFile(filename string) {
if debug {
2015-01-17 19:53:33 +00:00
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, filename)
}
2015-01-17 19:53:33 +00:00
s.ns.PutTime("lastFileAt", time.Now())
s.ns.PutString("lastFileName", filename)
}
func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
return FolderStatistics{
LastFile: s.GetLastFile(),
}
}