-
Notifications
You must be signed in to change notification settings - Fork 66
/
ranking.go
93 lines (78 loc) · 1.9 KB
/
ranking.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
package miner
import (
"sort"
"time"
"github.com/application-research/estuary/model"
"github.com/filecoin-project/go-address"
)
const minerListTTL = time.Minute
func (mm *MinerManager) SortedMinerList() ([]address.Address, []*minerDealStats, error) {
mm.minerLk.Lock()
defer mm.minerLk.Unlock()
if time.Since(mm.lastComputed) < minerListTTL {
return mm.sortedMiners, mm.rawData, nil
}
sml, err := mm.ComputeSortedMinerList()
if err != nil {
return nil, nil, err
}
sortedAddrs := make([]address.Address, 0, len(sml))
for _, m := range sml {
sus, err := mm.minerIsSuspended(m.Miner)
if err != nil {
return nil, nil, err
}
if !sus {
sortedAddrs = append(sortedAddrs, m.Miner)
}
}
mm.rawData = sml
mm.lastComputed = time.Now()
mm.sortedMiners = sortedAddrs
return sortedAddrs, sml, nil
}
func (mm *MinerManager) minerIsSuspended(m address.Address) (bool, error) {
var miner model.StorageMiner
if err := mm.db.Find(&miner, "address = ?", m.String()).Error; err != nil {
return false, err
}
return miner.Suspended, nil
}
func (mm *MinerManager) ComputeSortedMinerList() ([]*minerDealStats, error) {
var deals []model.ContentDeal
if err := mm.db.Find(&deals).Error; err != nil {
return nil, err
}
stats := make(map[address.Address]*minerDealStats)
for _, d := range deals {
maddr, err := d.MinerAddr()
if err != nil {
return nil, err
}
st, ok := stats[maddr]
if !ok {
st = &minerDealStats{
Miner: maddr,
}
stats[maddr] = st
}
st.TotalDeals++
if d.DealID > 0 {
if d.Failed {
st.DealFaults++
} else {
st.ConfirmedDeals++
}
} else if d.Failed {
st.FailedDeals++
}
}
minerStatsArr := make([]*minerDealStats, 0, len(stats))
for _, st := range stats {
minerStatsArr = append(minerStatsArr, st)
}
sort.Slice(minerStatsArr, func(i, j int) bool {
return minerStatsArr[i].Better(minerStatsArr[j])
})
return minerStatsArr, nil
}