diff --git a/kernel/conf/stat.go b/kernel/conf/stat.go index 55778ab5c..29825026c 100644 --- a/kernel/conf/stat.go +++ b/kernel/conf/stat.go @@ -23,6 +23,8 @@ type Stat struct { CBlockCount int `json:"cBlockCount"` DataSize int64 `json:"dataSize"` CDataSize int64 `json:"cDataSize"` + AssetsSize int64 `json:"assetsSize"` + CAssetsSize int64 `json:"cAssetsSize"` } func NewStat() *Stat { diff --git a/kernel/model/box.go b/kernel/model/box.go index 173d2e2bb..f37a44878 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -26,11 +26,13 @@ import ( "path/filepath" "sort" "strings" + "sync" "time" "github.com/88250/gulu" "github.com/88250/lute/ast" "github.com/88250/lute/parse" + "github.com/dustin/go-humanize" "github.com/facette/natsort" "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" @@ -52,20 +54,29 @@ type Box struct { } func AutoStat() { + time.Sleep(time.Minute) autoStat() - for range time.Tick(10 * time.Minute) { + for range time.Tick(2 * time.Hour) { autoStat() } } +var statLock = sync.Mutex{} + func autoStat() { + statLock.Lock() + defer statLock.Unlock() + Conf.Stat.TreeCount = treenode.CountTrees() Conf.Stat.CTreeCount = treenode.CeilCount(Conf.Stat.TreeCount) Conf.Stat.BlockCount = treenode.CountBlocks() Conf.Stat.CBlockCount = treenode.CeilCount(Conf.Stat.BlockCount) - Conf.Stat.DataSize, _ = util.SizeOfDirectory(util.DataDir) + Conf.Stat.DataSize, Conf.Stat.AssetsSize = util.DataSize() Conf.Stat.CDataSize = util.CeilSize(Conf.Stat.DataSize) + Conf.Stat.CAssetsSize = util.CeilSize(Conf.Stat.AssetsSize) Conf.Save() + + logging.LogInfof("auto stat [trees=%d, blocks=%d, dataSize=%s, assetsSize=%s]", Conf.Stat.TreeCount, Conf.Stat.BlockCount, humanize.Bytes(uint64(Conf.Stat.DataSize)), humanize.Bytes(uint64(Conf.Stat.AssetsSize))) } func ListNotebooks() (ret []*Box, err error) { diff --git a/kernel/util/file.go b/kernel/util/file.go index c1d788692..f40c1eab4 100644 --- a/kernel/util/file.go +++ b/kernel/util/file.go @@ -17,6 +17,7 @@ package util import ( + "io" "os" "path" "path/filepath" @@ -194,6 +195,27 @@ func SizeOfDirectory(path string) (size int64, err error) { return } +func DataSize() (dataSize, assetsSize int64) { + filepath.Walk(DataDir, func(path string, info os.FileInfo, err error) error { + if nil != err { + logging.LogErrorf("size of data failed: %s", err) + return io.EOF + } + if !info.IsDir() { + s := info.Size() + dataSize += s + + if strings.Contains(strings.TrimPrefix(path, DataDir), "assets") { + assetsSize += s + } + } else { + dataSize += 4096 + } + return nil + }) + return +} + func CeilSize(size int64) int64 { if 100*1024*1024 > size { return 100 * 1024 * 1024 diff --git a/kernel/util/runtime.go b/kernel/util/runtime.go index e9de5cdc9..412bc0ff6 100644 --- a/kernel/util/runtime.go +++ b/kernel/util/runtime.go @@ -25,7 +25,6 @@ import ( "github.com/88250/gulu" "github.com/denisbrodbeck/machineid" - "github.com/dustin/go-humanize" "github.com/siyuan-note/logging" ) @@ -41,9 +40,6 @@ const ( ) func logBootInfo() { - s, _ := SizeOfDirectory(DataDir) - dataDirSize := humanize.Bytes(uint64(s)) - logging.LogInfof("kernel is booting:\n"+ " * ver [%s]\n"+ " * arch [%s]\n"+ @@ -53,8 +49,8 @@ func logBootInfo() { " * read only [%v]\n"+ " * container [%s]\n"+ " * database [ver=%s]\n"+ - " * workspace directory [%s, data %s]", - Ver, runtime.GOARCH, os.Getpid(), Mode, WorkingDir, ReadOnly, Container, DatabaseVer, WorkspaceDir, dataDirSize) + " * workspace directory [%s]", + Ver, runtime.GOARCH, os.Getpid(), Mode, WorkingDir, ReadOnly, Container, DatabaseVer, WorkspaceDir) } func IsMutexLocked(m *sync.Mutex) bool {