syncthing/lib/stats/device.go

87 lines
2.4 KiB
Go
Raw Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 19:43:32 +00:00
//
2015-03-07 20:36:35 +00: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 https://mozilla.org/MPL/2.0/.
package stats
import (
"time"
2015-08-06 09:29:25 +00:00
"github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/protocol"
)
const (
lastSeenKey = "lastSeen"
connDurationKey = "lastConnDuration"
)
type DeviceStatistics struct {
LastSeen time.Time `json:"lastSeen"`
LastConnectionDurationS float64 `json:"lastConnectionDurationS"`
}
type DeviceStatisticsReference struct {
2015-01-17 19:53:33 +00:00
ns *db.NamespacedKV
device protocol.DeviceID
}
func NewDeviceStatisticsReference(dba backend.Backend, device protocol.DeviceID) *DeviceStatisticsReference {
return &DeviceStatisticsReference{
ns: db.NewDeviceStatisticsNamespace(dba, device.String()),
device: device,
}
}
func (s *DeviceStatisticsReference) GetLastSeen() (time.Time, error) {
t, ok, err := s.ns.Time(lastSeenKey)
if err != nil {
return time.Time{}, err
} else if !ok {
2015-01-17 19:53:33 +00:00
// The default here is 1970-01-01 as opposed to the default
// time.Time{} from s.ns
return time.Unix(0, 0), nil
}
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
return t, nil
}
func (s *DeviceStatisticsReference) GetLastConnectionDuration() (time.Duration, error) {
d, ok, err := s.ns.Int64(connDurationKey)
if err != nil {
return 0, err
} else if !ok {
return 0, nil
}
l.Debugln("stats.DeviceStatisticsReference.GetLastConnectionDuration:", s.device, d)
return time.Duration(d), nil
}
func (s *DeviceStatisticsReference) WasSeen() error {
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
return s.ns.PutTime(lastSeenKey, time.Now())
}
func (s *DeviceStatisticsReference) LastConnectionDuration(d time.Duration) error {
l.Debugln("stats.DeviceStatisticsReference.LastConnectionDuration:", s.device, d)
return s.ns.PutInt64(connDurationKey, d.Nanoseconds())
}
func (s *DeviceStatisticsReference) GetStatistics() (DeviceStatistics, error) {
lastSeen, err := s.GetLastSeen()
if err != nil {
return DeviceStatistics{}, err
}
lastConnDuration, err := s.GetLastConnectionDuration()
if err != nil {
return DeviceStatistics{}, err
}
return DeviceStatistics{
LastSeen: lastSeen,
LastConnectionDurationS: lastConnDuration.Seconds(),
}, nil
}