-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetch_assets.go
159 lines (129 loc) · 3.5 KB
/
fetch_assets.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
package statistics
import (
"context"
"database/sql"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/gnasnik/titan-explorer/core/dao"
"github.com/gnasnik/titan-explorer/core/generated/model"
"github.com/golang-module/carbon/v2"
errs "github.com/pkg/errors"
"sort"
"time"
)
const (
defaultRequestLimit = 500
defaultBackupDays = 7
)
// AssertFetcher represents a fetcher for asset information.
type AssertFetcher struct {
BaseFetcher
}
// Register the AssertFetcher during initialization
func init() {
RegisterFetcher(newAssertFetcher)
}
// newAssertFetcher creates a new instance of AssertFetcher.
func newAssertFetcher() Fetcher {
return &AssertFetcher{BaseFetcher: newBaseFetcher()}
}
// Fetch fetches asset information.
func (a AssertFetcher) Fetch(ctx context.Context, scheduler *Scheduler) error {
log.Info("Start to fetch assert info")
start := time.Now()
defer func() {
log.Infof("Fetch assert files, cost: %v", time.Since(start))
}()
latest, err := dao.GetLatestAsset(ctx)
if err != nil && !errs.Is(err, sql.ErrNoRows) {
return err
}
var (
startTime, endTime time.Time
limit, offset int
)
if latest == nil || latest.EndTime.IsZero() {
startTime = carbon.Now().StartOfDay().SubDays(defaultBackupDays).StdTime()
} else {
startTime = latest.EndTime
}
limit = defaultRequestLimit
endTime = carbon.Now().EndOfDay().StdTime()
Loop:
assertsRes, err := scheduler.Api.GetReplicaEvents(ctx, startTime, endTime, limit, offset)
if err != nil {
log.Errorf("client api GetReplicaEvents: %v", err)
return err
}
offset += len(assertsRes.ReplicaEvents)
if len(assertsRes.ReplicaEvents) == 1 && assertsRes.ReplicaEvents[0].Cid == latest.Cid {
return nil
}
assets, err := toAssets(assertsRes.ReplicaEvents)
if err != nil {
log.Errorf("toAssets: %v", err)
return err
}
if len(assets) == 0 {
return nil
}
err = dao.AddAssets(ctx, assets)
if err != nil {
log.Errorf("create user assets: %v", err)
}
if assertsRes.Total > offset {
goto Loop
}
stats, err := dao.CountAssets(ctx)
if err != nil {
log.Errorf("count assets err: %v", err)
return err
}
if len(stats) == 0 {
return nil
}
sort.Slice(stats, func(i, j int) bool {
return stats[i].TotalSize > stats[j].TotalSize
})
for index, current := range stats {
stats[index].Rank = int64(index) + 1
storages, _, err := dao.ListStorageStats(ctx, current.ProjectId, dao.QueryOption{
Page: 1,
PageSize: 1,
StartTime: carbon.Now().SubHours(24).String(),
EndTime: carbon.Now().String(),
})
if err != nil {
log.Errorf("ListStorageStats: %v", err)
continue
}
if len(storages) == 0 {
continue
}
current.StorageChange24H = current.TotalSize - storages[0].TotalSize
current.StorageChangePercentage24H = float64(current.TotalSize-storages[0].TotalSize) / float64(storages[0].TotalSize)
}
return dao.AddStorageStats(ctx, stats)
}
// toAssets converts a slice of ReplicaEventInfo to a slice of Asset.
func toAssets(in []*types.ReplicaEventInfo) ([]*model.Asset, error) {
var out []*model.Asset
for _, r := range in {
if types.AssetSource(r.Source) == types.AssetSourceAWS || types.AssetSource(r.Source) == types.AssetSourceMinio {
continue
}
out = append(out, &model.Asset{
NodeID: r.NodeID,
Event: int64(r.Event),
Cid: r.Cid,
Hash: r.Hash,
TotalSize: r.TotalSize,
Expiration: r.Expiration,
EndTime: r.EndTime,
})
}
return out, nil
}
func (a AssertFetcher) Finalize() error {
return nil
}
var _ Fetcher = &AssertFetcher{}