Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 13 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type ClientInterface interface {
IsHealthy() bool
GetTask(taskUID int64) (resp *Task, err error)
GetTasks(param *TasksQuery) (resp *TaskResult, err error)
CancelTasks(param *TasksQuery) (resp *TaskInfo, err error)
CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error)
WaitForTask(taskUID int64, options ...WaitParams) (*Task, error)
GenerateTenantToken(APIKeyUID string, searchRules map[string]interface{}, options *TenantTokenOptions) (resp string, err error)
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (c *Client) GetTasks(param *TasksQuery) (resp *TaskResult, err error) {
return resp, nil
}

func (c *Client) CancelTasks(param *TasksQuery) (resp *TaskInfo, err error) {
func (c *Client) CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/tasks/cancel",
Expand All @@ -298,7 +298,17 @@ func (c *Client) CancelTasks(param *TasksQuery) (resp *TaskInfo, err error) {
functionName: "CancelTasks",
}
if param != nil {
encodeTasksQuery(param, &req)
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
}
encodeTasksQuery(paramToSend, &req)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this function encodeTasksQuery support multiple sets of params?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, I could put interface{} as a parameter instead of a typed struct, but I wouldn't have access to the fields easily.

}
if err := c.executeRequest(req); err != nil {
return nil, err
Expand Down
68 changes: 36 additions & 32 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,19 +833,28 @@ func TestClient_CancelTasks(t *testing.T) {
type args struct {
UID string
client *Client
query *TasksQuery
query *CancelTasksQuery
}
tests := []struct {
name string
args args
want string
}{
{
name: "TestBasicCancelTasks",
name: "TestCancelTasksWithNoFilters",
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: nil,
},
want: "",
},
{
name: "TestCancelTasksWithStatutes",
args: args{
UID: "indexUID",
client: defaultClient,
query: &CancelTasksQuery{
Statuses: []string{"succeeded"},
},
},
Expand All @@ -856,7 +865,7 @@ func TestClient_CancelTasks(t *testing.T) {
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: &CancelTasksQuery{
IndexUIDS: []string{"0"},
},
},
Expand All @@ -867,7 +876,7 @@ func TestClient_CancelTasks(t *testing.T) {
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: &CancelTasksQuery{
IndexUIDS: []string{"0", "1"},
},
},
Expand All @@ -878,7 +887,7 @@ func TestClient_CancelTasks(t *testing.T) {
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: &CancelTasksQuery{
UIDS: []int64{0},
},
},
Expand All @@ -889,7 +898,7 @@ func TestClient_CancelTasks(t *testing.T) {
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: &CancelTasksQuery{
UIDS: []int64{0, 1},
},
},
Expand All @@ -900,29 +909,18 @@ func TestClient_CancelTasks(t *testing.T) {
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: &CancelTasksQuery{
BeforeEnqueuedAt: time.Now(),
},
},
want: strings.NewReplacer(":", "%3A").Replace("?beforeEnqueuedAt=" + time.Now().Format("2006-01-02T15:04:05Z")),
},
{
name: "TestCancelTasksWithCanceledByFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
CanceledBy: []int64{1},
},
},
want: "?canceledBy=1",
},
{
name: "TestCancelTasksWithParameters",
args: args{
UID: "indexUID",
client: defaultClient,
query: &TasksQuery{
query: &CancelTasksQuery{
Statuses: []string{"enqueued"},
IndexUIDS: []string{"indexUID"},
UIDS: []int64{1},
Expand All @@ -938,21 +936,27 @@ func TestClient_CancelTasks(t *testing.T) {
t.Cleanup(cleanup(c))

gotResp, err := c.CancelTasks(tt.args.query)
require.NoError(t, err)
if tt.args.query == nil {
require.Error(t, err)
require.Equal(t, "missing_task_filters",
err.(*Error).MeilisearchApiError.Code)
} else {
require.NoError(t, err)

_, err = c.WaitForTask(gotResp.TaskUID)
require.NoError(t, err)
_, err = c.WaitForTask(gotResp.TaskUID)
require.NoError(t, err)

gotTask, err := c.GetTask(gotResp.TaskUID)
require.NoError(t, err)
gotTask, err := c.GetTask(gotResp.TaskUID)
require.NoError(t, err)

require.NotNil(t, gotResp.Status)
require.NotNil(t, gotResp.Type)
require.NotNil(t, gotResp.TaskUID)
require.NotNil(t, gotResp.EnqueuedAt)
require.Equal(t, "", gotResp.IndexUID)
require.Equal(t, "taskCancelation", gotResp.Type)
require.Equal(t, tt.want, gotTask.Details.OriginalFilter)
require.NotNil(t, gotResp.Status)
require.NotNil(t, gotResp.Type)
require.NotNil(t, gotResp.TaskUID)
require.NotNil(t, gotResp.EnqueuedAt)
require.Equal(t, "", gotResp.IndexUID)
require.Equal(t, "taskCancelation", gotResp.Type)
require.Equal(t, tt.want, gotTask.Details.OriginalFilter)
}
})
}
}
Expand Down
14 changes: 13 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type TaskInfo struct {
EnqueuedAt time.Time `json:"enqueuedAt"`
}

// TasksQuery is the request body for list documents method
// TasksQuery is a list of filter available to send as query parameters
type TasksQuery struct {
UIDS []int64
Limit int64
Expand All @@ -161,6 +161,18 @@ type TasksQuery struct {
AfterFinishedAt time.Time
}

// CancelTasksQuery is a list of filter available to send as query parameters
type CancelTasksQuery struct {
UIDS []int64
IndexUIDS []string
Statuses []string
Types []string
BeforeEnqueuedAt time.Time
AfterEnqueuedAt time.Time
BeforeStartedAt time.Time
AfterStartedAt time.Time
}
Comment on lines +165 to +174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type CancelTasksQuery struct {
UIDS []int64
IndexUIDS []string
Statuses []string
Types []string
BeforeEnqueuedAt time.Time
AfterEnqueuedAt time.Time
BeforeStartedAt time.Time
AfterStartedAt time.Time
}
type CancelTasksQuery struct {
UIDS []int64
IndexUIDS []string
Statuses []string
Types []string
BeforeEnqueuedAt time.Time
AfterEnqueuedAt time.Time
BeforeStartedAt time.Time
AfterStartedAt time.Time
BeforeFinishedAt time.Time
AfterFinishedAt time.Time
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add those two filters because they didn't make sense, and they are not in the parameters available in the documentation. Are you sure it's a good think to add it?


type Details struct {
ReceivedDocuments int64 `json:"receivedDocuments,omitempty"`
IndexedDocuments int64 `json:"indexedDocuments,omitempty"`
Expand Down
Loading