-
Notifications
You must be signed in to change notification settings - Fork 4
/
db_client.go
131 lines (105 loc) · 3.64 KB
/
db_client.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
package explorer
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/internal/explorer/db"
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/client"
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
)
// DBClient is an implementation for the db client interface [github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/client.DBClient]
//
// It fetches the desired data from the database, does the appropriate type conversions, and returns the result.
type DBClient struct {
DB db.Database
}
var _ client.DBClient = (*DBClient)(nil)
func (c *DBClient) Nodes(ctx context.Context, filter types.NodeFilter, pagination types.Limit) ([]types.Node, int, error) {
dbNodes, cnt, err := c.DB.GetNodes(ctx, filter, pagination)
if err != nil {
return nil, 0, err
}
nodes := make([]types.Node, len(dbNodes))
for idx, node := range dbNodes {
nodes[idx] = nodeFromDBNode(node)
}
return nodes, int(cnt), nil
}
func (c *DBClient) Farms(ctx context.Context, filter types.FarmFilter, pagination types.Limit) ([]types.Farm, int, error) {
dbFarms, cnt, err := c.DB.GetFarms(ctx, filter, pagination)
if err != nil {
return nil, 0, err
}
farms := make([]types.Farm, 0, len(dbFarms))
for _, farm := range dbFarms {
f, err := farmFromDBFarm(farm)
if err != nil {
return nil, 0, err
}
farms = append(farms, f)
}
return farms, int(cnt), nil
}
func (c *DBClient) Contracts(ctx context.Context, filter types.ContractFilter, pagination types.Limit) ([]types.Contract, int, error) {
dbContracts, cnt, err := c.DB.GetContracts(ctx, filter, pagination)
if err != nil {
return nil, 0, err
}
contracts := make([]types.Contract, len(dbContracts))
for idx, contract := range dbContracts {
contracts[idx], err = contractFromDBContract(contract)
if err != nil {
return nil, 0, err
}
}
return contracts, int(cnt), nil
}
func (c *DBClient) Contract(ctx context.Context, contractID uint32) (types.Contract, error) {
dbContract, err := c.DB.GetContract(ctx, contractID)
if err != nil {
return types.Contract{}, err
}
contract, err := contractFromDBContract(dbContract)
if err != nil {
log.Err(err).Msg("failed to convert db contract to api contract")
}
return contract, nil
}
func (c *DBClient) ContractBills(ctx context.Context, contractID uint32, limit types.Limit) ([]types.ContractBilling, uint, error) {
dbBills, cnt, err := c.DB.GetContractBills(ctx, contractID, limit)
if err != nil {
return nil, 0, fmt.Errorf("failed to get contract %d bills: %w", contractID, err)
}
bills := []types.ContractBilling{}
for _, report := range dbBills {
bills = append(bills, types.ContractBilling(report))
}
return bills, cnt, nil
}
func (c *DBClient) Twins(ctx context.Context, filter types.TwinFilter, pagination types.Limit) ([]types.Twin, int, error) {
dbTwins, cnt, err := c.DB.GetTwins(ctx, filter, pagination)
if err != nil {
return nil, 0, err
}
return dbTwins, int(cnt), nil
}
func (c *DBClient) Node(ctx context.Context, nodeID uint32) (types.NodeWithNestedCapacity, error) {
dbNode, err := c.DB.GetNode(ctx, nodeID)
if err != nil {
return types.NodeWithNestedCapacity{}, err
}
node := nodeWithNestedCapacityFromDBNode(dbNode)
return node, nil
}
func (c *DBClient) NodeStatus(ctx context.Context, nodeID uint32) (types.NodeStatus, error) {
dbNode, err := c.DB.GetNode(ctx, nodeID)
if err != nil {
return types.NodeStatus{}, err
}
node := nodeWithNestedCapacityFromDBNode(dbNode)
status := types.NodeStatus{Status: node.Status}
return status, nil
}
func (c *DBClient) Stats(ctx context.Context, filter types.StatsFilter) (types.Stats, error) {
return c.DB.GetStats(ctx, filter)
}