mv internal lib
This commit is contained in:
19
lib/stats/debug.go
Normal file
19
lib/stats/debug.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// 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 (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/calmh/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
debug = strings.Contains(os.Getenv("STTRACE"), "stats") || os.Getenv("STTRACE") == "all"
|
||||
l = logger.DefaultLogger
|
||||
)
|
||||
58
lib/stats/device.go
Normal file
58
lib/stats/device.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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/protocol"
|
||||
"github.com/syncthing/syncthing/lib/db"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
)
|
||||
|
||||
type DeviceStatistics struct {
|
||||
LastSeen time.Time `json:"lastSeen"`
|
||||
}
|
||||
|
||||
type DeviceStatisticsReference struct {
|
||||
ns *db.NamespacedKV
|
||||
device protocol.DeviceID
|
||||
}
|
||||
|
||||
func NewDeviceStatisticsReference(ldb *leveldb.DB, device protocol.DeviceID) *DeviceStatisticsReference {
|
||||
prefix := string(db.KeyTypeDeviceStatistic) + device.String()
|
||||
return &DeviceStatisticsReference{
|
||||
ns: db.NewNamespacedKV(ldb, prefix),
|
||||
device: device,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
|
||||
t, ok := s.ns.Time("lastSeen")
|
||||
if !ok {
|
||||
// The default here is 1970-01-01 as opposed to the default
|
||||
// time.Time{} from s.ns
|
||||
return time.Unix(0, 0)
|
||||
}
|
||||
if debug {
|
||||
l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (s *DeviceStatisticsReference) WasSeen() {
|
||||
if debug {
|
||||
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
|
||||
}
|
||||
s.ns.PutTime("lastSeen", time.Now())
|
||||
}
|
||||
|
||||
func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
|
||||
return DeviceStatistics{
|
||||
LastSeen: s.GetLastSeen(),
|
||||
}
|
||||
}
|
||||
71
lib/stats/folder.go
Normal file
71
lib/stats/folder.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// 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/protocol"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/db"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
)
|
||||
|
||||
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 *leveldb.DB, 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 protocol.FileInfo) {
|
||||
if debug {
|
||||
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
|
||||
}
|
||||
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(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user