Add statistic api

This commit is contained in:
2025-10-02 21:34:35 +08:00
parent 17026a74c5
commit aac1992dba
5 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
package service
import (
"fmt"
"nysoure/server/dao"
"nysoure/server/utils"
"os"
"time"
)
type Statistic struct {
TotalResources int64 `json:"total_resources"`
TotalFiles int64 `json:"total_files"`
StartTime int64 `json:"start_time"`
}
var (
startTime int64
cache = utils.NewMemValueCache[*Statistic](1 * time.Minute)
)
func init() {
timeFile := utils.GetStoragePath() + "/.start_time"
if _, err := os.Stat(timeFile); os.IsNotExist(err) {
startTime = time.Now().Unix()
str := fmt.Sprintf("%d", startTime)
err := os.WriteFile(timeFile, []byte(str), 0644)
if err != nil {
panic("Failed to write start time file: " + err.Error())
}
} else {
data, err := os.ReadFile(timeFile)
if err != nil {
panic("Failed to read start time file: " + err.Error())
}
var t int64
_, err = fmt.Sscanf(string(data), "%d", &t)
if err != nil {
panic("Failed to parse start time: " + err.Error())
}
startTime = t
}
}
func getStatistic() (*Statistic, error) {
totalResources, err := dao.CountResources()
if err != nil {
return nil, err
}
totalFiles, err := dao.CountFiles()
if err != nil {
return nil, err
}
return &Statistic{
TotalResources: totalResources,
TotalFiles: totalFiles,
StartTime: startTime,
}, nil
}
func GetStatistic() (*Statistic, error) {
return cache.Get(getStatistic)
}