Skip to content

Commit

Permalink
Add tasks filters for v0.30 (#1376)
Browse files Browse the repository at this point in the history
* Add new filters on get tasks for v0.30 of Meilisearch

* Revert last commit

* Add new filters on get tasks for v0.30 of Meilisearch

* Add tests for beforeEnqueuedAt and afterEnqueuedAt

* Add date formating on beforeEnqueuedAt and afterEnqueuedAt

* Add date formating of beforeStartedAT and afterStartedAt

* Add tests for beforeStartedAt and afterStartedAt

* Add beforeFinishedAt and AfterFinished At

* Add test on uid filter

* Add uid filter parsing

* Add new possible error codes

* Add consistency in getTasks method

* Move serializing of query params to http request file

* Simplify toQueryParams

* Remove unused import

* Improve sleeping time to avoid flacky test

* Add comments above tests
  • Loading branch information
bidoubiwa authored Nov 11, 2022
1 parent 1701947 commit 27ce152
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 12 deletions.
21 changes: 20 additions & 1 deletion src/http-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import {

import { addTrailingSlash, addProtocolIfNotPresent } from './utils'

type queryParams<T> = { [key in keyof T]: string }

function toQueryParams<T extends object>(parameters: T): queryParams<T> {
const params = Object.keys(parameters) as Array<keyof T>

const queryParams = params.reduce<queryParams<T>>((acc, key) => {
const value = parameters[key]
if (value === undefined) {
return acc
} else if (Array.isArray(value)) {
return { ...acc, [key]: value.join(',') }
} else if (value instanceof Date) {
return { ...acc, [key]: value.toISOString() }
}
return { ...acc, [key]: value }
}, {} as queryParams<T>)
return queryParams
}

function constructHostURL(host: string): string {
try {
host = addProtocolIfNotPresent(host)
Expand Down Expand Up @@ -219,4 +238,4 @@ class HttpRequests {
}
}

export { HttpRequests }
export { HttpRequests, toQueryParams }
14 changes: 3 additions & 11 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
TaskObject,
TasksResultsObject,
} from './types'
import { HttpRequests } from './http-requests'
import { removeUndefinedFromObject, sleep } from './utils'
import { HttpRequests, toQueryParams } from './http-requests'
import { sleep } from './utils'

class Task {
indexUid: TaskObject['indexUid']
Expand Down Expand Up @@ -70,17 +70,9 @@ class TaskClient {
async getTasks(parameters: TasksQuery = {}): Promise<TasksResults> {
const url = `tasks`

const queryParams = {
indexUid: parameters?.indexUid?.join(','),
type: parameters?.type?.join(','),
status: parameters?.status?.join(','),
from: parameters.from,
limit: parameters.limit,
}

const tasks = await this.httpRequest.get<Promise<TasksResultsObject>>(
url,
removeUndefinedFromObject(queryParams)
toQueryParams<TasksQuery>(parameters)
)

return {
Expand Down
16 changes: 16 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,15 @@ export const enum TaskTypes {

export type TasksQuery = {
indexUid?: string[]
uid?: number[]
type?: TaskTypes[]
status?: TaskStatus[]
beforeEnqueuedAt?: Date
afterEnqueuedAt?: Date
beforeStartedAt?: Date
afterStartedAt?: Date
beforeFinishedAt?: Date
afterFinishedAt?: Date
limit?: number
from?: number
}
Expand Down Expand Up @@ -513,6 +520,15 @@ export const enum ErrorStatusCode {

/** @see https://docs.meilisearch.com/errors/#dump_not_found */
DUMP_NOT_FOUND = 'dump_not_found',

/** @see https://docs.meilisearch.com/errors/#invalid_task_uid */
INVALID_TASK_UID = 'invalid_task_uid',

/** @see https://docs.meilisearch.com/errors/#invalid_task_date */
INVALID_TASK_DATE = 'invalid_task_date',

/** @see https://docs.meilisearch.com/errors/#missing_task_filters */
MISSING_TASK_FILTERS = 'missing_task_filters',
}

export type TokenIndexRules = {
Expand Down
140 changes: 140 additions & 0 deletions tests/task.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorStatusCode, TaskTypes, TaskStatus } from '../src/types'
import { sleep } from '../src/utils'
import {
clearAllIndexes,
config,
Expand Down Expand Up @@ -82,6 +83,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(task.uid).toEqual(enqueuedTask.taskUid)
})

// get tasks
test(`${permission} key: Get all tasks`, async () => {
const client = await getClient(permission)
const enqueuedTask = await client
Expand All @@ -95,6 +97,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(tasks.results[0].uid).toEqual(enqueuedTask.taskUid)
})

// get tasks: type
test(`${permission} key: Get all tasks with type filter`, async () => {
const client = await getClient(permission)
await client.index(index.uid).addDocuments([{ id: 1 }])
Expand All @@ -114,6 +117,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(onlyDocumentAddition.size).toEqual(2)
})

// get tasks: type
test(`${permission} key: Get all tasks with type filter on an index`, async () => {
const client = await getClient(permission)
await client.deleteIndex(index2.uid)
Expand All @@ -135,6 +139,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(onlyDocumentAddition.size).toEqual(2)
})

// get tasks: pagination
test(`${permission} key: Get all tasks with pagination`, async () => {
const client = await getClient(permission)
const task1 = await client.index(index.uid).addDocuments([{ id: 1 }])
Expand All @@ -150,6 +155,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(tasks.next).toEqual(0)
})

// get tasks: status
test(`${permission} key: Get all tasks with status filter`, async () => {
const client = await getClient(permission)
const task1 = await client.index(index.uid).addDocuments([{ id: 1 }])
Expand All @@ -167,6 +173,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(onlySuccesfullTasks.size).toEqual(2)
})

// get tasks: status
test(`${permission} key: Get all tasks with status filter on an index`, async () => {
const client = await getClient(permission)
const task1 = await client.index(index.uid).addDocuments([{ id: 1 }])
Expand All @@ -190,6 +197,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(onlyTaskWithSameUid.size).toEqual(1)
})

// get tasks: indexUid
test(`${permission} key: Get all tasks with indexUid filter`, async () => {
const client = await getClient(permission)
await client.index(index.uid).addDocuments([{ id: 1 }])
Expand All @@ -206,6 +214,138 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(onlyTaskWithSameUid.size).toEqual(2)
})

// get tasks: uid
test(`${permission} key: Get all tasks with uid filter`, async () => {
const client = await getClient(permission)
const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])

const tasks = await client.getTasks({
uid: [taskUid],
})

expect(tasks.results[0].uid).toEqual(taskUid)
})

// get tasks: beforeEnqueuedAt
test(`${permission} key: Get all tasks with beforeEnqueuedAt filter`, async () => {
const client = await getClient(permission)
const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
await sleep(1) // in ms

const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])

const tasks = await client.getTasks({
beforeEnqueuedAt: currentTime,
})
const tasksUids = tasks.results.map((t) => t.uid)

expect(tasksUids.includes(taskUid)).toBeFalsy()
})

// get tasks: afterEnqueuedAt
test(`${permission} key: Get all tasks with afterEnqueuedAt filter`, async () => {
const client = await getClient(permission)
const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])
await sleep(2) // in ms

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)

const tasks = await client.getTasks({
afterEnqueuedAt: currentTime,
})
const tasksUids = tasks.results.map((t) => t.uid)

expect(tasksUids.includes(taskUid)).toBeFalsy()
})

// get tasks: beforeStartedAt
test(`${permission} key: Get all tasks with beforeStartedAt filter`, async () => {
const client = await getClient(permission)
const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
await sleep(1) // in ms

const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])
await client.index(index.uid).waitForTask(taskUid) // ensures the tasks has a `startedAt` value

const tasks = await client.getTasks({
beforeStartedAt: currentTime,
})
const tasksUids = tasks.results.map((t) => t.uid)

expect(tasksUids.includes(taskUid)).toBeFalsy()
})

// get tasks: afterStartedAt
test(`${permission} key: Get all tasks with afterStartedAt filter`, async () => {
const client = await getClient(permission)
const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])
await client.index(index.uid).waitForTask(taskUid) // ensures the tasks has a `startedAt` value
await sleep(1) // in ms

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)

const tasks = await client.getTasks({
afterStartedAt: currentTime,
})
const tasksUids = tasks.results.map((t) => t.uid)

expect(tasksUids.includes(taskUid)).toBeFalsy()
})

// get tasks: beforeFinishedAt
test(`${permission} key: Get all tasks with beforeFinishedAt filter`, async () => {
const client = await getClient(permission)
const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
await sleep(1) // in ms

const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])
await client.index(index.uid).waitForTask(taskUid) // ensures the tasks has a `finishedAt` value

const tasks = await client.getTasks({
beforeFinishedAt: currentTime,
})
const tasksUids = tasks.results.map((t) => t.uid)

expect(tasksUids.includes(taskUid)).toBeFalsy()
})

// get tasks: afterFinishedAt
test(`${permission} key: Get all tasks with afterFinishedAt filter`, async () => {
const client = await getClient(permission)
const { taskUid } = await client
.index(index.uid)
.addDocuments([{ id: 1 }])
await client.index(index.uid).waitForTask(taskUid) // ensures the tasks has a `finishedAt` value
await sleep(1) // in ms

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)

const tasks = await client.getTasks({
afterFinishedAt: currentTime,
})
const tasksUids = tasks.results.map((t) => t.uid)

expect(tasksUids.includes(taskUid)).toBeFalsy()
})

test(`${permission} key: Get all indexes tasks with index instance`, async () => {
const client = await getClient(permission)
await client.index(index.uid).addDocuments([{ id: 1 }])
Expand Down

0 comments on commit 27ce152

Please sign in to comment.