Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/cmd/conversations/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ type ListOptions struct {
SetFeedback bool
IncludeImpactAnalytics bool
SetImpactAnalytics bool
FeedbackVote int32
SetFeedbackVote bool
Clicked bool
SetClicked bool
Converted bool
SetConverted bool
HasAlgoliaSearch bool
SetHasAlgoliaSearch bool
}

// NewListCmd returns the `conversations list` command.
Expand Down Expand Up @@ -57,11 +65,27 @@ func NewListCmd(f *cmdutil.Factory) *cobra.Command {

# Include impact analytics
$ algolia agentstudio conversations list 11111111-1111-4111-8111-111111111111 --include-impact-analytics

# Filter conversations by feedback and impact
$ algolia agentstudio conversations list 11111111-1111-4111-8111-111111111111 --include-feedback --feedback-vote 1 --include-impact-analytics --clicked
`),
RunE: func(cmd *cobra.Command, args []string) error {
opts.AgentID = args[0]
opts.SetFeedback = cmd.Flags().Changed("include-feedback")
opts.SetImpactAnalytics = cmd.Flags().Changed("include-impact-analytics")
opts.SetFeedbackVote = cmd.Flags().Changed("feedback-vote")
opts.SetClicked = cmd.Flags().Changed("clicked")
opts.SetConverted = cmd.Flags().Changed("converted")
opts.SetHasAlgoliaSearch = cmd.Flags().Changed("has-algolia-search")
if opts.SetFeedbackVote && (opts.FeedbackVote < 0 || opts.FeedbackVote > 1) {
return cmdutil.FlagErrorf("--feedback-vote must be 0 or 1")
}
if opts.SetFeedbackVote && !opts.IncludeFeedback {
return cmdutil.FlagErrorf("--feedback-vote requires --include-feedback")
}
if (opts.SetClicked || opts.SetConverted || opts.SetHasAlgoliaSearch) && !opts.IncludeImpactAnalytics {
return cmdutil.FlagErrorf("impact filters require --include-impact-analytics")
}
return runListCmd(opts)
},
}
Expand All @@ -73,6 +97,10 @@ func NewListCmd(f *cmdutil.Factory) *cobra.Command {
cmd.Flags().
BoolVar(&opts.IncludeFeedback, "include-feedback", false, "Include feedback per conversation")
cmd.Flags().BoolVar(&opts.IncludeImpactAnalytics, "include-impact-analytics", false, "Include impact analytics per conversation")
cmd.Flags().Int32Var(&opts.FeedbackVote, "feedback-vote", 0, "Filter by feedback vote (0 or 1; requires --include-feedback)")
cmd.Flags().BoolVar(&opts.Clicked, "clicked", false, "Filter by item clicks (requires --include-impact-analytics)")
cmd.Flags().BoolVar(&opts.Converted, "converted", false, "Filter by conversions (requires --include-impact-analytics)")
cmd.Flags().BoolVar(&opts.HasAlgoliaSearch, "has-algolia-search", false, "Filter by search tool use (requires --include-impact-analytics)")

opts.PrintFlags.AddFlags(cmd)
return cmd
Expand Down Expand Up @@ -108,6 +136,18 @@ func runListCmd(opts *ListOptions) error {
if opts.SetImpactAnalytics {
req = req.WithIncludeImpactAnalytics(opts.IncludeImpactAnalytics)
}
if opts.SetFeedbackVote {
req = req.WithFeedbackVote(opts.FeedbackVote)
}
if opts.SetClicked {
req = req.WithClicked(opts.Clicked)
}
if opts.SetConverted {
req = req.WithConverted(opts.Converted)
}
if opts.SetHasAlgoliaSearch {
req = req.WithHasAlgoliaSearch(opts.HasAlgoliaSearch)
}

opts.IO.StartProgressIndicatorWithLabel("Fetching conversations")

Expand Down
56 changes: 56 additions & 0 deletions pkg/cmd/conversations/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,59 @@ func TestListConversations_ImpactAnalyticsAPIError(t *testing.T) {
require.Len(t, r.Requests, 1)
assert.Equal(t, "true", r.Requests[0].URL.Query().Get("includeImpactAnalytics"))
}

func TestListConversations_Filters(t *testing.T) {
tests := []struct {
name string
flags string
want map[string]string
absent []string
}{
{name: "omitted", absent: []string{"feedbackVote", "clicked", "converted", "hasAlgoliaSearch"}},
{name: "positive votes and impact", flags: " --include-feedback --feedback-vote 1 --include-impact-analytics --clicked --converted --has-algolia-search --page 2 --limit 20", want: map[string]string{"includeFeedback": "true", "feedbackVote": "1", "includeImpactAnalytics": "true", "clicked": "true", "converted": "true", "hasAlgoliaSearch": "true", "page": "2", "limit": "20"}},
{name: "zero vote and false impact", flags: " --include-feedback --feedback-vote 0 --include-impact-analytics --clicked=false --converted=false --has-algolia-search=false", want: map[string]string{"feedbackVote": "0", "clicked": "false", "converted": "false", "hasAlgoliaSearch": "false"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &httpmock.Registry{}
r.Register(httpmock.REST("GET", listPath), httpmock.StringResponse(emptyListBody))
f, out := test.NewFactory(false, r, nil, "")
_, err := test.Execute(list.NewListCmd(f), agentID+tt.flags, out)
require.NoError(t, err)
r.Verify(t)
require.Len(t, r.Requests, 1)
q := r.Requests[0].URL.Query()
for key, value := range tt.want {
assert.Equal(t, []string{value}, q[key], "query %s", key)
}
for _, key := range tt.absent {
assert.False(t, q.Has(key), "unexpected query %s", key)
}
assert.JSONEq(t, emptyListBody, strings.TrimSpace(out.String()))
})
}
}

func TestListConversations_InvalidFilters(t *testing.T) {
tests := []struct{ flags, message string }{
{" --feedback-vote 1", "feedback-vote requires --include-feedback"},
{" --include-feedback=false --feedback-vote 0", "feedback-vote requires --include-feedback"},
{" --include-feedback --feedback-vote -1", "feedback-vote must be 0 or 1"},
{" --include-feedback --feedback-vote 2", "feedback-vote must be 0 or 1"},
{" --include-feedback --feedback-vote invalid", "invalid"},
{" --clicked", "require --include-impact-analytics"},
{" --converted=false", "require --include-impact-analytics"},
{" --include-impact-analytics=false --has-algolia-search=false", "require --include-impact-analytics"},
{" --include-impact-analytics --clicked=invalid", "invalid"},
}
for _, tt := range tests {
t.Run(tt.flags, func(t *testing.T) {
r := &httpmock.Registry{}
f, out := test.NewFactory(false, r, nil, "")
_, err := test.Execute(list.NewListCmd(f), agentID+tt.flags, out)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.message)
assert.Empty(t, r.Requests)
})
}
}
Loading