add: assets stats
This commit is contained in:
parent
bdc4888a0c
commit
dd19668b3b
|
|
@ -2,4 +2,5 @@
|
||||||
.git
|
.git
|
||||||
bindata.go
|
bindata.go
|
||||||
*.exe
|
*.exe
|
||||||
|
gin.log
|
||||||
config/config.json
|
config/config.json
|
||||||
|
|
@ -11,6 +11,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type QueryParams struct {
|
type QueryParams struct {
|
||||||
|
From bool `bql:"From"`
|
||||||
|
FromYear int `bql:"year ="`
|
||||||
|
FromMonth int `bql:"month ="`
|
||||||
Where bool `bql:"where"`
|
Where bool `bql:"where"`
|
||||||
Currency string `bql:"currency ="`
|
Currency string `bql:"currency ="`
|
||||||
Year int `bql:"year ="`
|
Year int `bql:"year ="`
|
||||||
|
|
@ -159,6 +162,10 @@ func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryPa
|
||||||
break
|
break
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
val := valueField.Bool()
|
val := valueField.Bool()
|
||||||
|
// where 前的 from 可能会带有 and
|
||||||
|
if typeField.Name == "Where" {
|
||||||
|
bql = strings.Trim(bql, " AND")
|
||||||
|
}
|
||||||
if val {
|
if val {
|
||||||
bql = fmt.Sprintf("%s %s ", bql, typeField.Tag.Get("bql"))
|
bql = fmt.Sprintf("%s %s ", bql, typeField.Tag.Get("bql"))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"github.com/beancount-gs/script"
|
"github.com/beancount-gs/script"
|
||||||
"github.com/beancount-gs/service"
|
"github.com/beancount-gs/service"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitServerFiles() error {
|
func InitServerFiles() error {
|
||||||
|
|
@ -66,6 +68,7 @@ func RegisterRouter(router *gin.Engine) {
|
||||||
authorized.GET("/stats/payee", service.StatsPayee)
|
authorized.GET("/stats/payee", service.StatsPayee)
|
||||||
authorized.GET("/stats/account/percent", service.StatsAccountPercent)
|
authorized.GET("/stats/account/percent", service.StatsAccountPercent)
|
||||||
authorized.GET("/stats/account/trend", service.StatsAccountTrend)
|
authorized.GET("/stats/account/trend", service.StatsAccountTrend)
|
||||||
|
authorized.GET("/stats/account/balance", service.StatsAccountBalance)
|
||||||
authorized.GET("/stats/month/total", service.StatsMonthTotal)
|
authorized.GET("/stats/month/total", service.StatsMonthTotal)
|
||||||
authorized.GET("/transaction", service.QueryTransactions)
|
authorized.GET("/transaction", service.QueryTransactions)
|
||||||
authorized.POST("/transaction", service.AddTransactions)
|
authorized.POST("/transaction", service.AddTransactions)
|
||||||
|
|
@ -103,11 +106,17 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// gin 日志设置
|
||||||
|
gin.DisableConsoleColor()
|
||||||
|
fs, _ := os.Create("gin.log")
|
||||||
|
gin.DefaultWriter = io.MultiWriter(fs)
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
// 注册路由
|
// 注册路由
|
||||||
RegisterRouter(router)
|
RegisterRouter(router)
|
||||||
// 启动服务
|
// 启动服务
|
||||||
var port = ":3001"
|
var port = ":3001"
|
||||||
|
url := "http://localhost" + port
|
||||||
|
script.LogSystemInfo("Server start at " + url)
|
||||||
err = router.Run(port)
|
err = router.Run(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
script.LogSystemError("Failed to start server, " + err.Error())
|
script.LogSystemError("Failed to start server, " + err.Error())
|
||||||
|
|
|
||||||
|
|
@ -161,6 +161,55 @@ func StatsAccountTrend(c *gin.Context) {
|
||||||
OK(c, result)
|
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 {
|
type MonthTotalBQLResult struct {
|
||||||
Year int
|
Year int
|
||||||
Month int
|
Month int
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue