syncthing/lib/stats/folder.go

70 lines
1.6 KiB
Go
Raw Normal View History

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