-
Notifications
You must be signed in to change notification settings - Fork 402
/
pricing.go
85 lines (67 loc) · 1.84 KB
/
pricing.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package storagenodedb
import (
"context"
"database/sql"
"errors"
"github.com/zeebo/errs"
"storj.io/common/storj"
"storj.io/storj/storagenode/pricing"
)
// ensures that pricingDB implements pricing.DB interface.
var _ pricing.DB = (*pricingDB)(nil)
// ErrPricing represents errors from the pricing database.
var ErrPricing = errs.Class("pricing")
// PricingDBName represents the database name.
const PricingDBName = "pricing"
// pricing works with node pricing DB.
//
// architecture: Database
type pricingDB struct {
dbContainerImpl
}
// Store inserts or updates pricing model into the db.
func (db *pricingDB) Store(ctx context.Context, pricing pricing.Pricing) (err error) {
defer mon.Task()(&ctx)(&err)
query := `INSERT OR REPLACE INTO pricing (
satellite_id,
egress_bandwidth_price,
repair_bandwidth_price,
audit_bandwidth_price,
disk_space_price
) VALUES(?,?,?,?,?)`
_, err = db.ExecContext(ctx, query,
pricing.SatelliteID,
pricing.EgressBandwidth,
pricing.RepairBandwidth,
pricing.AuditBandwidth,
pricing.DiskSpace,
)
return ErrPricing.Wrap(err)
}
// Get retrieves pricing model for specific satellite.
func (db *pricingDB) Get(ctx context.Context, satelliteID storj.NodeID) (_ *pricing.Pricing, err error) {
defer mon.Task()(&ctx)(&err)
pricingModel := pricing.Pricing{
SatelliteID: satelliteID,
}
row := db.QueryRowContext(ctx,
`SELECT egress_bandwidth_price,
repair_bandwidth_price,
audit_bandwidth_price,
disk_space_price
FROM pricing WHERE satellite_id = ?`,
satelliteID,
)
err = row.Scan(
&pricingModel.EgressBandwidth,
&pricingModel.RepairBandwidth,
&pricingModel.AuditBandwidth,
&pricingModel.DiskSpace,
)
if errors.Is(err, sql.ErrNoRows) {
err = nil
}
return &pricingModel, ErrPricing.Wrap(err)
}