-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_model.go
604 lines (522 loc) · 14.6 KB
/
db_model.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package main
import (
"encoding/csv"
"fmt"
"html/template"
"log"
"math/big"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/gocarina/gocsv"
"golang.org/x/exp/constraints"
)
const dbPath = "db/"
const devLocationName = "Europe/Berlin"
var devLocation *time.Location
// FreeTimeDecisionPoint defines the cut-off time for deciding whether a day
// will be spent working on ReC98 or not.
const FreeTimeDecisionPoint = time.Duration(time.Hour * 16)
/// Custom types
/// ------------
// ProjectInfo bundles information on a repository that has been added to.
type ProjectInfo struct {
Name string
BlogTags []string
}
var projectMap = map[string]*ProjectInfo{
"ReC98": {"", []string{"rec98"}},
"rec98.nmlgc.net": {"Website", []string{"website"}},
"ssg": {"Seihou", []string{"seihou", "sh01"}},
"tupblocks": {"tupblocks", []string{"build-process"}},
"mly": {"mly", []string{"midi-projects"}},
"BGMPacks": {"BGM packs", []string{"bgm"}},
"msdos-player": {"MS-DOS Player", []string{"build-process"}},
}
// DiffInfo contains all pieces of information parsed from a GitHub diff URL.
type DiffInfo struct {
URL string
Project *ProjectInfo
Rev string
Top *object.Commit // Can be nil if not belonging to ReC98
Bottom *object.Commit // Can be nil if not belonging to ReC98
}
// NewDiffInfo parses a GitHub diff URL into a DiffInfo structure, resolving
// its top and bottom commits using the given repo.
func NewDiffInfo(url string, repo *Repository) DiffInfo {
fatal := func(err string) {
log.Fatalf("%s: %s\n", url, err)
}
must := func(ret *object.Commit, err error) *object.Commit {
if err != nil {
fatal(err.Error())
}
return ret
}
if len(url) == 0 {
fatal("no diff URL provided")
}
s := strings.Split(url, "/")
if len(s) != 4 {
fatal("expected 3 slashes")
}
project, ok := projectMap[s[1]]
if !ok {
fatal("unknown project")
}
rev := s[3]
top, bottom := func() (top *object.Commit, bottom *object.Commit) {
if project.Name != "" {
return nil, nil
}
switch s[2] {
case "compare":
revs := strings.Split(rev, "...")
if len(revs) == 1 && strings.Contains(rev, "..") {
fatal("two-dot ranges not supported")
}
bottom = must(repo.GetCommit(revs[0]))
top = must(repo.GetCommit(revs[1]))
case "commit":
top = must(repo.GetCommit(rev))
if len(top.ParentHashes) > 1 {
fatal("more than one parent; use the \"compare\" mode instead!")
}
bottom = must(top.Parent(0))
default:
fatal("mode must be either \"compare\" or \"commit\"")
}
return
}()
return DiffInfo{
URL: url,
Project: project,
Rev: rev,
Top: top,
Bottom: bottom,
}
}
type eInvalidID struct {
input string
}
func (e eInvalidID) Error() string {
return fmt.Sprintf("invalid ID: \"%s\"", e.input)
}
func parseID(input string, format *regexp.Regexp) (prefix byte, id int, err error) {
idStr := format.FindStringSubmatch(input)
if len(idStr) < 3 {
return 0, 0, eInvalidID{input}
}
ret, err := strconv.ParseUint(idStr[2], 10, 64)
if err != nil {
return 0, 0, err
}
return idStr[1][0], int(ret), nil
}
// LocalDateStamp represents a date-only timestamp in the devLocation.
type LocalDateStamp struct {
time.Time
}
// DateInDevLocation decodes an ISO 8601 date to a LocalDateStamp.
func DateInDevLocation(s string) (ret LocalDateStamp) {
FatalIf(ret.UnmarshalCSV(s))
return
}
// UnmarshalCSV decodes an ISO 8601 date to a LocalDateStamp.
func (d *LocalDateStamp) UnmarshalCSV(s string) (err error) {
d.Time, err = time.ParseInLocation("2006-01-02", s, devLocation)
d.Time = d.Time.Add(FreeTimeDecisionPoint)
return err
}
/// ------------
type Table interface {
Name() string
}
type TableRW interface {
Table
sync.Locker
}
// TableRWOp runs the given operation on a read-write table, and serializes the
// table back to disk on success.
func TableRWOp[T any](table TableRW, op func() (data []*T, err error)) error {
table.Lock()
defer table.Unlock()
data, err := op()
if err != nil {
return err
}
return saveTSV(data, table.Name())
}
/// Schemas
/// -------
type IDScope byte
const (
SMicro IDScope = 'M'
SPush IDScope = 'P'
STransaction IDScope = 'T'
)
// ToTransaction returns the corresponding transaction scope for the delivery
// scope s.
func (s IDScope) ToTransaction() IDScope {
switch s {
case SPush:
return STransaction
case SMicro:
return SMicro
default:
log.Fatalf("trying to use %c as a delivery scope?\n", s)
return 0
}
}
// CustomerID represents a consecutively numbered, 1-based customer ID.
type CustomerID int
// ScopedID represents a consecutively numbered, 1-based ID in any of the ID
// scopes.
type ScopedID struct {
Scope IDScope
ID int
}
var scopedIDFormat = regexp.MustCompile("(M|P|T)([0-9]{4})")
func (i ScopedID) String() string {
return fmt.Sprintf("%c%04d", i.Scope, i.ID)
}
// UnmarshalCSV decodes a ScopedID from its string representation.
func (i *ScopedID) UnmarshalCSV(s string) error {
prefix, id, err := parseID(s, scopedIDFormat)
*i = ScopedID{ID: id, Scope: IDScope(prefix)}
return err
}
// Customer represents everyone who bought something.
type Customer struct {
Name string
URL string
}
// Transaction represents a single money transfer that may or may not be large
// enough to result in one or more pushes.
type Transaction struct {
ID *ScopedID
Time time.Time
Customer CustomerID
Cents int
Goal template.HTML
DelayReason template.HTML
// Calculated after the push table has loaded
Outstanding big.Rat
}
// Consumes outstanding cents up to the remaining fraction from p, and returns
// the new remaining push fraction.
func (t *Transaction) consume(p *pushTSV, fractionNeeded *big.Rat) *big.Rat {
if t.ID.Scope == SMicro {
t.Outstanding.SetInt64(0)
return fractionNeeded.SetInt64(0)
}
nullRat := &big.Rat{}
if fractionNeeded.Cmp(nullRat) <= 0 {
log.Fatalf(
"%s consumed more transactions than it should have (%v)",
p.ID, p.Transactions,
)
} else if t.Outstanding.Cmp(nullRat) <= 0 {
log.Fatalf("more pushes associated with %s than it paid for", t.ID)
}
t.Outstanding.Sub(&t.Outstanding, fractionNeeded)
// Did we need more than this transaction paid for?
if t.Outstanding.Cmp(nullRat) == -1 {
// If yes, we fully consumed this transaction. We still need the amount
// that's now in the negative.
fractionNeeded = fractionNeeded.Set(&t.Outstanding).Neg(fractionNeeded)
t.Outstanding.SetInt64(0)
return fractionNeeded
}
// Got the exact fraction.
return fractionNeeded.SetInt64(0)
}
// Push represents a single unit of work.
type Push struct {
ID ScopedID
Transactions []*Transaction
Goal template.HTML
Delivered time.Time
Diff DiffInfo
IncludeInEstimate bool
}
// FundedBy returns all customers that were involved in funding this push.
func (p Push) FundedBy() (ret []CustomerID) {
for _, t := range p.Transactions {
ret = append(ret, t.Customer)
}
RemoveDuplicates(&ret)
return
}
type Price[T constraints.Integer | constraints.Float] struct {
Push T
Micro T
}
// PriceAt represents the price of one push or push-equivalent microtransaction
// at a given point in time.
type PriceAt struct {
Time time.Time
Price[int]
}
// Incoming represents an unprocessed order coming in from the client side.
type Incoming struct {
// Retrieved via the POST body
CustName string
CustURL string
Metric string
Goal string
Cycle string
Micro bool
// 1-based index into the discountOffers array, or 0 for none.
Discount DiscountID
// Retrieved from PayPal
Cents int
Time *time.Time
// Will only render the associated discount reserve as part of the cap.
ConfirmedAndWaitingForDiscount bool
// Session ID assigned by the payment provider
ProviderSession string
}
// TagDescription bundles a blog tag with a descriptive sentence.
type TagDescription struct {
Tag string
Desc string
}
// ProviderAuth collects all data required for authenticating with payment
// providers.
type ProviderAuth struct {
APIBase string
ClientID string
Secret string
}
// StripeSub contains the key to cancel a Stripe subscription.
type StripeSub struct {
Salt string
ID string
}
type tCustomers []*Customer
type tTransactions struct {
All []*Transaction
Scoped map[IDScope][]*Transaction
}
type tPushes []*Push
type tPrices []*PriceAt
type tBlogTags map[string][]string
type tTagDescriptions struct {
Ordered []*TagDescription
Map map[string]string
}
type tIncoming struct {
sync.Mutex
data []*Incoming
}
func (t *tIncoming) Name() string { return "incoming" }
type tStripeSubs struct {
sync.Mutex
data map[string]string
}
func (t *tStripeSubs) Name() string { return "stripe_subs" }
func (c tCustomers) ByID(id CustomerID) Customer {
return *c[id]
}
func (p tPrices) At(t time.Time) (price Price[int]) {
for _, pushprice := range p {
if pushprice.Time.Before(t) {
price = pushprice.Price
}
}
return
}
func (p tPrices) Current() (prices Price[float64]) {
price := p.At(time.Now())
return Price[float64]{
Push: float64(price.Push),
Micro: float64(price.Micro),
}
}
// Total calculates the total amount of incoming and reserved cents, with
// discount round-ups being calculated relative to the given remaining amount
// of money in the cap.
func (i *tIncoming) Total(capRemaining int, pushprice float64) (cents int, reserved int) {
for _, in := range i.data {
if !in.ConfirmedAndWaitingForDiscount {
cents += in.Cents
capRemaining -= in.Cents
}
if in.Discount != 0 {
offer := discountOffers[in.Discount-1]
fraction := offer.FractionCovered(pushprice)
roundupValue := int(DiscountRoundupValue(
float64(capRemaining), float64(in.Cents), pushprice, fraction,
))
reserved += roundupValue
capRemaining -= roundupValue
}
}
return
}
type eIncomingInsertError struct{}
func (e eIncomingInsertError) Error() string {
return "malformed transaction"
}
func (i *tIncoming) Insert(new *Incoming) error {
return TableRWOp(&incoming, func() ([]*Incoming, error) {
// No timestamp?
if new.Time == nil {
return nil, eIncomingInsertError{}
}
for oldIn := range i.data {
// Duplicates?
if i.data[oldIn].ProviderSession == new.ProviderSession {
return nil, eIncomingInsertError{}
}
}
i.data = append(i.data, new)
return i.data, nil
})
}
func (t *tStripeSubs) ToSlice() (ret []*StripeSub) {
for salt, id := range t.data {
ret = append(ret, &StripeSub{Salt: salt, ID: id})
}
return
}
func (t *tStripeSubs) Insert(salt string, id string) error {
return TableRWOp(&stripeSubs, func() ([]*StripeSub, error) {
t.data[salt] = id
return t.ToSlice(), nil
})
}
func (t *tStripeSubs) Delete(salt string) error {
return TableRWOp(&stripeSubs, func() ([]*StripeSub, error) {
delete(t.data, salt)
return t.ToSlice(), nil
})
}
var customers = tCustomers{}
var transactions = tTransactions{}
var prices = tPrices{}
var incoming = tIncoming{}
var blogTags = tBlogTags{}
var tagDescriptions = tTagDescriptions{}
var providerAuth = make(map[string]ProviderAuth)
var stripeSubs = tStripeSubs{data: make(map[string]string)}
/// -------
// TSV input structures
// --------------------
type pushTSV struct {
ID *ScopedID
Transactions []int
Goal template.HTML
Delivered time.Time
Diff string
IncludeInEstimate bool
}
func (p *pushTSV) toActualPush(repo *Repository) *Push {
return &Push{
ID: *p.ID,
Goal: p.Goal,
Delivered: p.Delivered,
Diff: NewDiffInfo(p.Diff, repo),
IncludeInEstimate: p.IncludeInEstimate,
Transactions: func() (ret []*Transaction) {
if len(p.Transactions) == 0 {
log.Fatalf("%s has no transactions associated with it", p.ID)
}
fractionNeeded := big.NewRat(1, 1)
for _, tid := range p.Transactions {
t := transactions.Scoped[p.ID.Scope.ToTransaction()][tid-1]
ret = append(ret, t)
}
for _, t := range ret {
fractionNeeded = t.consume(p, fractionNeeded)
}
if fractionNeeded.Cmp(&big.Rat{}) != 0 {
log.Fatalf(
"%s is not fully paid for (missing %v pushes)",
p.ID, fractionNeeded,
)
}
return
}(),
}
}
var tsvPushes []*pushTSV
// NewPushes parses tsv into a tPushes object, consuming the given transactions
// and validating their assignment to the respective pushes. Commit references
// are directly resolved using the given repo.
func NewPushes(transactions tTransactions, tsv []*pushTSV, repo *Repository) (ret tPushes) {
for _, p := range tsvPushes {
ret = append(ret, p.toActualPush(repo))
}
return
}
type providerAuthTSV struct {
Provider string
ProviderAuth
}
// --------------------
func tsvPath(table string) string {
return filepath.Join(dbPath, table+".tsv")
}
func loadTSV(slice interface{}, table string, unmarshaler func(gocsv.CSVReader, interface{}) error) {
LoadTSV(slice, tsvPath(table), unmarshaler)
}
func saveTSV(slice interface{}, table string) error {
fnRegular := tsvPath(table)
fnNew := fmt.Sprintf("%s-%v.tsv", fnRegular, time.Now().UnixNano())
f, err := os.Create(fnNew)
if err != nil {
return err
}
writer := csv.NewWriter(f)
writer.Comma = '\t'
err = gocsv.MarshalCSV(slice, gocsv.NewSafeCSVWriter(writer))
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return os.Rename(fnNew, fnRegular)
}
func init() {
var err error
var providerAuths []*providerAuthTSV
devLocation, err = time.LoadLocation(devLocationName)
FatalIf(err)
loadTSV(&customers, "customers", gocsv.UnmarshalCSV)
loadTSV(&transactions.All, "transactions", gocsv.UnmarshalCSV)
loadTSV(&tsvPushes, "pushes", gocsv.UnmarshalCSV)
loadTSV(&prices, "prices", gocsv.UnmarshalCSV)
loadTSV(&incoming.data, incoming.Name(), gocsv.UnmarshalCSV)
loadTSV(&blogTags, "blog_tags", gocsv.UnmarshalCSVToMap)
loadTSV(&tagDescriptions.Ordered, "tag_descriptions", gocsv.UnmarshalCSV)
loadTSV(&providerAuths, "provider_auth", gocsv.UnmarshalCSV)
loadTSV(&stripeSubs.data, stripeSubs.Name(), gocsv.UnmarshalCSVToMap)
transactions.Scoped = make(map[IDScope][]*Transaction)
for _, transaction := range transactions.All {
scopePrices := prices.At(transaction.Time)
price := int64(scopePrices.Push)
if transaction.ID.Scope == SMicro {
price = int64(scopePrices.Micro)
}
transaction.Outstanding.SetFrac64(int64(transaction.Cents), price)
transactions.Scoped[transaction.ID.Scope] = append(
transactions.Scoped[transaction.ID.Scope], transaction,
)
}
tagDescriptions.Map = make(map[string]string)
for _, td := range tagDescriptions.Ordered {
tagDescriptions.Map[td.Tag] = td.Desc
}
for _, auth := range providerAuths {
providerAuth[auth.Provider] = auth.ProviderAuth
}
}