|
| 1 | +package generator |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + feast "github.com/feast-dev/feast/sdk/go" |
| 8 | + "github.com/feast-dev/feast/sdk/go/protos/feast/types" |
| 9 | + "math/rand" |
| 10 | + "os" |
| 11 | + "strconv" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type LoadSpec struct { |
| 16 | + EntitySpec []EntitySpec `yaml:"entities"` |
| 17 | + RequestSpecs []RequestSpec `yaml:"requests"` |
| 18 | +} |
| 19 | + |
| 20 | +type EntitySpec struct { |
| 21 | + Name string `yaml:"name"` |
| 22 | + Type string `yaml:"type"` |
| 23 | + FileSource FileSource `yaml:"fileSource"` |
| 24 | + RandInt RandInt `yaml:"randInt"` |
| 25 | +} |
| 26 | + |
| 27 | +type FileSource struct { |
| 28 | + Path string `yaml:"path"` |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +type RandInt struct { |
| 33 | + Min int64 `yaml:"min"` |
| 34 | + Max int64 `yaml:"max"` |
| 35 | +} |
| 36 | + |
| 37 | +// Generate all possible values for an entity |
| 38 | +type EntityPoolGenerator interface { |
| 39 | + GenerateEntityValues() ([]*types.Value, error) |
| 40 | +} |
| 41 | + |
| 42 | +// Generate all possible values for an entity from a file source |
| 43 | +type FileSourceEntityValueGenerator struct { |
| 44 | + entity EntitySpec |
| 45 | +} |
| 46 | + |
| 47 | +func (generator FileSourceEntityValueGenerator) GenerateEntityValues() ([]*types.Value, error) { |
| 48 | + var entityValues []*types.Value |
| 49 | + file, err := os.Open(generator.entity.FileSource.Path) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + scanner := bufio.NewScanner(file) |
| 54 | + scanner.Split(bufio.ScanLines) |
| 55 | + for scanner.Scan() { |
| 56 | + parsedValue, err := generator.parseStrToEntityValue(generator.entity.Type, scanner.Text()) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + entityValues = append(entityValues, parsedValue) |
| 61 | + } |
| 62 | + return entityValues, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (generator FileSourceEntityValueGenerator) parseStrToEntityValue(valueType string, valueStr string) (*types.Value, error) { |
| 66 | + switch valueType { |
| 67 | + case "string": |
| 68 | + return feast.StrVal(valueStr), nil |
| 69 | + case "int32": |
| 70 | + parsedValue, err := strconv.ParseInt(valueStr, 10, 32) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + return feast.Int32Val(int32(parsedValue)), nil |
| 75 | + case "int64": |
| 76 | + parsedValue, err := strconv.ParseInt(valueStr, 10, 64) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + return feast.Int64Val(parsedValue), nil |
| 81 | + case "float": |
| 82 | + parsedValue, err := strconv.ParseFloat(valueStr, 32) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return feast.FloatVal(float32(parsedValue)), nil |
| 87 | + case "double": |
| 88 | + parsedValue, err := strconv.ParseFloat(valueStr, 64) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + return feast.DoubleVal(parsedValue), nil |
| 93 | + case "bool": |
| 94 | + parsedValue, err := strconv.ParseBool(valueStr) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + return feast.BoolVal(parsedValue), nil |
| 99 | + } |
| 100 | + |
| 101 | + return nil, errors.New(fmt.Sprintf("Unrecognized value type: %s", valueType)) |
| 102 | +} |
| 103 | + |
| 104 | +// Generate all possible values for an entity based on a range of integer |
| 105 | +type RandIntEntityValueGenerator struct { |
| 106 | + entity EntitySpec |
| 107 | +} |
| 108 | + |
| 109 | +func (generator RandIntEntityValueGenerator) GenerateEntityValues() ([]*types.Value, error) { |
| 110 | + entityType := generator.entity.Type |
| 111 | + switch entityType { |
| 112 | + case "int64": |
| 113 | + minValue := generator.entity.RandInt.Min |
| 114 | + maxValue := generator.entity.RandInt.Max |
| 115 | + poolSize := maxValue - minValue + 1 |
| 116 | + entityValues := make([]*types.Value, poolSize) |
| 117 | + for i := int64(0); i < poolSize; i++ { |
| 118 | + entityValues[i] = feast.Int64Val(i + minValue) |
| 119 | + } |
| 120 | + return entityValues, nil |
| 121 | + case "int32": |
| 122 | + minValue := int32(generator.entity.RandInt.Min) |
| 123 | + maxValue := int32(generator.entity.RandInt.Max) |
| 124 | + poolSize := maxValue - minValue + 1 |
| 125 | + entityValues := make([]*types.Value, poolSize) |
| 126 | + for i := int32(0); i < poolSize; i++ { |
| 127 | + entityValues[i] = feast.Int32Val(i + minValue) |
| 128 | + } |
| 129 | + return entityValues, nil |
| 130 | + default: |
| 131 | + return nil, errors.New(fmt.Sprintf("Unsupported entity type: %s", entityType)) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +type RequestSpec struct { |
| 136 | + Entities []string `yaml:"entities"` |
| 137 | + Features []string `yaml:"features"` |
| 138 | + EntityCount int32 `yaml:"entityCount"` |
| 139 | +} |
| 140 | + |
| 141 | +type RequestGenerator struct { |
| 142 | + entityToValuePoolMap map[string][]*types.Value |
| 143 | + requests []RequestSpec |
| 144 | + project string |
| 145 | +} |
| 146 | + |
| 147 | +func NewRequestGenerator(loadSpec LoadSpec, project string) (RequestGenerator, error) { |
| 148 | + entityToValuePoolMap := map[string][]*types.Value{} |
| 149 | + for _, entity := range loadSpec.EntitySpec { |
| 150 | + var poolGenerator EntityPoolGenerator |
| 151 | + if entity.FileSource != (FileSource{}) { |
| 152 | + poolGenerator = FileSourceEntityValueGenerator{entity} |
| 153 | + } else { |
| 154 | + poolGenerator = RandIntEntityValueGenerator{entity} |
| 155 | + } |
| 156 | + pool, err := poolGenerator.GenerateEntityValues() |
| 157 | + if err != nil { |
| 158 | + return RequestGenerator{}, err |
| 159 | + } |
| 160 | + entityToValuePoolMap[entity.Name] = pool |
| 161 | + } |
| 162 | + return RequestGenerator{ |
| 163 | + entityToValuePoolMap: entityToValuePoolMap, |
| 164 | + requests: loadSpec.RequestSpecs, |
| 165 | + project: project, |
| 166 | + }, nil |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +func (generator *RequestGenerator) GenerateRandomRows(entities []string, entityCount int32) []feast.Row { |
| 171 | + rows := make([]feast.Row, entityCount) |
| 172 | + for _, entity := range entities { |
| 173 | + valuePool := generator.entityToValuePoolMap[entity] |
| 174 | + rand.Seed(time.Now().UnixNano()) |
| 175 | + rand.Shuffle(len(valuePool), func(i, j int) { valuePool[i], valuePool[j] = valuePool[j], valuePool[i] }) |
| 176 | + } |
| 177 | + |
| 178 | + for i := int32(0); i < entityCount; i++ { |
| 179 | + row := feast.Row{} |
| 180 | + for _, entity := range entities { |
| 181 | + valuePool := generator.entityToValuePoolMap[entity] |
| 182 | + row[entity] = valuePool[i] |
| 183 | + } |
| 184 | + rows[i]=row |
| 185 | + } |
| 186 | + |
| 187 | + return rows |
| 188 | +} |
| 189 | + |
| 190 | +func (generator *RequestGenerator) GenerateRequests() []feast.OnlineFeaturesRequest { |
| 191 | + var onlineFeatureRequests []feast.OnlineFeaturesRequest |
| 192 | + for _, request := range generator.requests { |
| 193 | + entityRows := generator.GenerateRandomRows(request.Entities, request.EntityCount) |
| 194 | + onlineFeatureRequests = append(onlineFeatureRequests, feast.OnlineFeaturesRequest{ |
| 195 | + Features: request.Features, |
| 196 | + Entities: entityRows, |
| 197 | + Project: generator.project, |
| 198 | + }) |
| 199 | + } |
| 200 | + return onlineFeatureRequests |
| 201 | +} |
0 commit comments