-
Notifications
You must be signed in to change notification settings - Fork 960
/
go_integration_test_utils.go
242 lines (214 loc) · 6.58 KB
/
go_integration_test_utils.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package test
import (
"context"
"fmt"
"log"
"github.com/apache/arrow/go/v8/arrow/memory"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/apache/arrow/go/v8/arrow"
"github.com/apache/arrow/go/v8/parquet/file"
"github.com/apache/arrow/go/v8/parquet/pqarrow"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/apache/arrow/go/v8/arrow/array"
"github.com/feast-dev/feast/go/internal/feast/model"
"github.com/feast-dev/feast/go/protos/feast/types"
gotypes "github.com/feast-dev/feast/go/types"
)
type Row struct {
EventTimestamp int64
DriverId int64
ConvRate float32
AccRate float32
AvgDailyTrips int32
Created int64
}
func ReadParquet(filePath string) ([]*Row, error) {
allocator := memory.NewGoAllocator()
pqfile, err := file.OpenParquetFile(filePath, false)
if err != nil {
return nil, err
}
reader, err := pqarrow.NewFileReader(pqfile, pqarrow.ArrowReadProperties{}, allocator)
if err != nil {
return nil, err
}
fmt.Println(reader)
table, err := reader.ReadTable(context.Background())
if err != nil {
return nil, err
}
columns := make(map[string]arrow.Array)
fields := table.Schema().Fields()
for idx, field := range fields {
columns[field.Name] = table.Column(idx).Data().Chunk(0)
}
rows := make([]*Row, 0)
for rowIdx := 0; rowIdx < int(table.NumRows()); rowIdx++ {
rows = append(rows, &Row{
EventTimestamp: columns["event_timestamp"].(*array.Timestamp).Value(rowIdx).ToTime(arrow.Second).Unix(),
DriverId: columns["driver_id"].(*array.Int64).Value(rowIdx),
ConvRate: columns["conv_rate"].(*array.Float32).Value(rowIdx),
AccRate: columns["acc_rate"].(*array.Float32).Value(rowIdx),
AvgDailyTrips: columns["avg_daily_trips"].(*array.Int32).Value(rowIdx),
Created: columns["created"].(*array.Timestamp).Value(rowIdx).ToTime(arrow.Second).Unix(),
})
}
return rows, nil
}
func GetLatestFeatures(Rows []*Row, entities map[int64]bool) map[int64]*Row {
correctFeatureRows := make(map[int64]*Row)
for _, Row := range Rows {
if _, ok := entities[Row.DriverId]; ok {
if _, ok := correctFeatureRows[Row.DriverId]; ok {
if Row.EventTimestamp > correctFeatureRows[Row.DriverId].EventTimestamp {
correctFeatureRows[Row.DriverId] = Row
}
} else {
correctFeatureRows[Row.DriverId] = Row
}
}
}
return correctFeatureRows
}
func SetupCleanFeatureRepo(basePath string) error {
cmd := exec.Command("feast", "init", "my_project")
path, err := filepath.Abs(basePath)
cmd.Env = os.Environ()
if err != nil {
return err
}
cmd.Dir = path
err = cmd.Run()
if err != nil {
return err
}
applyCommand := exec.Command("feast", "apply")
applyCommand.Env = os.Environ()
featureRepoPath, err := filepath.Abs(filepath.Join(path, "my_project", "feature_repo"))
if err != nil {
return err
}
applyCommand.Dir = featureRepoPath
applyCommand.Run()
t := time.Now()
formattedTime := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
materializeCommand := exec.Command("feast", "materialize-incremental", formattedTime)
materializeCommand.Env = os.Environ()
materializeCommand.Dir = featureRepoPath
err = materializeCommand.Run()
if err != nil {
return err
}
return nil
}
func SetupInitializedRepo(basePath string) error {
path, err := filepath.Abs(basePath)
if err != nil {
return err
}
applyCommand := exec.Command("feast", "apply")
applyCommand.Env = os.Environ()
featureRepoPath, err := filepath.Abs(filepath.Join(path, "feature_repo"))
if err != nil {
return err
}
// var stderr bytes.Buffer
// var stdout bytes.Buffer
applyCommand.Dir = featureRepoPath
out, err := applyCommand.CombinedOutput()
if err != nil {
log.Println(string(out))
return err
}
t := time.Now()
formattedTime := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
materializeCommand := exec.Command("feast", "materialize-incremental", formattedTime)
materializeCommand.Env = os.Environ()
materializeCommand.Dir = featureRepoPath
out, err = materializeCommand.CombinedOutput()
if err != nil {
log.Println(string(out))
return err
}
return nil
}
func CleanUpInitializedRepo(basePath string) {
featureRepoPath, err := filepath.Abs(filepath.Join(basePath, "feature_repo"))
if err != nil {
log.Fatal(err)
}
err = os.Remove(filepath.Join(featureRepoPath, "data", "registry.db"))
if err != nil {
log.Fatal(err)
}
err = os.Remove(filepath.Join(featureRepoPath, "data", "online_store.db"))
if err != nil {
log.Fatal(err)
}
}
func GetProtoFromRecord(rec arrow.Record) (map[string]*types.RepeatedValue, error) {
r := make(map[string]*types.RepeatedValue)
schema := rec.Schema()
for idx, column := range rec.Columns() {
field := schema.Field(idx)
if field.Type.ID() == arrow.FixedWidthTypes.Timestamp_ms.ID() || field.Type.ID() == arrow.FixedWidthTypes.Date32.ID() {
continue
}
values, err := gotypes.ArrowValuesToProtoValues(column)
if err != nil {
return nil, err
}
r[field.Name] = &types.RepeatedValue{Val: values}
}
return r, nil
}
func CreateBaseFeatureView(name string, features []*model.Field, projection *model.FeatureViewProjection) *model.BaseFeatureView {
return &model.BaseFeatureView{
Name: name,
Features: features,
Projection: projection,
}
}
func CreateNewEntity(name string, joinKey string) *model.Entity {
return &model.Entity{
Name: name,
JoinKey: joinKey,
}
}
func CreateNewField(name string, dtype types.ValueType_Enum) *model.Field {
return &model.Field{Name: name,
Dtype: dtype,
}
}
func CreateNewFeatureService(name string, project string, createdTimestamp *timestamppb.Timestamp, lastUpdatedTimestamp *timestamppb.Timestamp, projections []*model.FeatureViewProjection) *model.FeatureService {
return &model.FeatureService{
Name: name,
Project: project,
CreatedTimestamp: createdTimestamp,
LastUpdatedTimestamp: lastUpdatedTimestamp,
Projections: projections,
}
}
func CreateNewFeatureViewProjection(name string, nameAlias string, features []*model.Field, joinKeyMap map[string]string) *model.FeatureViewProjection {
return &model.FeatureViewProjection{Name: name,
NameAlias: nameAlias,
Features: features,
JoinKeyMap: joinKeyMap,
}
}
func CreateFeatureView(base *model.BaseFeatureView, ttl *durationpb.Duration, entities []string, entityColumns []*model.Field) *model.FeatureView {
return &model.FeatureView{
Base: base,
Ttl: ttl,
EntityNames: entities,
EntityColumns: entityColumns,
}
}