folder.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 https://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. LastScan time.Time `json:"lastScan"`
  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. Deleted bool `json:"deleted"`
  23. }
  24. func NewFolderStatisticsReference(ldb *db.Instance, folder string) *FolderStatisticsReference {
  25. prefix := string(db.KeyTypeFolderStatistic) + folder
  26. return &FolderStatisticsReference{
  27. ns: db.NewNamespacedKV(ldb, prefix),
  28. folder: folder,
  29. }
  30. }
  31. func (s *FolderStatisticsReference) GetLastFile() LastFile {
  32. at, ok := s.ns.Time("lastFileAt")
  33. if !ok {
  34. return LastFile{}
  35. }
  36. file, ok := s.ns.String("lastFileName")
  37. if !ok {
  38. return LastFile{}
  39. }
  40. deleted, _ := s.ns.Bool("lastFileDeleted")
  41. return LastFile{
  42. At: at,
  43. Filename: file,
  44. Deleted: deleted,
  45. }
  46. }
  47. func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) {
  48. l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
  49. s.ns.PutTime("lastFileAt", time.Now())
  50. s.ns.PutString("lastFileName", file)
  51. s.ns.PutBool("lastFileDeleted", deleted)
  52. }
  53. func (s *FolderStatisticsReference) ScanCompleted() {
  54. s.ns.PutTime("lastScan", time.Now())
  55. }
  56. func (s *FolderStatisticsReference) GetLastScanTime() time.Time {
  57. lastScan, ok := s.ns.Time("lastScan")
  58. if !ok {
  59. return time.Time{}
  60. }
  61. return lastScan
  62. }
  63. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  64. return FolderStatistics{
  65. LastFile: s.GetLastFile(),
  66. LastScan: s.GetLastScanTime(),
  67. }
  68. }