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: refacto (part 1) #371

Merged
merged 5 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ appId=
apiKey=
indexName=npm-search
bootstrapIndexName=npm-search-bootstrap
DOGSTATSD_HOST="localhost"
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"start": "node --max-old-space-size=920 --experimental-modules src/index.js",
"test": "jest && eslint .",
"test": "jest --forceExit && eslint .",
"test:watch": "jest --watchAll --no-watchman",
"format": "prettier --write **/*.{js,md,json}",
"doctoc": "doctoc --notitle --maxlevel 3 README.md",
Expand Down Expand Up @@ -37,10 +37,12 @@
"bunyan": "1.8.12",
"bunyan-debug-stream": "1.1.1",
"dotenv": "6.2.0",
"dtrace-provider": "0.8.7",
"escape-html": "1.0.3",
"got": "9.6.0",
"gravatar-url": "2.0.0",
"hosted-git-info": "2.7.1",
"hot-shots": "6.3.0",
"lodash": "4.17.15",
"ms": "2.1.2",
"nice-package": "3.1.0",
Expand Down
22 changes: 16 additions & 6 deletions src/changelog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import got from 'got';
import race from 'promise-rat-race';

import datadog from './datadog.js';

const baseUrlMap = new Map([
[
'github.com',
Expand All @@ -25,7 +27,7 @@ const baseUrlMap = new Map([
],
]);

function getChangelog({ repository }) {
async function getChangelog({ repository }) {
if (repository === null) {
return { changelogFilename: null };
}
Expand Down Expand Up @@ -64,11 +66,19 @@ function getChangelog({ repository }) {
'history',
].map(file => [baseUrl.replace(/\/$/, ''), file].join('/'));

return race(files.map(got, { method: 'HEAD' }))
.then(({ url }) => ({ changelogFilename: url }))
.catch(() => ({ changelogFilename: null }));
try {
const { url } = await race(files.map(got, { method: 'HEAD' }));
return { changelogFilename: url };
} catch (e) {
return { changelogFilename: null };
}
}

export function getChangelogs(pkgs) {
return Promise.all(pkgs.map(getChangelog));
export async function getChangelogs(pkgs) {
const start = Date.now();

const all = await Promise.all(pkgs.map(getChangelog));

datadog.timing('changelogs.getChangelogs', Date.now() - start);
return all;
}
55 changes: 32 additions & 23 deletions src/createStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,44 @@ const defaultState = {
let currentState;

export default algoliaIndex => ({
check() {
async check() {
if (c.seq !== null) return this.reset();
return this.get().then(
state => (state === undefined ? this.reset() : state)
);
const state = await this.get();

if (state === undefined) {
return this.reset();
}
return state;
},
get() {
return currentState
? Promise.resolve(currentState)
: algoliaIndex.getSettings().then(({ userData }) => userData);

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

const { userData } = await algoliaIndex.getSettings();
return userData;
},
set(state) {

async set(state) {
currentState = state;

return algoliaIndex
.setSettings({
userData: state,
})
.then(() => state);
await algoliaIndex.setSettings({
userData: state,
});
return state;
},
reset() {
return this.set(defaultState);

async reset() {
return await this.set(defaultState);
},
save(partial) {
return this.get().then((current = defaultState) =>
this.set({
...current,
...partial,
})
);

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

return await this.set({
...current,
...partial,
});
},
});
18 changes: 18 additions & 0 deletions src/datadog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import StatsD from 'hot-shots';
import log from './log.js';

const env = process.env.NODE_ENV === 'production' ? 'prod' : 'dev';

const client = new StatsD({
host: process.env.DOGSTATSD_HOST || 'localhost',
port: 8125,
prefix: 'alg.npmsearch.',
globalTags: {
env,
},
errorHandler(error) {
log.error('[DATADOG ERROR]', error);
},
});

export default client;
Loading