add: assets stats
This commit is contained in:
parent
bdc4888a0c
commit
dd19668b3b
|
|
@ -2,4 +2,5 @@
|
|||
.git
|
||||
bindata.go
|
||||
*.exe
|
||||
gin.log
|
||||
config/config.json
|
||||
|
|
@ -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"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue