From dd19668b3b4375081f4881c8d408ab1bee6c1d93 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Thu, 2 Dec 2021 22:48:45 +0800 Subject: [PATCH] add: assets stats --- .gitignore | 1 + script/bql.go | 7 +++++++ server.go | 9 +++++++++ service/stats.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/.gitignore b/.gitignore index 3be03f7..55157d5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .git bindata.go *.exe +gin.log config/config.json \ No newline at end of file diff --git a/script/bql.go b/script/bql.go index 785cca3..3c75a7c 100644 --- a/script/bql.go +++ b/script/bql.go @@ -11,6 +11,9 @@ import ( ) type QueryParams struct { + From bool `bql:"From"` + FromYear int `bql:"year ="` + FromMonth int `bql:"month ="` Where bool `bql:"where"` Currency string `bql:"currency ="` Year int `bql:"year ="` @@ -159,6 +162,10 @@ func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryPa break case reflect.Bool: val := valueField.Bool() + // where 前的 from 可能会带有 and + if typeField.Name == "Where" { + bql = strings.Trim(bql, " AND") + } if val { bql = fmt.Sprintf("%s %s ", bql, typeField.Tag.Get("bql")) } diff --git a/server.go b/server.go index 4cc099f..d190e02 100644 --- a/server.go +++ b/server.go @@ -4,7 +4,9 @@ import ( "github.com/beancount-gs/script" "github.com/beancount-gs/service" "github.com/gin-gonic/gin" + "io" "net/http" + "os" ) func InitServerFiles() error { @@ -66,6 +68,7 @@ func RegisterRouter(router *gin.Engine) { authorized.GET("/stats/payee", service.StatsPayee) authorized.GET("/stats/account/percent", service.StatsAccountPercent) authorized.GET("/stats/account/trend", service.StatsAccountTrend) + authorized.GET("/stats/account/balance", service.StatsAccountBalance) authorized.GET("/stats/month/total", service.StatsMonthTotal) authorized.GET("/transaction", service.QueryTransactions) authorized.POST("/transaction", service.AddTransactions) @@ -103,11 +106,17 @@ func main() { return } } + // gin 日志设置 + gin.DisableConsoleColor() + fs, _ := os.Create("gin.log") + gin.DefaultWriter = io.MultiWriter(fs) router := gin.Default() // 注册路由 RegisterRouter(router) // 启动服务 var port = ":3001" + url := "http://localhost" + port + script.LogSystemInfo("Server start at " + url) err = router.Run(port) if err != nil { script.LogSystemError("Failed to start server, " + err.Error()) diff --git a/service/stats.go b/service/stats.go index e4b32b6..3b6a59a 100644 --- a/service/stats.go +++ b/service/stats.go @@ -161,6 +161,55 @@ func StatsAccountTrend(c *gin.Context) { OK(c, result) } +type AccountBalanceBQLResult struct { + Year string `bql:"year" json:"year"` + Month string `bql:"month" json:"month"` + Day string `bql:"day" json:"day"` + Balance string `bql:"balance" json:"balance"` +} + +type AccountBalanceResult struct { + Date string `json:"date"` + Amount json.Number `json:"amount"` + OperatingCurrency string `json:"operatingCurrency"` +} + +func StatsAccountBalance(c *gin.Context) { + ledgerConfig := script.GetLedgerConfigFromContext(c) + var statsQuery StatsQuery + if err := c.ShouldBindQuery(&statsQuery); err != nil { + BadRequest(c, err.Error()) + return + } + + queryParams := script.QueryParams{ + AccountLike: statsQuery.Prefix, + Where: true, + } + + balResultList := make([]AccountBalanceBQLResult, 0) + bql := fmt.Sprintf("select '\\', year, '\\', month, '\\', day, '\\', last(convert(balance, '%s')), '\\'", ledgerConfig.OperatingCurrency) + err := script.BQLQueryListByCustomSelect(ledgerConfig, bql, &queryParams, &balResultList) + if err != nil { + InternalError(c, err.Error()) + return + } + + resultList := make([]AccountBalanceResult, 0) + for _, bqlResult := range balResultList { + if bqlResult.Balance != "" { + fields := strings.Fields(bqlResult.Balance) + amount, _ := decimal.NewFromString(fields[0]) + resultList = append(resultList, AccountBalanceResult{ + Date: bqlResult.Year + "-" + bqlResult.Month + "-" + bqlResult.Day, + Amount: json.Number(amount.Round(2).String()), + OperatingCurrency: fields[1], + }) + } + } + OK(c, resultList) +} + type MonthTotalBQLResult struct { Year int Month int