Last active
August 28, 2018 17:43
-
-
Save simonmichael/5b0d3aafb4b71ccdadaa4fd02beac475 to your computer and use it in GitHub Desktop.
ledger-analytics + hledger, https://news.ycombinator.com/item?id=17845565
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ledger-analytics$ git diff analyze.js | |
diff --git a/analyze.js b/analyze.js | |
index 09a98dd..34eefcd 100644 | |
--- a/analyze.js | |
+++ b/analyze.js | |
@@ -2,7 +2,7 @@ const util = require('util') | |
const exec = util.promisify(require('child_process').exec) | |
module.exports.getCommodities = async (file) => { | |
- const { stdout } = await exec('ledger -f ' + file + ' commodities') | |
+ const { stdout } = await exec('hledger -f ' + file + ' commodities') | |
const commodities = stdout.split('\n').filter(x => x.length > 0) | |
return commodities | |
} | |
@@ -11,7 +11,7 @@ module.exports.getCommodities = async (file) => { | |
module.exports.getTimelineData = async (file, query, commodity = undefined, byMonth = false) => { | |
const commodityArg = (commodity !== undefined) ? ` -X ` + commodity : `` | |
const byLengthArgs = byMonth ? '-M' : '-D' | |
- const { stdout } = await exec(`ledger -f ` + file + commodityArg + ` -j reg ` + query + ` ` + byLengthArgs + ` --collapse --plot-total-format="%(format_date(date, "%Y-%m-%d")) %(abs(quantity(scrub(display_total))))\n"`) | |
+ const { stdout } = await exec(`hledger -f ` + file + commodityArg + ` reg ` + query + ` ` + byLengthArgs + ` -Ocsv | cut -d, -f2,6 | sed -e 's/"//g' -e 's/,\$/ /' `) | |
// Convert datetime format from: | |
/* | |
2018-08-17 -63.26 | |
@@ -47,7 +47,7 @@ module.exports.getTimelineData = async (file, query, commodity = undefined, byMo | |
module.exports.getGrowth = async (file, query, commodity = undefined) => { | |
const commodityArg = (commodity !== undefined) ? ` -X ` + commodity : `` | |
- const { stdout } = await exec(`ledger -f ` + file + ` ` + commodityArg + ` -J reg ` + query + ` -M --collapse`) | |
+ const { stdout } = await exec(`hledger -f ` + file + ` ` + commodityArg + ` reg ` + query + ` -M -Ocsv | cut -d, -f2,7 | sed -e 's/"//g' -e 's/,\$/ /' `) | |
const growth = stdout | |
.split('\n') | |
.filter(x => x.length > 0) | |
@@ -65,7 +65,7 @@ module.exports.getGrowth = async (file, query, commodity = undefined) => { | |
module.exports.getAccounts = async (file, account) => { | |
const accountArg = account === undefined ? '' : account | |
- const { stdout } = await exec(`ledger -f ` + file + ' accounts ' + accountArg) | |
+ const { stdout } = await exec(`hledger -f ` + file + ' accounts ' + accountArg) | |
const accounts = stdout.split('\n').filter(x => x.length > 0) | |
return accounts | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- stack runghc --verbosity info | |
--package hledger-lib | |
--package hledger | |
--package here | |
--package text | |
-} | |
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Monad | |
import Data.List | |
import qualified Data.Map as M | |
import Data.String.Here | |
import qualified Data.Text.IO as T | |
import Hledger | |
import Hledger.Cli | |
cmdmode = hledgerCommandMode | |
[here| commodities | |
List the commodities used in the journal. | |
FLAGS | |
|] | |
[] | |
[generalflagsgroup1] | |
[] | |
([], Nothing) | |
main :: IO () | |
main = do | |
copts <- getHledgerCliOpts cmdmode | |
withJournalDo copts $ \_ j -> do | |
let cs = filter (/= "AUTO") $ | |
nub $ sort $ M.keys (jcommodities j) ++ M.keys (jinferredcommodities j) | |
forM_ cs T.putStrLn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment