syncthing/lib/stats/folder.go

72 lines
1.7 KiB
Go
Raw Normal View History

// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 13:36:35 -07: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/.
package stats
import (
"time"
"github.com/syncthing/protocol"
2015-08-06 02:29:25 -07:00
"github.com/syncthing/syncthing/lib/db"
"github.com/syndtr/goleveldb/leveldb"
)
type FolderStatistics struct {
LastFile LastFile `json:"lastFile"`
}
type FolderStatisticsReference struct {
2015-01-17 12:53:33 -07:00
ns *db.NamespacedKV
folder string
}
2015-01-17 12:53:33 -07:00
type LastFile struct {
At time.Time `json:"at"`
Filename string `json:"filename"`
Deleted bool `json:"deleted"`
2015-01-17 12:53:33 -07:00
}
func NewFolderStatisticsReference(ldb *leveldb.DB, folder string) *FolderStatisticsReference {
prefix := string(db.KeyTypeFolderStatistic) + folder
return &FolderStatisticsReference{
2015-01-17 12:53:33 -07:00
ns: db.NewNamespacedKV(ldb, prefix),
folder: folder,
}
}
2015-01-17 12:53:33 -07:00
func (s *FolderStatisticsReference) GetLastFile() LastFile {
at, ok := s.ns.Time("lastFileAt")
if !ok {
return LastFile{}
}
2015-01-17 12:53:33 -07:00
file, ok := s.ns.String("lastFileName")
if !ok {
return LastFile{}
}
deleted, ok := s.ns.Bool("lastFileDeleted")
2015-01-17 12:53:33 -07:00
return LastFile{
At: at,
Filename: file,
Deleted: deleted,
}
}
func (s *FolderStatisticsReference) ReceivedFile(file protocol.FileInfo) {
if debug {
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
}
2015-01-17 12:53:33 -07:00
s.ns.PutTime("lastFileAt", time.Now())
s.ns.PutString("lastFileName", file.Name)
s.ns.PutBool("lastFileDeleted", file.IsDeleted())
}
func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
return FolderStatistics{
LastFile: s.GetLastFile(),
}
}