-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreport
More file actions
executable file
·131 lines (111 loc) · 5.02 KB
/
report
File metadata and controls
executable file
·131 lines (111 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
set -ex
## Generate ledger-cli reports
# Requires at least ledger 3
# Other prerequisites
# gnuplot and the gnuplot scripts from this repo
# piechart (https://github.com/cbdevnet/piechart)
# A matching ledgerrc (see this repo)
# Run as ./report <ledger file> [periods <period expressions>]
# Resulting graphs will be placed in the reports/ directory
# The script uses the following categories
# Assets
# Assets:Liquid
# Expenses
# Expenses:Food
# Liabilities
# Income
# The periodic feature is experimental and might not work exactly as expected
# Reporting periods must be exactly equal-width buckets, meaning
# that the query result must have the exact same number of lines.
# This also includes things such as dates without a transaction (a problem
# which may be sidestepped by using --empty) and leap years,
# making --daily probably a bad window. --weekly --empty is probably a good
# minimum interval.
# The periodic report will create the same type of graphs from
# the same queries as the regular report, but will try to
# plot the data for every period as its own line.
# Data which can not reliably be segmented into equal-width
# periods should be plotted with the _aperiodic functions,
# which always produce the same plot.
# Assume ledger file if none given
export LEDGER_FILE=${1:-"main.ledger"}
export MODE=${2:-"complete"}
# Get current date and ledger command
DATE=$(date +%Y%m%d)
FILEPREFIX="$DATE"
LEDGER="ledger --init-file ./ledgerrc"
# Store all reporting periods when requested
if [ "$MODE" == "periods" ]; then
DATADIR=$(mktemp -d)
FILEPREFIX="$FILEPREFIX-Periodic"
PERIODS=()
while [ -n "$3" ]; do
PERIODS+=($3)
shift
done
printf "Report range: %s\n" "${PERIODS[@]}"
fi
plot_stdin(){
# The gnuplot scripts accept the following parameters
# SVGTITLE Title of the SVG document
# (and, in periodic reports, the data filename)
# TITLE Title of the image
# LEGEND Chart legend text
printf "%s" "$1" | xargs $LEDGER | gnuplot script.gnuplot -e "plot '<cat' using 1:2 with $2 title ''.LEGEND"
}
plot_periodic(){
printf "plot " > "$DATADIR/$SVGTITLE.gnuplot"
touch "$DATADIR/$SVGTITLE.data.in"
touch "$DATADIR/$SVGTITLE.data"
LINE=2
for period in "${PERIODS[@]}"; do
LINEPREFIX=",\\"
if [ "$LINE" == "2" ]; then
LINEPREFIX="\\"
fi
printf -- "--period %s %s" "$period" "$1" | xargs $LEDGER | paste "$DATADIR/$SVGTITLE.data.in" - > "$DATADIR/$SVGTITLE.data"
printf '%s\n"%s" using 1:%s title "%s" with %s' "$LINEPREFIX" "$DATADIR/$SVGTITLE.data" "$LINE" "$LEGEND [$period]" "$2" >> "$DATADIR/$SVGTITLE.gnuplot"
LINE=$(($LINE + 2))
cp "$DATADIR/$SVGTITLE.data" "$DATADIR/$SVGTITLE.data.in"
done
gnuplot script.gnuplot "$DATADIR/$SVGTITLE.gnuplot"
}
# Can only create periodic graphs from periodic data
line_aperiodic(){
plot_stdin "$1" 'linespoints'
}
line(){
if [ "$MODE" == "periods" ]; then
plot_periodic "$1" "linespoints"
else
plot_stdin "$1" 'linespoints'
fi
}
bar(){
if [ "$MODE" == "periods" ]; then
plot_periodic "$1" "boxes"
else
plot_stdin "$1" 'boxes'
fi
}
SVGTITLE="WeeklyAssets" TITLE="Assets, weekly" LEGEND="Assets" \
line "--total-data --weekly --collapse --empty --market register ^Assets" > reports/$FILEPREFIX-Assets-Weekly.svg
SVGTITLE="Assets" TITLE="Assets, all TX" LEGEND="Assets" \
line_aperiodic "--total-data --collapse --market register ^Assets" > reports/$FILEPREFIX-Assets.svg
SVGTITLE="Equity" TITLE="Equity, weekly" LEGEND="Liquid Assets vs Liabilities" \
line "--total-data --collapse --empty --weekly register ^Assets:Liquid ^Liabilities" > reports/$FILEPREFIX-Equity.svg
SVGTITLE="Balance" TITLE="Monthly Balance" LEGEND="Expenses (Positive implies overspending)" \
bar "--amount-data --collapse --empty --monthly register ^Income ^Expenses" > reports/$FILEPREFIX-Balance-Monthly.svg
SVGTITLE="Food" TITLE="Food Expenses" LEGEND="Expenses:Food" \
bar "--amount-data --collapse --monthly --empty register ^Expenses:Food" > reports/$FILEPREFIX-Food-Monthly.svg
SVGTITLE="Expenses" TITLE="Monthly Expenses" LEGEND="Expenses" \
line "--amount-data --collapse --monthly --empty register ^Expenses" > reports/$FILEPREFIX-Expenses-Monthly.svg
SVGTITLE="Income" TITLE="Monthly Income" LEGEND="Income" \
line "--amount-data --collapse --monthly --empty register ^Income" > reports/$FILEPREFIX-Income-Monthly.svg
$LEDGER --depth 2 --balance-format "%-(to_int(display_total))|%-(partial_account)\n" --sort amount --no-total balance ^Expenses | tail -n +2 | piechart --delimiter "|" --order value,legend --color random > reports/$FILEPREFIX-Expense-Pie.svg
$LEDGER --depth 2 --balance-format "%-(to_int(display_total))|%-(partial_account)\n" --sort amount --market --no-total balance ^Assets | tail -n +2 | piechart --delimiter "|" --order value,legend --color random > reports/$FILEPREFIX-AssetsOverview-Pie.svg
$LEDGER --related --balance-format "%-(to_int(display_total))|%-(partial_account)\n" --sort amount --no-total balance ^Income | tail -n +2 | piechart --delimiter "|" --order value,legend --color random > reports/$FILEPREFIX-IncomeTargets-Pie.svg
if [ -n "$DATADIR" ]; then
rm -rf "$DATADIR"
fi