You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

storageStats.go 1.4 kB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  6. "gitlink.org.cn/cloudream/storage/common/pkgs/sysevent"
  7. "gorm.io/gorm"
  8. "log"
  9. )
  10. type StorageStatsWatcher struct {
  11. Name string
  12. }
  13. func (w *StorageStatsWatcher) OnEvent(event sysevent.SysEvent) {
  14. repo := NewStorageRepository(DB)
  15. if event.Category == "storageStats" {
  16. if storageStats, ok := event.Body.(*stgmod.BodyStorageStats); ok {
  17. storage, err := repo.GetStorageByID(int64(storageStats.StorageID))
  18. if err != nil {
  19. if errors.Is(err, gorm.ErrRecordNotFound) {
  20. // 插入新记录
  21. newStorage := &Storage{
  22. StorageID: storageStats.StorageID,
  23. DataCount: storageStats.DataCount,
  24. NewDataCount: 0,
  25. }
  26. repo.CreateStorage(newStorage)
  27. } else {
  28. log.Printf("Error querying storage: %v", err)
  29. }
  30. } else {
  31. // 更新记录
  32. newDataCount := storageStats.DataCount - storage.DataCount
  33. storage.DataCount = storageStats.DataCount
  34. storage.NewDataCount = newDataCount
  35. err := repo.UpdateStorage(storage)
  36. if err != nil {
  37. log.Printf("Error update storage: %v", err)
  38. }
  39. }
  40. } else {
  41. fmt.Printf("Watcher %s: Unexpected Body type, expected *BodyStorageInfo, got %T\n", w.Name, event.Body)
  42. }
  43. } else {
  44. fmt.Printf("Watcher %s received an event with category %s\n", w.Name, event.Category)
  45. }
  46. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。