Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
jdogmcsteezy committed Aug 23, 2024
1 parent 0361072 commit 0715019
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 39 deletions.
32 changes: 17 additions & 15 deletions cmd/indexCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ var indexCreateFlags = &struct {
func newIndexCreateFlagSet() *pflag.FlagSet {
flagSet := &pflag.FlagSet{}
flagSet.BoolVarP(&indexCreateFlags.yes, flags.Yes, "y", false, "When true do not prompt for confirmation.")
flagSet.StringVar(&indexCreateFlags.inputFile, flags.InputFile, StdIn, "A yaml file containing IndexDefinitions created using \"asvec index list --yaml\"") //nolint:lll // For readability
flagSet.StringVarP(&indexCreateFlags.namespace, flags.Namespace, flags.NamespaceShort, "", "The namespace for the index.") //nolint:lll // For readability
flagSet.VarP(&indexCreateFlags.set, flags.Set, flags.SetShort, "The sets for the index.") //nolint:lll // For readability
flagSet.Var(&indexCreateFlags.set, "sets", "The sets for the index.")
flagSet.MarkHidden("sets") // mitigate breaking change //nolint:lll // For readability
flagSet.StringVar(&indexCreateFlags.inputFile, flags.InputFile, StdIn, "A yaml file containing IndexDefinitions created using \"asvec index list --yaml\"") //nolint:lll // For readability
flagSet.StringVarP(&indexCreateFlags.namespace, flags.Namespace, flags.NamespaceShort, "", "The namespace for the index.") //nolint:lll // For readability
flagSet.VarP(&indexCreateFlags.set, flags.Set, flags.SetShort, "The sets for the index.") //nolint:lll // For readability //nolint:lll // For readability
flagSet.StringVarP(&indexCreateFlags.indexName, flags.IndexName, flags.IndexNameShort, "", "The name of the index.") //nolint:lll // For readability
flagSet.StringVarP(&indexCreateFlags.vectorField, flags.VectorField, "f", "", "The name of the vector field.") //nolint:lll // For readability
flagSet.Uint32VarP(&indexCreateFlags.dimensions, flags.Dimension, "d", 0, "The dimension of the vector field.") //nolint:lll // For readability
Expand All @@ -80,6 +78,13 @@ func newIndexCreateFlagSet() *pflag.FlagSet {
flagSet.AddFlagSet(indexCreateFlags.hnswHealer.NewFlagSet())
flagSet.AddFlagSet(indexCreateFlags.hnswMerge.NewFlagSet())

flagSet.Var(&indexCreateFlags.set, "sets", "The sets for the index.")
err := flagSet.MarkHidden("sets")

if err != nil {
panic(err)
}

return flagSet
}

Expand Down Expand Up @@ -128,8 +133,7 @@ asvec index create -i myindex -n test -s testset -d 256 -m COSINE --%s vector \

if !oneRequiredFlagsSet {
var data []byte
// ioReader := os.Stdin
readInput := false

if indexCreateFlags.inputFile != StdIn {
logger.Info("reading input file")
r, err := os.Open(indexCreateFlags.inputFile)
Expand All @@ -139,38 +143,36 @@ asvec index create -i myindex -n test -s testset -d 256 -m COSINE --%s vector \
}

defer r.Close()
// ioReader = r

data, err = io.ReadAll(r)
if err != nil {
logger.Error("failed to read index definitions", slog.Any("error", err))
return err
}

readInput = true
} else {
logger.Debug("attempting to read index definitions from stdin")
logger.Debug("checking if index definitions are being piped to stdin")

stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
logger.Debug("stdin is from a pipe")

data, err = io.ReadAll(os.Stdin)
if err != nil {
logger.Error("failed to read index definitions from stdin", slog.Any("error", err))
return err
}

readInput = true
} else {
logger.Debug("no data is being piped to stdin")
}
}

if readInput {
if data != nil {
logger.Debug("read index definitions", slog.Any("data", data))

// Unmarshal YAML data
intermediate := map[string]interface{}{}
err = yaml.Unmarshal([]byte(data), &intermediate)
err = yaml.Unmarshal(data, &intermediate)

if err != nil {
logger.Error("failed to unmarshal index definitions to untyped map", slog.Any("error", err))
return err
Expand Down
45 changes: 32 additions & 13 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ var queryFlags = &struct {
}

const (
defaultMaxResults = 5
defaultMaxDataKeys = 5
defaultMaxResults = 5
defaultMaxDataKeys = 5
failedToRunVectorSearchErrMsg = "failed to run vector search"
)

func newQueryFlagSet() *pflag.FlagSet {
Expand Down Expand Up @@ -200,14 +201,23 @@ asvec query -i my-index -n my-namespace -v "[1,0,1,0,0,0,1,0,1,1]" --max-width 1
view.Printf("Hint: To increase the number of records returned, use the --%s flag.", flags.MaxResults)

if !viper.IsSet(flags.Fields) {
view.Printf("Hint: To choose which record keys are displayed, use the --%s flag. By default only %d are displayed.", flags.Fields, defaultMaxDataKeys)
view.Printf(
"Hint: To choose which record keys are displayed, use the --%s flag. By default only %d are displayed.",
flags.Fields,
defaultMaxDataKeys,
)
}
}
},
}
}

func queryVectorByKey(ctx context.Context, client *avs.Client, indexDef *protos.IndexDefinition, hnswSearchParams *protos.HnswSearchParams) ([]*avs.Neighbor, error) {
func queryVectorByKey(
ctx context.Context,
client *avs.Client,
indexDef *protos.IndexDefinition,
hnswSearchParams *protos.HnswSearchParams,
) ([]*avs.Neighbor, error) {
logger := logger.With(
slog.String("key", queryFlags.key),
slog.String("index", queryFlags.indexName),
Expand All @@ -229,7 +239,10 @@ func queryVectorByKey(ctx context.Context, client *avs.Client, indexDef *protos.
logger.ErrorContext(ctx, msg, slog.Any("error", err))

if set == nil {
view.Warningf("The requested record was not found. If the record is in a set, use may also need to provide the --%s flag.", flags.Set)
view.Warningf(
"The requested record was not found. If the record is in a set, use may also need to provide the --%s flag.",
flags.Set,
)
}

return nil, fmt.Errorf("%s: %w", msg, err)
Expand All @@ -241,6 +254,7 @@ func queryVectorByKey(ctx context.Context, client *avs.Client, indexDef *protos.
if !ok {
msg := "field not found in specified record"
logger.ErrorContext(ctx, msg, slog.String("field", indexDef.Field), slog.Any("data", record.Data))

return nil, fmt.Errorf("%s: %w", msg, err)
}

Expand Down Expand Up @@ -273,14 +287,15 @@ func queryVectorByKey(ctx context.Context, client *avs.Client, indexDef *protos.
}

if err != nil {
msg := "failed to run vector search"
logger.ErrorContext(ctx, msg, slog.Any("error", err))
logger.ErrorContext(ctx, failedToRunVectorSearchErrMsg, slog.Any("error", err))
view.Errorf("Unable to run vector query: %s", err)

return nil, err
}

// Remove the queried vector from the results
newNeighbors := make([]*avs.Neighbor, 0, len(neighbors)-1)

for _, n := range neighbors {
if n.Key != queryFlags.key {
newNeighbors = append(newNeighbors, n)
Expand All @@ -292,7 +307,12 @@ func queryVectorByKey(ctx context.Context, client *avs.Client, indexDef *protos.
return neighbors, nil
}

func trialAndErrorQuery(ctx context.Context, client *avs.Client, dimension int, hnswSearchParams *protos.HnswSearchParams) ([]*avs.Neighbor, error) {
func trialAndErrorQuery(
ctx context.Context,
client *avs.Client,
dimension int,
hnswSearchParams *protos.HnswSearchParams,
) ([]*avs.Neighbor, error) {
logger := logger.With(
slog.String("index", queryFlags.indexName),
slog.String("namespace", queryFlags.namespace),
Expand All @@ -310,9 +330,9 @@ func trialAndErrorQuery(ctx context.Context, client *avs.Client, dimension int,
queryFlags.includeFields,
nil,
)

if err != nil {
msg := "failed to run vector search"
logger.WarnContext(ctx, msg, slog.Any("error", err))
logger.WarnContext(ctx, failedToRunVectorSearchErrMsg, slog.Any("error", err))
}

if err != nil || len(neighbors) == 0 {
Expand All @@ -329,9 +349,9 @@ func trialAndErrorQuery(ctx context.Context, client *avs.Client, dimension int,
)

if err != nil {
msg := "failed to run vector search"
logger.WarnContext(ctx, msg, slog.Any("error", err))
logger.WarnContext(ctx, failedToRunVectorSearchErrMsg, slog.Any("error", err))
view.Errorf("Unable to run vector query: %s", err)

return nil, err
}
}
Expand All @@ -353,5 +373,4 @@ func init() {
}

queryCmd.MarkFlagsMutuallyExclusive(flags.Vector, flags.Key)

}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var view = NewView(os.Stdout, os.Stderr, logger)
var Version = "development" // Overwritten at build time by ld_flags

var rootFlags = &struct {
clientFlags *flags.ClientFlags
logLevel flags.LogLevelFlag
noColor bool
clientFlags *flags.ClientFlags
}{
clientFlags: flags.NewClientFlags(),
}
Expand Down
1 change: 0 additions & 1 deletion cmd/userDrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/spf13/pflag"
)

//nolint:govet // Padding not a concern for a CLI
var userDropFlags = &struct {
clientFlags *flags.ClientFlags
dropUser string
Expand Down
1 change: 0 additions & 1 deletion cmd/userNewPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/spf13/pflag"
)

//nolint:govet // Padding not a concern for a CLI
var userNewPassFlags = &struct {
clientFlags *flags.ClientFlags
username string
Expand Down
7 changes: 6 additions & 1 deletion cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ func (v *View) getNeighborTableWriter() *writers.NeighborTableWriter {
return writers.NewNeighborTableWriter(v.out, v.logger)
}

func (v *View) PrintQueryResults(neighbors []*avs.Neighbor, format int, maxDataKeys, maxDataValueColWidth int) {
func (v *View) PrintQueryResults(
neighbors []*avs.Neighbor,
format int,
maxDataKeys,
maxDataValueColWidth int,
) {
t := v.getNeighborTableWriter()

for _, n := range neighbors {
Expand Down
6 changes: 5 additions & 1 deletion cmd/writers/indexList.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func NewIndexTableWriter(writer io.Writer, verbose bool, logger *slog.Logger) *I
return &t
}

func (itw *IndexTableWriter) AppendIndexRow(index *protos.IndexDefinition, status *protos.IndexStatusResponse, format int) {
func (itw *IndexTableWriter) AppendIndexRow(
index *protos.IndexDefinition,
status *protos.IndexStatusResponse,
format int,
) {
row := table.Row{index.Id.Name, index.Id.Namespace, index.SetFilter, index.Field,
index.Dimensions, index.VectorDistanceMetric, status.GetUnmergedRecordCount()}

Expand Down
10 changes: 8 additions & 2 deletions cmd/writers/neighborList.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
)

//nolint:govet // Padding not a concern for a CLI
type NeighborTableWriter struct {
table table.Writer
logger *slog.Logger
Expand Down Expand Up @@ -53,7 +52,12 @@ func NewNeighborTableWriter(writer io.Writer, logger *slog.Logger) *NeighborTabl
return &t
}

func (itw *NeighborTableWriter) AppendNeighborRow(neighbor *avs.Neighbor, maxDataKeys, renderFormat, maxDataValueColWidth int) {
func (itw *NeighborTableWriter) AppendNeighborRow(
neighbor *avs.Neighbor,
maxDataKeys,
renderFormat,
maxDataValueColWidth int,
) {
row := table.Row{
neighbor.Namespace,
neighbor.Set,
Expand All @@ -74,6 +78,7 @@ func (itw *NeighborTableWriter) AppendNeighborRow(neighbor *avs.Neighbor, maxDat
},
},
)

keys := make([]string, 0, len(neighbor.Record.Data))

for key := range neighbor.Record.Data {
Expand All @@ -88,6 +93,7 @@ func (itw *NeighborTableWriter) AppendNeighborRow(neighbor *avs.Neighbor, maxDat

break
}

tData.AppendRow(table.Row{key, neighbor.Record.Data[key]})
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/writers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func formatEndpoints(nodeID uint64, nodeEndpoints map[uint64]*protos.ServerEndpo
return strings.Join(nodeToEndpointsStr, "\n")
}

func renderTable(table table.Writer, format int) string {
func renderTable(t table.Writer, format int) string {
if format == 0 {
return table.Render()
} else {
return table.RenderCSV()
return t.Render()
}

return t.RenderCSV()
}

0 comments on commit 0715019

Please sign in to comment.