folder.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. if debug {
  49. l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
  50. }
  51. s.ns.PutTime("lastFileAt", time.Now())
  52. s.ns.PutString("lastFileName", file)
  53. s.ns.PutBool("lastFileDeleted", deleted)
  54. }
  55. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  56. return FolderStatistics{
  57. LastFile: s.GetLastFile(),
  58. }
  59. }