-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1775 from meilisearch/feat/add-batch-routes
Add batch routes
- Loading branch information
Showing
7 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
Config, | ||
BatchObject, | ||
BatchesQuery, | ||
BatchesResults, | ||
BatchesResultsObject, | ||
} from "./types"; | ||
import { HttpRequests, toQueryParams } from "./http-requests"; | ||
|
||
class Batch { | ||
uid: BatchObject["uid"]; | ||
details: BatchObject["details"]; | ||
stats: BatchObject["stats"]; | ||
startedAt: BatchObject["startedAt"]; | ||
finishedAt: BatchObject["finishedAt"]; | ||
duration: BatchObject["duration"]; | ||
|
||
constructor(batch: BatchObject) { | ||
this.uid = batch.uid; | ||
this.details = batch.details; | ||
this.stats = batch.stats; | ||
this.startedAt = batch.startedAt; | ||
this.finishedAt = batch.finishedAt; | ||
this.duration = batch.duration; | ||
} | ||
} | ||
|
||
class BatchClient { | ||
httpRequest: HttpRequests; | ||
|
||
constructor(config: Config) { | ||
this.httpRequest = new HttpRequests(config); | ||
} | ||
|
||
/** | ||
* Get one batch | ||
* | ||
* @param uid - Unique identifier of the batch | ||
* @returns | ||
*/ | ||
async getBatch(uid: number): Promise<Batch> { | ||
const url = `batches/${uid}`; | ||
const batch = await this.httpRequest.get<BatchObject>(url); | ||
return new Batch(batch); | ||
} | ||
|
||
/** | ||
* Get batches | ||
* | ||
* @param parameters - Parameters to browse the batches | ||
* @returns Promise containing all batches | ||
*/ | ||
async getBatches(parameters: BatchesQuery = {}): Promise<BatchesResults> { | ||
const url = `batches`; | ||
|
||
const batches = await this.httpRequest.get<Promise<BatchesResultsObject>>( | ||
url, | ||
toQueryParams<BatchesQuery>(parameters), | ||
); | ||
|
||
return { | ||
...batches, | ||
results: batches.results.map((batch) => new Batch(batch)), | ||
}; | ||
} | ||
} | ||
|
||
export { BatchClient, Batch }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { afterAll, beforeEach, describe, expect, test } from "vitest"; | ||
import { | ||
config, | ||
getClient, | ||
clearAllIndexes, | ||
} from "./utils/meilisearch-test-utils"; | ||
|
||
const index = { | ||
uid: "batch-test", | ||
}; | ||
|
||
afterAll(() => { | ||
return clearAllIndexes(config); | ||
}); | ||
|
||
describe.each([{ permission: "Master" }, { permission: "Admin" }])( | ||
"Tests on batches", | ||
({ permission }) => { | ||
beforeEach(async () => { | ||
const client = await getClient("Master"); | ||
const { taskUid } = await client.createIndex(index.uid); | ||
await client.waitForTask(taskUid); | ||
}); | ||
|
||
test(`${permission} key: Get all batches`, async () => { | ||
const client = await getClient(permission); | ||
const batches = await client.getBatches(); | ||
expect(batches.results.length).toBeGreaterThan(0); | ||
}); | ||
|
||
test(`${permission} key: Get one batch`, async () => { | ||
const client = await getClient(permission); | ||
|
||
const batches = await client.getBatches(); | ||
const batch = await client.getBatch(batches.results[0].uid); | ||
expect(batch.uid).toEqual(batches.results[0].uid); | ||
expect(batch.details).toBeDefined(); | ||
expect(batch.stats).toHaveProperty("totalNbTasks"); | ||
expect(batch.stats).toHaveProperty("status"); | ||
expect(batch.stats).toHaveProperty("types"); | ||
expect(batch.stats).toHaveProperty("indexUids"); | ||
expect(batch.duration).toBeDefined(); | ||
expect(batch.startedAt).toBeDefined(); | ||
expect(batch.finishedAt).toBeDefined(); | ||
}); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters