beancount-gs/service/stats.go

32 lines
744 B
Go
Raw Normal View History

2021-11-19 09:54:02 +00:00
package service
import (
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
"os/exec"
2021-11-21 14:37:13 +00:00
"strings"
2021-11-19 09:54:02 +00:00
)
func MonthsList(c *gin.Context) {
2021-11-21 14:37:13 +00:00
months := make([]string, 0)
2021-11-19 09:54:02 +00:00
2021-11-21 14:37:13 +00:00
ledgerConfig := script.GetLedgerConfigFromContext(c)
2021-11-19 09:54:02 +00:00
beanFilePath := ledgerConfig.DataPath + "/index.bean"
bql := "SELECT distinct year(date), month(date)"
2021-11-21 14:37:13 +00:00
cmd := exec.Command("bean-query", beanFilePath, bql)
output, err := cmd.Output()
2021-11-19 09:54:02 +00:00
if err != nil {
InternalError(c, "Failed to exec bql")
return
}
2021-11-21 14:37:13 +00:00
execResult := string(output)
months = make([]string, 0)
for _, line := range strings.Split(execResult, "\n")[2:] {
if line != "" {
yearMonth := strings.Fields(line)
months = append(months, yearMonth[0]+"-"+yearMonth[1])
}
}
OK(c, months)
2021-11-19 09:54:02 +00:00
}