all repos

clerk @ cf9c1d9

missing tooling for ledger/hledger

clerk/internal/analyzer/build.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
analyzer: build payee templates lazily, 1 month ago
1
package analyzer
2
3
import (
4
	"sort"
5
	"strings"
6
7
	"olexsmir.xyz/clerk/journal"
8
	"olexsmir.xyz/clerk/journal/ast"
9
)
10
11
// Build constructs [Analysis] from a flat resolved journal view.
12
func Build(rj *journal.ResolvedJournal) *Analysis {
13
	fileIndex := make(map[*journal.ParsedFile]int)
14
	for i, pf := range rj.Occurrences {
15
		fileIndex[pf] = i
16
	}
17
18
	a := &Analysis{
19
		Files:                   rj.Occurrences,
20
		Accounts:                make(map[string]*AccountInfo),
21
		AccountAliases:          make(map[string]string),
22
		Commodities:             make(map[string]*CommodityInfo),
23
		Payees:                  make(map[string]*PayeeInfo),
24
		Tags:                    make(map[string]*TagInfo),
25
		AccountsByPrefix:        make(map[string][]string),
26
		TransactionsByKey:       make(map[string][]*ast.Transaction),
27
		TransactionsCountByDate: make(map[string]int),
28
	}
29
	for _, item := range rj.Items {
30
		if item.IsInclude {
31
			continue
32
		}
33
		idx := fileIndex[item.Occurrence]
34
		a.addEntry(idx, item.Occurrence.Ast.Entries[item.EntryIndex])
35
	}
36
	a.buildPrefixIndex()
37
	a.sortAccountNames()
38
	a.collectPayeeNames()
39
	a.collectDates()
40
	a.collectTags()
41
	return a
42
}
43
44
func TxDuplicateKey(tx *ast.Transaction) string {
45
	var b strings.Builder
46
	b.WriteString(tx.Date.String())
47
	b.WriteByte('|')
48
	if tx.Payee != nil {
49
		b.WriteString(tx.Payee.Name)
50
	}
51
	b.WriteByte('|')
52
	for _, p := range tx.Postings {
53
		b.WriteString(p.Account.String())
54
		b.WriteByte(',')
55
	}
56
	return b.String()
57
}
58
59
// PayeeTemplates returns  the last Transactions's postings per payee name.
60
func (a *Analysis) PayeeTemplates() map[string][]PostingTemplate {
61
	templates := make(map[string][]PostingTemplate)
62
	for _, tx := range a.Transactions {
63
		payee := ""
64
		if tx.Payee != nil {
65
			payee = tx.Payee.Name
66
		}
67
		if payee == "" {
68
			continue
69
		}
70
71
		t := make([]PostingTemplate, len(tx.Postings))
72
		for i, p := range tx.Postings {
73
			t[i] = PostingTemplate{
74
				Account:    p.Account.String(),
75
				IsInferred: p.Amount == nil,
76
			}
77
			if p.Amount != nil {
78
				t[i].Amount = p.Amount.Quantity.String()
79
				t[i].Commodity = p.Amount.Commodity
80
			}
81
		}
82
		templates[payee] = t
83
	}
84
	return templates
85
}
86
87
func (a *Analysis) addEntry(fileIndex int, entry ast.Entry) {
88
	switch e := entry.(type) {
89
	case *ast.AccountDirective:
90
		a.addAccountDirective(e)
91
	case *ast.CommodityDirective:
92
		a.addCommodityDirective(e)
93
	case *ast.AliasDirective:
94
		a.addAliasDirective(e)
95
	case *ast.PayeeDirective:
96
		a.addPayeeDirective(e)
97
	case *ast.TagDirective:
98
		a.addTagDirective(e)
99
	case *ast.Comment:
100
		a.addCommentTags(fileIndex, nil, e)
101
	case *ast.Transaction:
102
		a.Transactions = append(a.Transactions, e)
103
		a.addPostings(fileIndex, e.Postings, &e.Date)
104
		a.addPayee(fileIndex, e.Payee)
105
		a.addCommentTags(fileIndex, &e.Date, e.Comment)
106
		for _, c := range e.HeaderComments {
107
			a.addCommentTags(fileIndex, &e.Date, c)
108
		}
109
		key := TxDuplicateKey(e)
110
		a.TransactionsByKey[key] = append(a.TransactionsByKey[key], e)
111
		a.TransactionsCountByDate[e.Date.String()]++
112
	case *ast.PeriodicTransaction:
113
		a.PeriodicTransactions = append(a.PeriodicTransactions, e)
114
		a.addPostings(fileIndex, e.Postings, nil)
115
		a.addCommentTags(fileIndex, nil, e.Comment)
116
		for _, c := range e.HeaderComments {
117
			a.addCommentTags(fileIndex, nil, c)
118
		}
119
	case *ast.AutomatedTransaction:
120
		a.AutomatedTransactions = append(a.AutomatedTransactions, e)
121
		a.addPostings(fileIndex, e.Postings, nil)
122
		a.addCommentTags(fileIndex, nil, e.Comment)
123
		for _, c := range e.HeaderComments {
124
			a.addCommentTags(fileIndex, nil, c)
125
		}
126
	}
127
128
	// Every directive-like entry goes into Directives.
129
	switch entry.(type) {
130
	case *ast.AccountDirective, *ast.CommodityDirective, *ast.PayeeDirective, *ast.TagDirective, *ast.IncludeDirective,
131
		*ast.AliasDirective, *ast.YearDirective, *ast.DecimalMarkDirective, *ast.DefaultCommodityDirective, *ast.MarketPriceDirective,
132
		*ast.ConversionDirective, *ast.ApplyDirective, *ast.EndDirective, *ast.CommentBlockDirective, *ast.IgnoredDirective:
133
		a.Directives = append(a.Directives, entry)
134
	}
135
}
136
137
func (a *Analysis) addAccountDirective(ad *ast.AccountDirective) {
138
	aname := ad.Account.String()
139
	info, ok := a.Accounts[aname]
140
	if !ok {
141
		info = &AccountInfo{}
142
		a.Accounts[aname] = info
143
	}
144
	info.Directives = append(info.Directives, ad)
145
	for _, sd := range ad.Subdirectives {
146
		if sd.Kind == ast.SubdirectiveAlias {
147
			a.AccountAliases[sd.Value] = aname
148
		}
149
	}
150
}
151
152
// addAliasDirective records a top-level "alias A = B" directive; the alias
153
// source name resolves to the target account.
154
func (a *Analysis) addAliasDirective(ad *ast.AliasDirective) {
155
	a.AccountAliases[ad.From.String()] = ad.To.String()
156
	a.AliasDirectives = append(a.AliasDirectives, ad)
157
}
158
159
func (a *Analysis) addPayeeDirective(pd *ast.PayeeDirective) {
160
	if pd.Name == nil {
161
		return
162
	}
163
	info, ok := a.Payees[pd.Name.Name]
164
	if !ok {
165
		info = &PayeeInfo{}
166
		a.Payees[pd.Name.Name] = info
167
	}
168
	info.Directives = append(info.Directives, pd)
169
}
170
171
func (a *Analysis) addTagDirective(td *ast.TagDirective) {
172
	if td.Name == "" {
173
		return
174
	}
175
	info, ok := a.Tags[td.Name]
176
	if !ok {
177
		info = &TagInfo{}
178
		a.Tags[td.Name] = info
179
	}
180
	info.Directives = append(info.Directives, td)
181
}
182
183
func (a *Analysis) addCommentTags(fileIndex int, date *ast.Date, c *ast.Comment) {
184
	if c == nil {
185
		return
186
	}
187
	for i := range c.Tags {
188
		t := &c.Tags[i]
189
		info, ok := a.Tags[t.Key]
190
		if !ok {
191
			info = &TagInfo{}
192
			a.Tags[t.Key] = info
193
		}
194
		info.Usage = append(info.Usage, TagUsage{FileIndex: fileIndex, Tag: t})
195
		info.UsedCount++
196
		if date != nil {
197
			info.LastUsed = maxDate(info.LastUsed, *date)
198
		}
199
	}
200
}
201
202
func (a *Analysis) addCommodityDirective(cd *ast.CommodityDirective) {
203
	info, ok := a.Commodities[cd.Commodity]
204
	if !ok {
205
		info = &CommodityInfo{}
206
		a.Commodities[cd.Commodity] = info
207
	}
208
	info.Directives = append(info.Directives, cd)
209
}
210
211
func (a *Analysis) addPayee(fileIndex int, payee *ast.Payee) {
212
	if payee == nil {
213
		return
214
	}
215
	info, ok := a.Payees[payee.Name]
216
	if !ok {
217
		info = &PayeeInfo{}
218
		a.Payees[payee.Name] = info
219
	}
220
	info.Usage = append(info.Usage, PayeeUsage{
221
		FileIndex: fileIndex,
222
		Payee:     payee,
223
	})
224
	info.UsedCount++
225
}
226
227
func (a *Analysis) addPostings(fileIndex int, postings []*ast.Posting, date *ast.Date) {
228
	for _, posting := range postings {
229
		aname := posting.Account.String()
230
		info, ok := a.Accounts[aname]
231
		if !ok {
232
			info = &AccountInfo{}
233
			a.Accounts[aname] = info
234
		}
235
		info.Usages = append(info.Usages, AccountUsage{
236
			FileIndex: fileIndex,
237
			Posting:   posting,
238
		})
239
		info.UsedCount++
240
		if date != nil {
241
			info.LastUsed = maxDate(info.LastUsed, *date)
242
		}
243
244
		a.addCommodityUsage(fileIndex, posting.Amount, date)
245
		if posting.Cost != nil {
246
			a.addCommodityUsage(fileIndex, &posting.Cost.Amount, date)
247
		}
248
		if posting.Balance != nil {
249
			a.addCommodityUsage(fileIndex, &posting.Balance.Amount, date)
250
			if posting.Balance.Cost != nil {
251
				a.addCommodityUsage(fileIndex, &posting.Balance.Cost.Amount, date)
252
			}
253
		}
254
255
		a.addCommentTags(fileIndex, date, posting.Comment)
256
		for i := range posting.Comments {
257
			a.addCommentTags(fileIndex, date, &posting.Comments[i])
258
		}
259
	}
260
}
261
262
func (a *Analysis) collectDates() {
263
	seen := make(map[string]bool)
264
	for _, tx := range a.Transactions {
265
		s := tx.Date.String()
266
		if s != "" && !seen[s] {
267
			seen[s] = true
268
			a.Dates = append(a.Dates, tx.Date)
269
		}
270
	}
271
	sort.Slice(a.Dates, func(i, j int) bool {
272
		return a.Dates[i].Compare(a.Dates[j]) < 0
273
	})
274
	a.DateStrings = make([]string, len(a.Dates))
275
	for i, d := range a.Dates {
276
		a.DateStrings[i] = d.String()
277
	}
278
}
279
280
func (a *Analysis) collectTags() {
281
	names := make([]string, 0, len(a.Tags))
282
	values := make(map[string]bool)
283
	for name, info := range a.Tags {
284
		names = append(names, name)
285
		seen := make(map[string]bool)
286
		for _, u := range info.Usage {
287
			if u.Tag.Value == "" {
288
				continue
289
			}
290
			seen[u.Tag.Value] = true
291
			values[u.Tag.Value] = true
292
		}
293
		if len(seen) > 0 {
294
			info.Values = sortedKeys(seen)
295
		}
296
	}
297
	sort.Strings(names)
298
	a.TagNames = names
299
	a.TagValues = sortedKeys(values)
300
}
301
302
func sortedKeys(m map[string]bool) []string {
303
	out := make([]string, 0, len(m))
304
	for k := range m {
305
		out = append(out, k)
306
	}
307
	sort.Strings(out)
308
	return out
309
}
310
311
// CountTransactionsOnDate returns the number of transactions on d.
312
func (a *Analysis) CountTransactionsOnDate(d ast.Date) int {
313
	return a.TransactionsCountByDate[d.String()]
314
}
315
316
func (a *Analysis) addCommodityUsage(fileIndex int, am *ast.Amount, date *ast.Date) {
317
	if am == nil || am.Commodity == "" {
318
		return
319
	}
320
	info, ok := a.Commodities[am.Commodity]
321
	if !ok {
322
		info = &CommodityInfo{}
323
		a.Commodities[am.Commodity] = info
324
	}
325
	info.Usages = append(info.Usages, CommodityUsage{
326
		FileIndex: fileIndex,
327
		Amount:    am,
328
	})
329
	info.UsedCount++
330
	if date != nil {
331
		info.LastUsed = maxDate(info.LastUsed, *date)
332
	}
333
}
334
335
func (a *Analysis) buildPrefixIndex() {
336
	for name := range a.Accounts {
337
		parts := strings.Split(name, ":")
338
		for i := 1; i < len(parts); i++ {
339
			prefix := strings.Join(parts[:i], ":") + ":"
340
			a.AccountsByPrefix[prefix] = append(a.AccountsByPrefix[prefix], name)
341
		}
342
	}
343
}
344
345
func (a *Analysis) collectPayeeNames() {
346
	names := make([]string, 0, len(a.Payees))
347
	for name := range a.Payees {
348
		names = append(names, name)
349
	}
350
	sort.Strings(names)
351
	a.PayeeNames = names
352
}
353
354
func (a *Analysis) sortAccountNames() {
355
	names := make([]string, 0, len(a.Accounts))
356
	for name := range a.Accounts {
357
		names = append(names, name)
358
	}
359
	sort.Strings(names)
360
	a.AccountNames = names
361
}
362
363
func maxDate(a, b ast.Date) ast.Date {
364
	if a.Year > b.Year ||
365
		(a.Year == b.Year && (a.Month > b.Month ||
366
			(a.Year == b.Year && a.Month == b.Month && a.Day > b.Day))) {
367
		return a
368
	}
369
	return b
370
}