-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathpredictor_service.go
83 lines (66 loc) · 1.96 KB
/
predictor_service.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
package optimus
import (
"fmt"
"math/big"
"github.com/sonm-io/core/proto"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (m *PredictorService) Predict(ctx context.Context, request *sonm.BidResources) (*sonm.Price, error) {
classification := m.Classification()
if classification == nil {
return nil, status.Errorf(codes.Unavailable, "regression is not finished yet")
}
knownBenchmarks := m.benchmarks.MapByCode()
givenBenchmarks := request.GetBenchmarks()
if len(givenBenchmarks) > len(knownBenchmarks) {
return nil, fmt.Errorf("benchmark list too large")
}
benchmarksValues := make([]uint64, len(knownBenchmarks))
for code, value := range givenBenchmarks {
bench, ok := knownBenchmarks[code]
if !ok {
return nil, fmt.Errorf("unknown benchmark code: %s", code)
}
benchmarksValues[bench.GetID()] = value
}
orderBenchmarks, err := sonm.NewBenchmarks(benchmarksValues)
if err != nil {
return nil, fmt.Errorf("could not parse benchmark values: %s", err)
}
order := &sonm.Order{
Benchmarks: orderBenchmarks,
}
price, err := classification.Predictor.PredictPrice(&MarketOrder{
Order: order,
})
if err != nil {
return nil, err
}
price /= priceMultiplier
priceBigF := big.NewFloat(price)
priceBigI, _ := priceBigF.Int(nil)
return &sonm.Price{
PerSecond: sonm.NewBigInt(priceBigI),
}, nil
}
func (m *PredictorService) PredictSupplier(ctx context.Context, request *sonm.PredictSupplierRequest) (*sonm.PredictSupplierReply, error) {
request.Normalize()
worker := newMockWorker(request.GetDevices())
engine, err := m.engineFactory(worker)
if err != nil {
return nil, err
}
if err := engine.execute(ctx); err != nil {
return nil, err
}
orderIDs := make([]*sonm.BigInt, 0, len(worker.Result))
for _, plan := range worker.Result {
orderIDs = append(orderIDs, plan.GetOrderID())
}
return &sonm.PredictSupplierReply{
Price: sonm.SumPrice(worker.Result),
OrderIDs: orderIDs,
}, nil
}