Skip to content

Instantly share code, notes, and snippets.

@ArtBIT
Last active May 14, 2019 13:33
Show Gist options
  • Save ArtBIT/b280081cdc3fac3496464d4346525767 to your computer and use it in GitHub Desktop.
Save ArtBIT/b280081cdc3fac3496464d4346525767 to your computer and use it in GitHub Desktop.
ledger-analytics collapse through awk
diff --git i/ledger-api.js w/ledger-api.js
index 963cc90..be07899 100644
--- i/ledger-api.js
+++ w/ledger-api.js
@@ -20,6 +20,9 @@ const cleanInput = (str) => {
return padSpace(str.trim())
}
+const sumByDate = "awk -F' ' '{a[$1]+=$2}END{for(x in a)print x\" \"a[x]}'";
+const pipe = (...commands) => commands.join(" | ");
+
class LedgerApi {
constructor () {
this._file = ``
@@ -91,16 +94,15 @@ class LedgerApi {
]
*/
- const { stdout } = await exec(`ledger reg -f` +
+ const { stdout } = await exec(pipe(`ledger reg -f` +
this._file +
this._query +
this._commodity +
` -j` +
this._period +
- this._extraArgs +
- ` --collapse` +
- ` --plot-total-format="%(format_date(date, "%Y-%m-%d")) %(abs(quantity(scrub(display_total))))"`.split('\n').join('')
- )
+ this._extraArgs
+ .split('\n').join('')
+ , sumByDate, 'sort'));
return stdout
.split('\n')
@@ -118,19 +120,20 @@ class LedgerApi {
};
async getCommodities () {
- const { stdout } = await exec('ledger commodities -f' + this._file + this._extraArgs)
+ const { stdout } = await exec(`ledger commodities -f` + this._file + this._extraArgs)
const commodities = stdout.split('\n').filter(x => x.length > 0)
return { commodities }
};
async getGrowth () {
- const { stdout } = await exec(`ledger reg -f` +
+ const { stdout } = await exec(pipe(`ledger reg -f` +
this._file +
this._query +
this._commodity +
` -J -M` +
- this._extraArgs +
- ` --collapse`.split('\n').join('')
+ this._extraArgs
+ .split('\n').join('')
+ , sumByDate, 'sort')
)
const growth = stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment