all repos

clerk @ c2e8832

missing tooling for ledger/hledger

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

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