Parcourir la source

:art: 数据统计

Liang Ding il y a 2 ans
Parent
commit
953ce38ab7

+ 8 - 0
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
     }
 }
 

+ 3 - 0
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;

+ 7 - 4
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{}
 }

+ 7 - 1
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()
 }
 

+ 0 - 7
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
-}

+ 13 - 0
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()

+ 13 - 0
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, ".")
 }