-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_view.go
237 lines (215 loc) · 6.19 KB
/
db_view.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
package main
import (
"fmt"
"html/template"
"log"
"math"
"math/big"
"sort"
"time"
)
// HTMLByID returns a HTML representation of the given customer.
func (t tCustomers) HTMLByID(id CustomerID) template.HTML {
c := t.ByID(id)
if len(c.URL) == 0 {
return template.HTML(c.Name)
}
return template.HTML(fmt.Sprintf(
`<a class="customer" href="%s">%s</a>`, c.URL, c.Name,
))
}
// OutstandingPushFraction bundles a transaction ID with its outstanding
// amount of pushes.
type OutstandingPushFraction struct {
ID ScopedID
Fraction float64
}
// ContribPerCustomer represents the contribution of a single customer towards
// the goal this structure is included in.
type ContribPerCustomer struct {
Customer CustomerID
PushFraction float64
Breakdown []OutstandingPushFraction
}
// TransactionsPerGoal lists all contributions towards a specific goal.
type TransactionsPerGoal struct {
Goal template.HTML
DelayReason template.HTML
PerCustomer []ContribPerCustomer
TotalPushFraction float64
MissingForFullPush float64
}
func (t *TransactionsPerGoal) forCustomer(c CustomerID) *ContribPerCustomer {
for i := range t.PerCustomer {
if t.PerCustomer[i].Customer == c {
return &t.PerCustomer[i]
}
}
t.PerCustomer = append(t.PerCustomer, ContribPerCustomer{Customer: c})
return &t.PerCustomer[len(t.PerCustomer)-1]
}
// TransactionBacklog returns all transactions that haven't been consumed by
// pushes, sorted by goal and customer.
func TransactionBacklog() (ret []TransactionsPerGoal) {
transactionsForGoal := func(goal template.HTML) *TransactionsPerGoal {
for i := range ret {
if ret[i].Goal == goal {
return &ret[i]
}
}
ret = append(ret, TransactionsPerGoal{Goal: goal})
return &ret[len(ret)-1]
}
for i := len(transactions.All) - 1; i >= 0; i-- {
t := transactions.All[i]
if t.Outstanding.Cmp(&big.Rat{}) > 0 {
tfg := transactionsForGoal(t.Goal)
if t.DelayReason != "" {
if tfg.DelayReason != "" {
if t.DelayReason != tfg.DelayReason {
log.Fatalf(`delay reason mismatch at %s:
"%s" (%s) vs.
"%s" (previous)`,
t.ID, t.DelayReason, t.ID, tfg.DelayReason)
}
} else {
tfg.DelayReason = t.DelayReason
}
}
fpc := tfg.forCustomer(t.Customer)
fraction, _ := t.Outstanding.Float64()
opf := OutstandingPushFraction{
ID: *t.ID,
Fraction: fraction,
}
fpc.Breakdown = append(fpc.Breakdown, opf)
fpc.PushFraction += opf.Fraction
if t.ID.Scope == STransaction {
tfg.TotalPushFraction += opf.Fraction
}
}
}
pushPrice := prices.Current().Push
for i := range ret {
_, frac := math.Modf(ret[i].TotalPushFraction)
if frac > 0.0 {
ret[i].MissingForFullPush = ((1.0 - frac) * pushPrice)
}
}
return ret
}
// Where returns the pushes fulfilling the given predicate, with the
// latest pushes at the beginning.
func (t tPushes) Where(pred func(p Push) bool) (ret []Push) {
for i := len(t) - 1; i >= 0; i-- {
p := *t[i]
if pred(p) {
ret = append(ret, p)
}
}
return ret
}
// All returns all pushes, with the latest deliveries at the beginning.
func (t tPushes) All() []Push {
return t.Where(func(p Push) bool { return true })
}
// DiffInfoWeighted combines a DiffInfo with the number of pushes it took to
// get done.
type DiffInfoWeighted struct {
DiffInfo
Pushes float64
}
// DiffsForEstimate returns the diffs of all pushes that are part of the
// progress estimate, sorted from the latest to the earliest delivery.
func (t tPushes) DiffsForEstimate() (ret []DiffInfoWeighted) {
selected := t.Where(func(p Push) bool {
return p.IncludeInEstimate
})
sort.SliceStable(selected, func(i, j int) bool {
return selected[i].Delivered.After(selected[j].Delivered)
})
for _, p := range selected {
if len(ret) > 0 && ret[len(ret)-1].DiffInfo.Rev == p.Diff.Rev {
ret[len(ret)-1].Pushes += 1.0
} else {
ret = append(ret, DiffInfoWeighted{p.Diff, 1.0})
}
}
return
}
// DeliveredAt returns all pushes delivered at the given date.
func (t tPushes) DeliveredAt(datestring string) []Push {
dp, err := time.Parse("2006-01-02", datestring)
FatalIf(err)
aY, aM, aD := dp.Date()
return t.Where(func(p Push) bool {
pY, pM, pD := p.Delivered.Date()
return pD == aD && pM == aM && pY == aY
})
}
// Cap bundles all information about the current state of the backlog, with
// regard to the crowdfunding cap.
type Cap struct {
Pushes int
PushPrice float64
MicroPrice float64
Cap float64
Outstanding float64
Incoming float64
Reserved float64
FreeEuros float64
FracOutstanding float64
FracIncoming float64
FracReserved float64
Ctx interface{}
}
// Reached returns whether the cap has been reached.
func (c Cap) Reached() bool {
return (c.Outstanding + 1) >= c.Cap
}
// CapCurrent calculates the cap from the current point in time.
func CapCurrent(ctx interface{}) (ret Cap) {
scopePrices := prices.Current()
ret.PushPrice = scopePrices.Push
ret.MicroPrice = scopePrices.Micro
ret.Pushes = 10
ret.Cap = float64(ret.Pushes) * ret.PushPrice
backlog := TransactionBacklog()
for _, tpg := range backlog {
if tpg.DelayReason != "" {
continue
}
for _, fpc := range tpg.PerCustomer {
for _, opf := range fpc.Breakdown {
switch opf.ID.Scope {
case STransaction:
ret.Outstanding += opf.Fraction * ret.PushPrice
case SMicro:
ret.Outstanding += opf.Fraction * ret.MicroPrice
}
}
}
}
centsIncoming, centsReserved := incoming.Total(
int(ret.Cap-ret.Outstanding), ret.PushPrice,
)
ret.Incoming = float64(centsIncoming)
ret.Reserved = float64(centsReserved)
sum := ret.Outstanding + ret.Incoming + ret.Reserved
fraction := func(dividend, divisor, resultIfNan float64) float64 {
ret := (dividend / divisor)
if math.IsNaN(ret) {
ret = resultIfNan
}
return math.Min(ret, 1.0) * 100.0
}
ret.FracOutstanding = fraction(ret.Outstanding, ret.Cap, 1.0)
ret.FracIncoming = fraction(ret.Incoming, ret.Cap, 0.0)
ret.FracReserved = fraction(ret.Reserved, ret.Cap, 0.0)
if (ret.FracOutstanding + ret.FracIncoming + ret.FracReserved) > 100.0 {
ret.FracIncoming = ((100.0 - ret.FracOutstanding) - ret.FracReserved)
}
ret.FreeEuros = (ret.Cap - sum) / 100.0
ret.Ctx = ctx
return ret
}