Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move algolia #385

Merged
merged 9 commits into from
Aug 8, 2019
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
69 changes: 69 additions & 0 deletions src/__tests__/createStateManager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import createStateManager from '../createStateManager.js';

describe('stateManager', () => {
describe('get()', () => {
it('should get userData from algolia', async () => {
const mock = {
getSettings: jest.fn(() => {
return {
userData: 'foobar',
};
}),
};
const stateManager = createStateManager(mock);
const userData = await stateManager.get();

expect(mock.getSettings).toHaveBeenCalled();
expect(userData).toBe('foobar');
});
});

describe('set()', () => {
it('should set userData to algolia', async () => {
const mock = {
setSettings: jest.fn(),
};
const stateManager = createStateManager(mock);
await stateManager.set('state');

expect(mock.setSettings).toHaveBeenCalledWith({
userData: 'state',
});
});
});

describe('reset()', () => {
it('should reset userData', async () => {
const mock = {
setSettings: jest.fn(),
};
const stateManager = createStateManager(mock);
await stateManager.reset();

expect(mock.setSettings).toHaveBeenCalled();
});
});

describe('save()', () => {
it('should save userData to algolia', async () => {
const mock = {
getSettings: jest.fn(() => {
return {
userData: { bar: 'foo' },
};
}),
setSettings: jest.fn(),
};
const stateManager = createStateManager(mock);
await stateManager.save({ foo: 'bar' });

expect(mock.getSettings).toHaveBeenCalled();
expect(mock.setSettings).toHaveBeenCalledWith({
userData: {
bar: 'foo',
foo: 'bar',
},
});
});
});
});
63 changes: 63 additions & 0 deletions src/algolia/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import algoliasearch from 'algoliasearch';

function createClient(appId, apiKey, indexName) {
const client = algoliasearch(appId, apiKey);
return {
index: client.initIndex(indexName),
client,
};
}

/**
* Prepare algolia for indexing
* @param {object} config
*/
async function prepare(config) {
if (!config.apiKey) {
throw new Error(
'npm-search: Please provide the `apiKey` env variable and restart'
);
}

// Get main index and boostrap algolia client
const { index: mainIndex, client } = createClient(
config.appId,
config.apiKey,
config.indexName
);
const { index: bootstrapIndex } = createClient(
config.appId,
config.apiKey,
config.bootstrapIndexName
);

// Ensure indices exists by calling an empty setSettings()
await mainIndex.setSettings({});
await bootstrapIndex.setSettings({});

return {
client,
mainIndex,
bootstrapIndex,
};
}

/**
*
* @param {AlgoliasearchIndex} index
* @param {object} config
*/
async function putDefaultSettings(index, config) {
await index.setSettings(config.indexSettings);

await index.batchSynonyms(config.indexSynonyms, {
replaceExistingSynonyms: true,
});
const { taskID } = await index.batchRules(config.indexRules, {
replaceExistingRules: true,
});

await index.waitTask(taskID);
}

export { prepare, putDefaultSettings };
15 changes: 0 additions & 15 deletions src/createAlgoliaIndex.js

This file was deleted.

104 changes: 53 additions & 51 deletions src/createStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,56 @@ const defaultState = {
bootstrapLastId: undefined,
};

let currentState;

export default algoliaIndex => ({
async check() {
if (config.seq !== null) return this.reset();
const state = await this.get();

if (state === undefined) {
return this.reset();
}

return state;
},

async get() {
if (currentState) {
return currentState;
}

const start = Date.now();
const { userData } = await algoliaIndex.getSettings();
datadog.timing('stateManager.get', Date.now() - start);

return userData;
},

async set(state) {
currentState = state;

const start = Date.now();
await algoliaIndex.setSettings({
userData: state,
});
datadog.timing('stateManager.set', Date.now() - start);

return state;
},

async reset() {
return await this.set(defaultState);
},

async save(partial) {
const current = (await this.get()) || defaultState;

return await this.set({
...current,
...partial,
});
},
});
export default algoliaIndex => {
let currentState;

return {
async check() {
if (config.seq !== null) return this.reset();
const state = await this.get();

if (state === undefined) {
return this.reset();
}

return state;
},

async get() {
if (currentState) {
return currentState;
}

const start = Date.now();
const { userData } = await algoliaIndex.getSettings();
datadog.timing('stateManager.get', Date.now() - start);

return userData;
},

async set(state) {
currentState = state;

const start = Date.now();
await algoliaIndex.setSettings({
userData: state,
});
datadog.timing('stateManager.set', Date.now() - start);

return state;
},

async reset() {
return await this.set(defaultState);
},

async save(partial) {
const current = (await this.get()) || defaultState;

return await this.set({
...current,
...partial,
});
},
};
};
Loading