-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implemented ensure-index command * bump version
- Loading branch information
1 parent
2804065
commit dc81849
Showing
4 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
lib/interface/cli/commands/offline-logs/ensure-index.cmd.js
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,42 @@ | ||
const { MongoClient } = require("mongodb"); | ||
const Command = require("../../Command"); | ||
const cmd = require("./base.cmd"); | ||
const utils = require('./utils') | ||
|
||
const command = new Command({ | ||
command: "ensure-index", | ||
parent: cmd, | ||
description: | ||
"Checks whether a collection has indexes and gives the user an option to add them. Adding an index may improve performance of some of the read operations.", | ||
webDocs: { | ||
category: "Logs", | ||
title: "Ensures one or more offline-logs collections has indexes", | ||
}, | ||
builder: (yargs) => | ||
yargs.example( | ||
'codefresh offline-logs ensure-index --uri "mongodb://192.168.99.100:27017" --db logs' | ||
), | ||
handler: async (argv) => { | ||
const { uri, db } = argv; | ||
const client = new MongoClient(uri); | ||
await client.connect(); | ||
const database = client.db(db); | ||
const failedCollections = []; | ||
for (const collection of utils.defaultCollections) { | ||
try { | ||
await utils.ensureIndex(database, collection); | ||
} catch (error) { | ||
console.error(`failed to ensure index of collection '${collection}', error: ${error.message}`); | ||
failedCollections.push(collection); | ||
} | ||
} | ||
client.close(); | ||
if (failedCollections.length) { | ||
throw new Error( | ||
`failed to ensure indexes of ${failedCollections.join(", ")}` | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
module.exports = command; |
77 changes: 77 additions & 0 deletions
77
lib/interface/cli/commands/offline-logs/ensure-index.spec.js
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,77 @@ | ||
// const {Db, Collection} = require('mongodb'); | ||
const { Collection } = require('yaml/types'); | ||
const utils = require('./utils') | ||
|
||
describe("ensure-index", () => { | ||
describe("ensureIndex", () => { | ||
utils.getUserInput = jest.fn() | ||
const mockCollection = { | ||
indexExists: jest.fn(), | ||
estimatedDocumentCount: jest.fn().mockReturnValue(700), | ||
createIndex: jest.fn(), | ||
}; | ||
const mockDatabase = { | ||
collection() { | ||
return mockCollection; | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockCollection.indexExists.mockClear(); | ||
mockCollection.estimatedDocumentCount.mockClear(); | ||
mockCollection.createIndex.mockClear(); | ||
utils.getUserInput.mockClear(); | ||
}); | ||
|
||
it("index does not exists and user says yes on prompt", async () => { | ||
//setup | ||
mockCollection.indexExists.mockReturnValue(false); | ||
utils.getUserInput.mockReturnValue(true); | ||
const collection = 'whatever'; | ||
const expectedIndexObj = { accountId: 1, jobId: 1 } | ||
|
||
//execute | ||
await utils.ensureIndex(mockDatabase, collection); | ||
|
||
//check | ||
expect(mockCollection.indexExists).toBeCalledTimes(1); | ||
expect(mockCollection.indexExists).toBeCalledWith("accountId_1_jobId_1"); | ||
expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(1); | ||
expect(utils.getUserInput).toBeCalledTimes(1); | ||
expect(mockCollection.createIndex).toBeCalledTimes(1); | ||
expect(mockCollection.createIndex).toBeCalledWith(expectedIndexObj) | ||
}); | ||
it("index does not exists and user says no on prompt", async () => { | ||
//setup | ||
mockCollection.indexExists.mockReturnValue(false); | ||
utils.getUserInput.mockReturnValue(false); | ||
const collection = 'whatever'; | ||
|
||
//execute | ||
await utils.ensureIndex(mockDatabase, collection); | ||
|
||
//check | ||
expect(mockCollection.indexExists).toBeCalledTimes(1); | ||
expect(mockCollection.indexExists).toBeCalledWith("accountId_1_jobId_1"); | ||
expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(1); | ||
expect(utils.getUserInput).toBeCalledTimes(1); | ||
expect(mockCollection.createIndex).toBeCalledTimes(0); | ||
}); | ||
it("index exists", async () => { | ||
//setup | ||
mockCollection.indexExists.mockReturnValue(true); | ||
const collection = 'whatever'; | ||
|
||
//execute | ||
await utils.ensureIndex(mockDatabase, collection); | ||
|
||
//check | ||
expect(mockCollection.indexExists).toBeCalledTimes(1); | ||
expect(mockCollection.indexExists).toBeCalledWith("accountId_1_jobId_1"); | ||
expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(0); | ||
expect(utils.getUserInput).toBeCalledTimes(0); | ||
expect(mockCollection.createIndex).toBeCalledTimes(0); | ||
}); | ||
|
||
}); | ||
}); |
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