diff --git a/core/filestorage/filesync/stats.go b/core/filestorage/filesync/stats.go index d5c2fca36..e986b76c0 100644 --- a/core/filestorage/filesync/stats.go +++ b/core/filestorage/filesync/stats.go @@ -8,7 +8,9 @@ import ( "github.com/ipfs/go-cid" ipld "github.com/ipfs/go-ipld-format" "github.com/samber/lo" + "go.uber.org/zap" + "github.com/anytypeio/go-anytype-middleware/pb" "github.com/anytypeio/go-anytype-middleware/util/conc" ) @@ -37,13 +39,45 @@ func (f *fileSync) SpaceStat(ctx context.Context, spaceId string) (ss SpaceStat, if err != nil { return } - return SpaceStat{ + newStats := SpaceStat{ SpaceId: spaceId, FileCount: int(info.FilesCount), CidsCount: int(info.CidsCount), BytesUsage: int(info.UsageBytes), BytesLimit: int(info.LimitBytes), - }, nil + } + f.spaceStatsLock.Lock() + prevStats, ok := f.spaceStats[spaceId] + if prevStats != newStats { + f.spaceStats[spaceId] = newStats + // Do not send event if it is first time we get stats + if ok { + f.sendSpaceUsageEvent(uint64(newStats.BytesUsage)) + } + } + f.spaceStatsLock.Unlock() + + return newStats, nil +} + +func (f *fileSync) updateSpaceUsageInformation(spaceId string) { + if _, err := f.SpaceStat(context.Background(), spaceId); err != nil { + log.Warn("can't get space usage information", zap.String("spaceId", spaceId), zap.Error(err)) + } +} + +func (f *fileSync) sendSpaceUsageEvent(bytesUsage uint64) { + f.sendEvent(&pb.Event{ + Messages: []*pb.EventMessage{ + { + Value: &pb.EventMessageValueOfFileSpaceUsage{ + FileSpaceUsage: &pb.EventFileSpaceUsage{ + BytesUsage: bytesUsage, + }, + }, + }, + }, + }) } func (f *fileSync) FileListStats(ctx context.Context, spaceID string, fileIDs []string) ([]FileStat, error) {