Deprecate the use of bean-report
Signed-off-by: JmPotato <ghzpotato@gmail.com>
This commit is contained in:
parent
e505212731
commit
0a5f6f69c6
|
|
@ -104,13 +104,31 @@ func BQLQueryListByCustomSelect(ledgerConfig *Config, selectBql string, queryPar
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func BeanReportAllPrices(ledgerConfig *Config) string {
|
func BeanReportAllPrices(ledgerConfig *Config) []CommodityPrice {
|
||||||
beanFilePath := GetLedgerPriceFilePath(ledgerConfig.DataPath)
|
beanFilePath := GetLedgerPriceFilePath(ledgerConfig.DataPath)
|
||||||
|
var (
|
||||||
LogInfo(ledgerConfig.Mail, "bean-report "+beanFilePath+" all_prices")
|
command string
|
||||||
cmd := exec.Command("bean-report", beanFilePath, "all_prices")
|
useBeanReport = checkCommandExists("bean-report")
|
||||||
|
)
|
||||||
|
// `bean-report` had been deprecated since https://github.com/beancount/beancount/commit/a7c4f14f083de63e8d4e5a8d3664209daf95e1ec,
|
||||||
|
// we use `bean-query` instead. Here we add a check to use `bean-report` if `bean-query` is not installed for better compatibility.
|
||||||
|
if useBeanReport {
|
||||||
|
command = fmt.Sprintf("bean-report %s all_prices", beanFilePath)
|
||||||
|
} else {
|
||||||
|
// 'price' column works as a column placeholder to be consistent with the output of `bean-report`.
|
||||||
|
command = fmt.Sprintf(`bean-query %s "SELECT date, 'price', currency, price FROM account ~ 'Assets' WHERE price is not NULL"`, beanFilePath)
|
||||||
|
}
|
||||||
|
LogInfo(ledgerConfig.Mail, command)
|
||||||
|
cmd := exec.Command(command)
|
||||||
output, _ := cmd.Output()
|
output, _ := cmd.Output()
|
||||||
return string(output)
|
outputStr := string(output)
|
||||||
|
lines := strings.Split(outputStr, "\n")
|
||||||
|
LogInfo(ledgerConfig.Mail, outputStr)
|
||||||
|
// Remove the first two lines of the output since they are the header and separator with BQL output.
|
||||||
|
if !useBeanReport && len(lines) > 2 {
|
||||||
|
lines = lines[2:]
|
||||||
|
}
|
||||||
|
return newCommodityPriceListFromString(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryParams, queryResultPtr interface{}) (string, error) {
|
func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryParams, queryResultPtr interface{}) (string, error) {
|
||||||
|
|
|
||||||
|
|
@ -484,11 +484,8 @@ type CommodityPrice struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func RefreshLedgerCurrency(ledgerConfig *Config) []LedgerCurrency {
|
func newCommodityPriceListFromString(lines []string) []CommodityPrice {
|
||||||
// 查询货币获取当前汇率
|
commodityPriceList := make([]CommodityPrice, 0, len(lines))
|
||||||
output := BeanReportAllPrices(ledgerConfig)
|
|
||||||
statsPricesResultList := make([]CommodityPrice, 0)
|
|
||||||
lines := strings.Split(output, "\n")
|
|
||||||
// foreach lines
|
// foreach lines
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if strings.Trim(line, " ") == "" {
|
if strings.Trim(line, " ") == "" {
|
||||||
|
|
@ -496,14 +493,19 @@ func RefreshLedgerCurrency(ledgerConfig *Config) []LedgerCurrency {
|
||||||
}
|
}
|
||||||
// split line by " "
|
// split line by " "
|
||||||
words := strings.Fields(line)
|
words := strings.Fields(line)
|
||||||
statsPricesResultList = append(statsPricesResultList, CommodityPrice{
|
commodityPriceList = append(commodityPriceList, CommodityPrice{
|
||||||
Date: words[0],
|
Date: words[0],
|
||||||
Commodity: words[2],
|
Commodity: words[2],
|
||||||
Value: words[3],
|
Value: words[3],
|
||||||
Currency: words[4],
|
Currency: words[4],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
return commodityPriceList
|
||||||
|
}
|
||||||
|
|
||||||
|
func RefreshLedgerCurrency(ledgerConfig *Config) []LedgerCurrency {
|
||||||
|
// 查询货币获取当前汇率
|
||||||
|
statsPricesResultList := BeanReportAllPrices(ledgerConfig)
|
||||||
// statsPricesResultList 转为 map
|
// statsPricesResultList 转为 map
|
||||||
existCurrencyMap := make(map[string]CommodityPrice)
|
existCurrencyMap := make(map[string]CommodityPrice)
|
||||||
for _, statsPricesResult := range statsPricesResultList {
|
for _, statsPricesResult := range statsPricesResultList {
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,16 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func checkCommandExists(command string) bool {
|
||||||
|
cmd := exec.Command(command, "--version")
|
||||||
|
_, err := cmd.Output()
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetIpAddress() string {
|
func GetIpAddress() string {
|
||||||
addrs, _ := net.InterfaceAddrs()
|
addrs, _ := net.InterfaceAddrs()
|
||||||
for _, value := range addrs {
|
for _, value := range addrs {
|
||||||
|
|
|
||||||
|
|
@ -480,33 +480,6 @@ func StatsPayee(c *gin.Context) {
|
||||||
OK(c, result)
|
OK(c, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
type StatsPricesResult struct {
|
|
||||||
Date string `json:"date"`
|
|
||||||
Commodity string `json:"commodity"`
|
|
||||||
Currency string `json:"operatingCurrency"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func StatsCommodityPrice(c *gin.Context) {
|
func StatsCommodityPrice(c *gin.Context) {
|
||||||
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
OK(c, script.BeanReportAllPrices(script.GetLedgerConfigFromContext(c)))
|
||||||
output := script.BeanReportAllPrices(ledgerConfig)
|
|
||||||
script.LogInfo(ledgerConfig.Mail, output)
|
|
||||||
|
|
||||||
statsPricesResultList := make([]StatsPricesResult, 0)
|
|
||||||
lines := strings.Split(output, "\n")
|
|
||||||
// foreach lines
|
|
||||||
for _, line := range lines {
|
|
||||||
if strings.Trim(line, " ") == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// split line by " "
|
|
||||||
words := strings.Fields(line)
|
|
||||||
statsPricesResultList = append(statsPricesResultList, StatsPricesResult{
|
|
||||||
Date: words[0],
|
|
||||||
Commodity: words[2],
|
|
||||||
Value: words[3],
|
|
||||||
Currency: words[4],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
OK(c, statsPricesResultList)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue