folder.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Lowlevel, folder string) *FolderStatisticsReference {
  25. return &FolderStatisticsReference{
  26. ns: db.NewFolderStatisticsNamespace(ldb, folder),
  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, _ := 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) ScanCompleted() {
  53. s.ns.PutTime("lastScan", time.Now())
  54. }
  55. func (s *FolderStatisticsReference) GetLastScanTime() time.Time {
  56. lastScan, ok := s.ns.Time("lastScan")
  57. if !ok {
  58. return time.Time{}
  59. }
  60. return lastScan
  61. }
  62. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  63. return FolderStatistics{
  64. LastFile: s.GetLastFile(),
  65. LastScan: s.GetLastScanTime(),
  66. }
  67. }