mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-16 18:41:59 -07:00
2a4fc28318
We're going to need the db.Instance to keep some state, and for that to work we need the same one passed around everywhere. Hence this moves the leveldb-specific file opening stuff into the db package and exports the dbInstance type.
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// 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/syncthing/lib/db"
|
|
)
|
|
|
|
type FolderStatistics struct {
|
|
LastFile LastFile `json:"lastFile"`
|
|
}
|
|
|
|
type FolderStatisticsReference struct {
|
|
ns *db.NamespacedKV
|
|
folder string
|
|
}
|
|
|
|
type LastFile struct {
|
|
At time.Time `json:"at"`
|
|
Filename string `json:"filename"`
|
|
Deleted bool `json:"deleted"`
|
|
}
|
|
|
|
func NewFolderStatisticsReference(ldb *db.Instance, folder string) *FolderStatisticsReference {
|
|
prefix := string(db.KeyTypeFolderStatistic) + folder
|
|
return &FolderStatisticsReference{
|
|
ns: db.NewNamespacedKV(ldb, prefix),
|
|
folder: folder,
|
|
}
|
|
}
|
|
|
|
func (s *FolderStatisticsReference) GetLastFile() LastFile {
|
|
at, ok := s.ns.Time("lastFileAt")
|
|
if !ok {
|
|
return LastFile{}
|
|
}
|
|
file, ok := s.ns.String("lastFileName")
|
|
if !ok {
|
|
return LastFile{}
|
|
}
|
|
deleted, ok := s.ns.Bool("lastFileDeleted")
|
|
return LastFile{
|
|
At: at,
|
|
Filename: file,
|
|
Deleted: deleted,
|
|
}
|
|
}
|
|
|
|
func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) {
|
|
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
|
|
s.ns.PutTime("lastFileAt", time.Now())
|
|
s.ns.PutString("lastFileName", file)
|
|
s.ns.PutBool("lastFileDeleted", deleted)
|
|
}
|
|
|
|
func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
|
|
return FolderStatistics{
|
|
LastFile: s.GetLastFile(),
|
|
}
|
|
}
|