Skip to content

Commit

Permalink
feat(featctl/cmd): add format for command get historical feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jinghancc committed Nov 4, 2021
1 parent 74a608d commit 106e85a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
7 changes: 6 additions & 1 deletion featctl/cmd/get_historical_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var getHistoricalFeatureOpt types.ExportFeatureValuesOpt
var getHistoricalFeatureOutput *string

var getHistoricalFeatureCmd = &cobra.Command{
Use: "historical-feature",
Expand All @@ -20,13 +21,16 @@ var getHistoricalFeatureCmd = &cobra.Command{
if !cmd.Flags().Changed("limit") {
getHistoricalFeatureOpt.Limit = nil
}
if !cmd.Flags().Changed("output") {
getHistoricalFeatureOutput = stringPtr(ASCIITable)
}
},
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
oomStore := mustOpenOomStore(ctx, oomStoreCfg)
defer oomStore.Close()

if err := getHistoricalFeature(ctx, oomStore, getHistoricalFeatureOpt); err != nil {
if err := getHistoricalFeature(ctx, oomStore, getHistoricalFeatureOpt, *getHistoricalFeatureOutput); err != nil {
log.Fatalf("failed exporting features: %v\n", err)
}
},
Expand All @@ -44,4 +48,5 @@ func init() {

getHistoricalFeatureOpt.Limit = flags.Uint64P("limit", "l", 0, "max records to export")
getHistoricalFeatureOpt.GroupRevision = flags.Int64P("revision", "r", 0, "feature group revision")
getHistoricalFeatureOutput = flags.StringP("output", "o", "", "output format")
}
40 changes: 38 additions & 2 deletions featctl/cmd/get_historical_feature_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,39 @@ package cmd
import (
"context"
"encoding/csv"
"fmt"
"os"

"github.com/olekukonko/tablewriter"
"github.com/oom-ai/oomstore/pkg/oomstore"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
"github.com/spf13/cast"
)

func getHistoricalFeature(ctx context.Context, store *oomstore.OomStore, opt types.ExportFeatureValuesOpt) error {
func getHistoricalFeature(ctx context.Context, store *oomstore.OomStore, opt types.ExportFeatureValuesOpt, output string) error {
fields, stream, err := store.ExportFeatureValues(ctx, opt)
if err != nil {
return err
}

if err := printHistoricalFeatures(fields, stream, output); err != nil {
return fmt.Errorf("failed printing historical features: %+v", err)
}
return nil
}

func printHistoricalFeatures(fields []string, stream <-chan *types.RawFeatureValueRecord, output string) error {
switch output {
case CSV:
return printHistoricalFeaturesInCSV(fields, stream)
case ASCIITable:
return printHistoricalFeaturesInASCIITable(fields, stream)
default:
return fmt.Errorf("unsupported output format %s", output)
}
}

func printHistoricalFeaturesInCSV(fields []string, stream <-chan *types.RawFeatureValueRecord) error {
w := csv.NewWriter(os.Stdout)
defer w.Flush()

Expand All @@ -30,5 +50,21 @@ func getHistoricalFeature(ctx context.Context, store *oomstore.OomStore, opt typ
return err
}
}
return err
return nil
}

func printHistoricalFeaturesInASCIITable(fields []string, stream <-chan *types.RawFeatureValueRecord) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(fields)
table.SetAutoFormatHeaders(false)

for item := range stream {
if item.Error != nil {
return item.Error
}
table.Append(cast.ToStringSlice(item.Record))
}

table.Render()
return nil
}
26 changes: 13 additions & 13 deletions featctl/test/test_get_historical_feature.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ expected='device,price
8,6500
9,4500
'
#actual=$(featctl get historical-feature --group phone --feature price)
#assert_eq "$case" "$expected" "$actual"
#
#case='get with limit'
#expected='device,price
#1,3999
#2,5299
#3,3999
#4,1999
#5,999
#'
#actual=$(featctl get historical-feature --group phone --feature price --limit 5)
#assert_eq "$case" "$expected" "$actual"
actual=$(featctl get historical-feature --group phone --feature price -o csv)
assert_eq "$case" "$expected" "$actual"

case='get with limit'
expected='device,price
1,3999
2,5299
3,3999
4,1999
5,999
'
actual=$(featctl get historical-feature --group phone --feature price --limit 5 -o csv)
assert_eq "$case" "$expected" "$actual"

0 comments on commit 106e85a

Please sign in to comment.