folder.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package stats
  16. import (
  17. "time"
  18. "github.com/syncthing/syncthing/internal/db"
  19. "github.com/syndtr/goleveldb/leveldb"
  20. )
  21. type FolderStatistics struct {
  22. LastFile LastFile
  23. }
  24. type FolderStatisticsReference struct {
  25. ns *db.NamespacedKV
  26. folder string
  27. }
  28. type LastFile struct {
  29. At time.Time
  30. Filename string
  31. }
  32. func NewFolderStatisticsReference(ldb *leveldb.DB, folder string) *FolderStatisticsReference {
  33. prefix := string(db.KeyTypeFolderStatistic) + folder
  34. return &FolderStatisticsReference{
  35. ns: db.NewNamespacedKV(ldb, prefix),
  36. folder: folder,
  37. }
  38. }
  39. func (s *FolderStatisticsReference) GetLastFile() LastFile {
  40. at, ok := s.ns.Time("lastFileAt")
  41. if !ok {
  42. return LastFile{}
  43. }
  44. file, ok := s.ns.String("lastFileName")
  45. if !ok {
  46. return LastFile{}
  47. }
  48. return LastFile{
  49. At: at,
  50. Filename: file,
  51. }
  52. }
  53. func (s *FolderStatisticsReference) ReceivedFile(filename string) {
  54. if debug {
  55. l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, filename)
  56. }
  57. s.ns.PutTime("lastFileAt", time.Now())
  58. s.ns.PutString("lastFileName", filename)
  59. }
  60. func (s *FolderStatisticsReference) GetStatistics() FolderStatistics {
  61. return FolderStatistics{
  62. LastFile: s.GetLastFile(),
  63. }
  64. }