-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.go
84 lines (66 loc) · 2.1 KB
/
data.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
package decentralized
import (
"context"
"fmt"
"strings"
"time"
"github.com/rss3-network/node/internal/database/model"
activityx "github.com/rss3-network/protocol-go/schema/activity"
networkx "github.com/rss3-network/protocol-go/schema/network"
"github.com/samber/lo"
)
func (h *Hub) getActivity(ctx context.Context, request model.ActivityQuery) (*activityx.Activity, *int, error) {
return h.databaseClient.FindActivity(ctx, request)
}
func (h *Hub) getActivities(ctx context.Context, request model.ActivitiesQuery) ([]*activityx.Activity, string, error) {
activities, err := h.databaseClient.FindActivities(ctx, request)
if err != nil {
return nil, "", fmt.Errorf("failed to find activities: %w", err)
}
last, err := lo.Last(activities)
if err == nil {
return activities, h.transformCursor(ctx, last), nil
}
return nil, "", nil
}
func (h *Hub) getCursor(ctx context.Context, cursor *string) (*activityx.Activity, error) {
if cursor == nil {
return nil, nil
}
str := strings.Split(*cursor, ":")
if len(str) != 2 {
return nil, fmt.Errorf("invalid cursor")
}
network, err := networkx.NetworkString(str[1])
if err != nil {
return nil, fmt.Errorf("invalid cursor: %w", err)
}
data, _, err := h.getActivity(ctx, model.ActivityQuery{ID: lo.ToPtr(str[0]), Network: lo.ToPtr(network)})
if err != nil {
return nil, fmt.Errorf("failed to get cursor: %w", err)
}
return data, nil
}
func (h *Hub) transformCursor(_ context.Context, activity *activityx.Activity) string {
if activity == nil {
return ""
}
return fmt.Sprintf("%s:%s", activity.ID, activity.Network)
}
func (h *Hub) getIndexCount(ctx context.Context) (int64, *time.Time, error) {
var (
count int64
updateTime *time.Time
)
checkpoints, err := h.databaseClient.LoadCheckpoints(ctx, "", networkx.Unknown, "")
if err != nil {
return count, nil, fmt.Errorf("failed to find index count: %w", err)
}
for _, checkpoint := range checkpoints {
count += checkpoint.IndexCount
if updateTime == nil || checkpoint.UpdatedAt.After(*updateTime) {
updateTime = lo.ToPtr(checkpoint.UpdatedAt)
}
}
return count, updateTime, nil
}