folder.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/lib/db"
  10. )
  11. type FolderStatistics struct {
  12. LastFile LastFile `json:"lastFile"`
  13. }
  14. type FolderStatisticsReference struct {
  15. ns *db.NamespacedKV
  16. folder string
  17. }
  18. type LastFile struct {
  19. At time.Time `json:"at"`
  20. Filename string `json:"filename"`
  21. Deleted bool `json:"deleted"`
  22. }
  23. func NewFolderStatisticsReference(ldb *db.Instance, 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. deleted, ok := s.ns.Bool("lastFileDeleted")
  40. return LastFile{
  41. At: at,
  42. Filename: file,
  43. Deleted: deleted,
  44. }
  45. }
  46. func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) {
  47. l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
  48. s.ns.PutTime("lastFileAt", time.Now())
  49. s.ns.PutString("lastFileName", file)
  50. s.ns.PutBool("lastFileDeleted", deleted)
  51. }
  52. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  53. return FolderStatistics{
  54. LastFile: s.GetLastFile(),
  55. }
  56. }