beancount-gs/service/stats.go

56 lines
1.4 KiB
Go
Raw Normal View History

2021-11-19 09:54:02 +00:00
package service
import (
2021-11-22 14:50:10 +00:00
"fmt"
2021-11-19 09:54:02 +00:00
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
2021-11-22 14:50:10 +00:00
"strings"
2021-11-19 09:54:02 +00:00
)
2021-11-22 08:47:49 +00:00
type YearMonth struct {
Year string `bql:"distinct year(date)" json:"year"`
Month string `bql:"month(date)" json:"month"`
}
2021-11-19 09:54:02 +00:00
2021-11-22 08:47:49 +00:00
func MonthsList(c *gin.Context) {
2021-11-21 14:37:13 +00:00
ledgerConfig := script.GetLedgerConfigFromContext(c)
2021-11-22 08:47:49 +00:00
yearMonthList := make([]YearMonth, 0)
err := script.BQLQueryList(ledgerConfig, nil, &yearMonthList)
2021-11-19 09:54:02 +00:00
if err != nil {
2021-11-22 08:47:49 +00:00
InternalError(c, err.Error())
2021-11-19 09:54:02 +00:00
return
}
2021-11-22 08:47:49 +00:00
months := make([]string, 0)
for _, yearMonth := range yearMonthList {
months = append(months, yearMonth.Year+"-"+yearMonth.Month)
2021-11-21 14:37:13 +00:00
}
OK(c, months)
2021-11-19 09:54:02 +00:00
}
2021-11-22 14:50:10 +00:00
type statsAccountTypeTotal struct {
AccountType string
Amount string
}
func StatsTotal(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
queryParams := script.GetQueryParams(c)
selectBql := fmt.Sprintf("SELECT '\\', root(account, 1), '\\', sum(convert(value(position), '%s')), '\\'", ledgerConfig.OperatingCurrency)
accountTypeTotalList := make([]statsAccountTypeTotal, 0)
err := script.BQLQueryListByCustomSelect(ledgerConfig, selectBql, &queryParams, &accountTypeTotalList)
if err != nil {
InternalError(c, err.Error())
return
}
result := make(map[string]string, 0)
for _, total := range accountTypeTotalList {
fields := strings.Fields(total.Amount)
if len(fields) > 1 {
result[total.AccountType] = fields[0]
}
}
OK(c, result)
}