folder.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "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. Deleted bool `json:"deleted"`
  23. }
  24. func NewFolderStatisticsReference(ldb *leveldb.DB, 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, ok := 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) GetStatistics() FolderStatistics {
  54. return FolderStatistics{
  55. LastFile: s.GetLastFile(),
  56. }
  57. }