2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-09-28 11:00:38 +00:00
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-01-13 12:22:56 +00:00
|
|
|
"github.com/syncthing/protocol"
|
2015-01-17 19:53:33 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/db"
|
2014-09-28 11:00:38 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeviceStatistics struct {
|
|
|
|
LastSeen time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeviceStatisticsReference struct {
|
2015-01-17 19:53:33 +00:00
|
|
|
ns *db.NamespacedKV
|
2014-09-28 11:00:38 +00:00
|
|
|
device protocol.DeviceID
|
|
|
|
}
|
|
|
|
|
2015-01-17 19:53:33 +00:00
|
|
|
func NewDeviceStatisticsReference(ldb *leveldb.DB, device protocol.DeviceID) *DeviceStatisticsReference {
|
|
|
|
prefix := string(db.KeyTypeDeviceStatistic) + device.String()
|
2014-09-28 11:00:38 +00:00
|
|
|
return &DeviceStatisticsReference{
|
2015-01-17 19:53:33 +00:00
|
|
|
ns: db.NewNamespacedKV(ldb, prefix),
|
2014-09-28 11:00:38 +00:00
|
|
|
device: device,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
|
2015-01-17 19:53:33 +00:00
|
|
|
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
|
2014-09-28 11:00:38 +00:00
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
if debug {
|
2015-01-17 19:53:33 +00:00
|
|
|
l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
return t
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeviceStatisticsReference) WasSeen() {
|
|
|
|
if debug {
|
|
|
|
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
|
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
s.ns.PutTime("lastSeen", time.Now())
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
|
|
|
|
return DeviceStatistics{
|
|
|
|
LastSeen: s.GetLastSeen(),
|
|
|
|
}
|
|
|
|
}
|