Skip to content

Commit

Permalink
Merge pull request #942 from oom-ai/stream-push
Browse files Browse the repository at this point in the history
Stream push
  • Loading branch information
lianxmfor authored Jan 12, 2022
2 parents d58a681 + 9a40eb1 commit ae7b96b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
64 changes: 64 additions & 0 deletions oomcli/cmd/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/oom-ai/oomstore/pkg/oomstore/types"
)

type PushOption struct {
EntityKey string
GroupName string
FeaturePair map[string]string

featureNames []string
featureValues []interface{}
}

var pushOpt PushOption

var pushCmd = &cobra.Command{
Use: "push",
Short: "Push stream feature",
PreRun: func(cmd *cobra.Command, args []string) {
for feature, value := range pushOpt.FeaturePair {
pushOpt.featureNames = append(pushOpt.featureNames, feature)
pushOpt.featureValues = append(pushOpt.featureValues, value)
}
},
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
oomStore := mustOpenOomStore(ctx, oomStoreCfg)
defer oomStore.Close()

if err := oomStore.Push(ctx, types.PushOpt{
EntityKey: pushOpt.EntityKey,
GroupName: pushOpt.GroupName,
FeatureNames: pushOpt.featureNames,
FeatureValues: pushOpt.featureValues,
}); err != nil {
exitf("failed push features: %+v\n", err)
}

fmt.Fprintln(os.Stderr, "succeeded")
},
}

func init() {
rootCmd.AddCommand(pushCmd)

flags := pushCmd.Flags()

flags.StringVarP(&pushOpt.EntityKey, "entity-key", "k", "", "entity key")
_ = getOnlineCmd.MarkFlagRequired("entity-key")

flags.StringVarP(&pushOpt.GroupName, "group", "g", "", "feature group")
_ = pushCmd.MarkFlagRequired("group")

flags.StringToStringVarP(&pushOpt.FeaturePair, "features", "f", nil, "features")
_ = pushCmd.MarkFlagRequired("features")
}
7 changes: 7 additions & 0 deletions oomcli/test/test_get_meta_entity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ items:
- name: user-click
category: stream
description: user click post feature
features:
- name: last_5_click_posts
value-type: string
description: user last 5 click posts
- name: number_of_user_started_posts
value-type: int64
description: number of posts that users stared today
'

actual=$(oomcli get meta entity -o yaml)
Expand Down
14 changes: 14 additions & 0 deletions oomcli/test/test_get_meta_feature.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ ID,NAME,GROUP,ENTITY,CATEGORY,VALUE-TYPE,DESCRIPTION,ONLINE-REVISION-ID
1,price,phone,device,batch,int64,price,<NULL>
2,model,phone,device,batch,string,model,<NULL>
3,age,student,user,batch,int64,age,<NULL>
4,last_5_click_posts,user-click,user,stream,string,user last 5 click posts,<NULL>
5,number_of_user_started_posts,user-click,user,stream,int64,number of posts that users stared today,<NULL>
'
actual=$(oomcli get meta feature -o csv --wide)
ignore_time() { cut -d ',' -f 1-8 <<<"$1"; }
Expand All @@ -22,6 +24,8 @@ expected='ID,NAME,GROUP,ENTITY,CATEGORY,VALUE-TYPE,DESCRIPTION
1,price,phone,device,batch,int64,price
2,model,phone,device,batch,string,model
3,age,student,user,batch,int64,age
4,last_5_click_posts,user-click,user,stream,string,user last 5 click posts
5,number_of_user_started_posts,user-click,user,stream,int64,number of posts that users stared today
'
actual=$(oomcli get meta feature -o csv)
assert_eq "$case" "$(sort <<< "$expected")" "$(sort <<< "$actual")"
Expand Down Expand Up @@ -66,6 +70,16 @@ items:
group-name: student
value-type: int64
description: age
- kind: Feature
name: last_5_click_posts
group-name: user-click
value-type: string
description: user last 5 click posts
- kind: Feature
name: number_of_user_started_posts
group-name: user-click
value-type: int64
description: number of posts that users stared today
'

actual=$(oomcli get meta feature -o yaml)
Expand Down
7 changes: 7 additions & 0 deletions oomcli/test/test_get_meta_group.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ items:
entity-name: user
category: stream
description: user click post feature
features:
- name: last_5_click_posts
value-type: string
description: user last 5 click posts
- name: number_of_user_started_posts
value-type: int64
description: number of posts that users stared today
'
actual=$(oomcli get meta group -o yaml)
assert_eq "$case" "$expected" "$actual"
16 changes: 16 additions & 0 deletions oomcli/test/test_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
SDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) && cd "$SDIR" || exit 1
source ./util.sh

init_store
register_features

case="push stream feature"
expected='
user,user-click.last_5_click_posts,user-click.number_of_user_started_posts
1,"1,2,3,4,5",10
'
oomcli push --entity-key 1 --group user-click --features last_5_click_posts=1,2,3,4,5 --features number_of_user_started_posts=10

actual=$(oomcli get online --feature user-click.last_5_click_posts,user-click.number_of_user_started_posts -k 1 -o csv)
assert_eq "$case" "$expected" "$actual"
3 changes: 3 additions & 0 deletions oomcli/test/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ register_features() {
oomcli register feature price --group phone --value-type "int64" --description "price"
oomcli register feature model --group phone --value-type "string" --description "model"
oomcli register feature age --group student --value-type "int64" --description "age"

oomcli register feature last_5_click_posts --group user-click --value-type "string" --description "user last 5 click posts"
oomcli register feature number_of_user_started_posts --group user-click --value-type "int64" --description "number of posts that users stared today"
}

# import sample data
Expand Down

0 comments on commit ae7b96b

Please sign in to comment.