1
0

folder.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package stats
  7. import (
  8. "time"
  9. "github.com/syncthing/syncthing/internal/db"
  10. "github.com/syndtr/goleveldb/leveldb"
  11. )
  12. type FolderStatistics struct {
  13. LastFile LastFile `json:"lastFile"`
  14. }
  15. type FolderStatisticsReference struct {
  16. ns *db.NamespacedKV
  17. folder string
  18. }
  19. type LastFile struct {
  20. At time.Time `json:"at"`
  21. Filename string `json:"filename"`
  22. }
  23. func NewFolderStatisticsReference(ldb *leveldb.DB, folder string) *FolderStatisticsReference {
  24. prefix := string(db.KeyTypeFolderStatistic) + folder
  25. return &FolderStatisticsReference{
  26. ns: db.NewNamespacedKV(ldb, prefix),
  27. folder: folder,
  28. }
  29. }
  30. func (s *FolderStatisticsReference) GetLastFile() LastFile {
  31. at, ok := s.ns.Time("lastFileAt")
  32. if !ok {
  33. return LastFile{}
  34. }
  35. file, ok := s.ns.String("lastFileName")
  36. if !ok {
  37. return LastFile{}
  38. }
  39. return LastFile{
  40. At: at,
  41. Filename: file,
  42. }
  43. }
  44. func (s *FolderStatisticsReference) ReceivedFile(filename string) {
  45. if debug {
  46. l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, filename)
  47. }
  48. s.ns.PutTime("lastFileAt", time.Now())
  49. s.ns.PutString("lastFileName", filename)
  50. }
  51. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  52. return FolderStatistics{
  53. LastFile: s.GetLastFile(),
  54. }
  55. }