folder.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/protocol"
  10. "github.com/syncthing/syncthing/internal/db"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. )
  13. type FolderStatistics struct {
  14. LastFile LastFile `json:"lastFile"`
  15. }
  16. type FolderStatisticsReference struct {
  17. ns *db.NamespacedKV
  18. folder string
  19. }
  20. type LastFile struct {
  21. At time.Time `json:"at"`
  22. Filename string `json:"filename"`
  23. Deleted bool `json:"deleted"`
  24. }
  25. func NewFolderStatisticsReference(ldb *leveldb.DB, folder string) *FolderStatisticsReference {
  26. prefix := string(db.KeyTypeFolderStatistic) + folder
  27. return &FolderStatisticsReference{
  28. ns: db.NewNamespacedKV(ldb, prefix),
  29. folder: folder,
  30. }
  31. }
  32. func (s *FolderStatisticsReference) GetLastFile() LastFile {
  33. at, ok := s.ns.Time("lastFileAt")
  34. if !ok {
  35. return LastFile{}
  36. }
  37. file, ok := s.ns.String("lastFileName")
  38. if !ok {
  39. return LastFile{}
  40. }
  41. deleted, ok := s.ns.Bool("lastFileDeleted")
  42. return LastFile{
  43. At: at,
  44. Filename: file,
  45. Deleted: deleted,
  46. }
  47. }
  48. func (s *FolderStatisticsReference) ReceivedFile(file protocol.FileInfo) {
  49. if debug {
  50. l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
  51. }
  52. s.ns.PutTime("lastFileAt", time.Now())
  53. s.ns.PutString("lastFileName", file.Name)
  54. s.ns.PutBool("lastFileDeleted", file.IsDeleted())
  55. }
  56. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  57. return FolderStatistics{
  58. LastFile: s.GetLastFile(),
  59. }
  60. }