diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 83faf1854..3f8a42a3d 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -405,6 +405,14 @@ declare interface IConfig { virtualRefAlias: boolean virtualRefAnchor: boolean virtualRefDoc: boolean + }, + stat: { + treeCount: number + cTreeCount: number + blockCount: number + cBlockCount: number + dataSize: number + cDataSize: number } } diff --git a/app/src/util/assets.ts b/app/src/util/assets.ts index eaff33424..b8163aae2 100644 --- a/app/src/util/assets.ts +++ b/app/src/util/assets.ts @@ -142,6 +142,9 @@ export const addGA = () => { subscriptionType: -1, syncEnabled: false, syncProvider: -1, + cTreeCount: window.siyuan.config.stat.cTreeCount, + cBlockCount: window.siyuan.config.stat.cBlockCount, + cDataSize: window.siyuan.config.stat.cDataSize, }; if (window.siyuan.user) { para.isLoggedIn = true; diff --git a/kernel/conf/stat.go b/kernel/conf/stat.go index 56a3ab094..55778ab5c 100644 --- a/kernel/conf/stat.go +++ b/kernel/conf/stat.go @@ -17,11 +17,14 @@ package conf type Stat struct { - DocCount int `json:"docCount"` // 总文档计数 + TreeCount int `json:"treeCount"` + CTreeCount int `json:"cTreeCount"` + BlockCount int `json:"blockCount"` + CBlockCount int `json:"cBlockCount"` + DataSize int64 `json:"dataSize"` + CDataSize int64 `json:"cDataSize"` } func NewStat() *Stat { - return &Stat{ - DocCount: 0, - } + return &Stat{} } diff --git a/kernel/model/box.go b/kernel/model/box.go index 6e5e0854a..324e043e0 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -52,13 +52,19 @@ type Box struct { } func AutoStat() { + autoStat() for range time.Tick(10 * time.Minute) { autoStat() } } func autoStat() { - Conf.Stat.DocCount = sql.CountAllDoc() + Conf.Stat.TreeCount = treenode.CountTrees() + Conf.Stat.CTreeCount = treenode.CeilCount(Conf.Stat.TreeCount) + Conf.Stat.BlockCount = treenode.CountBlocks() + Conf.Stat.BlockCount = treenode.CeilCount(Conf.Stat.BlockCount) + Conf.Stat.DataSize, _ = util.SizeOfDirectory(util.DataDir) + Conf.Stat.CDataSize = util.CeilSize(Conf.Stat.DataSize) Conf.Save() } diff --git a/kernel/sql/stat.go b/kernel/sql/stat.go index 8e0f2f45e..e8222d8d5 100644 --- a/kernel/sql/stat.go +++ b/kernel/sql/stat.go @@ -92,10 +92,3 @@ func getStat(key string) (ret string) { row.Scan(&ret) return } - -func CountAllDoc() (ret int) { - sqlStmt := "SELECT COUNT(*) FROM blocks WHERE type = 'd'" - row := queryRow(sqlStmt) - row.Scan(&ret) - return -} diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go index 173823dd9..b79754cb3 100644 --- a/kernel/treenode/blocktree.go +++ b/kernel/treenode/blocktree.go @@ -71,6 +71,19 @@ func CountBlocks() (ret int) { return len(blockTrees) } +func CeilCount(count int) int { + if 100 > count { + return 100 + } + + for i := 1; i < 40; i++ { + if count < i*500 { + return i * 500 + } + } + return 500*40 + 1 +} + func GetBlockTreeRootByPath(boxID, path string) *BlockTree { blockTreesLock.Lock() defer blockTreesLock.Unlock() diff --git a/kernel/util/file.go b/kernel/util/file.go index 174db57d0..526f91de8 100644 --- a/kernel/util/file.go +++ b/kernel/util/file.go @@ -209,6 +209,19 @@ func SizeOfDirectory(path string) (size int64, err error) { return } +func CeilSize(size int64) int64 { + if 100*1024*1024 > size { + return 100 * 1024 * 1024 + } + + for i := int64(1); i < 40; i++ { + if 1024*1024*200*i > size { + return 1024 * 1024 * int64(i) + } + } + return 1024*1024*200*40 + 1 +} + func IsReservedFilename(baseName string) bool { return "assets" == baseName || "templates" == baseName || "widgets" == baseName || "emojis" == baseName || ".siyuan" == baseName || strings.HasPrefix(baseName, ".") }