Skip to content

Commit

Permalink
feat: refacto (part 1) (#371)
Browse files Browse the repository at this point in the history
* feat: add datadog metrics

* feat: full async/await
  • Loading branch information
bodinsamuel committed Aug 5, 2019
1 parent 25d29dd commit c024451
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 236 deletions.
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"
6 changes: 4 additions & 2 deletions 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 All @@ -29,17 +29,19 @@
"license": "MIT",
"dependencies": {
"algoliasearch": "3.33.0",
"async": "2.6.3",
"async": "3.1.0",
"babel-cli": "6.26.0",
"babel-preset-env": "1.7.0",
"babel-preset-stage-2": "6.24.1",
"bunyan": "1.8.12",
"dtrace-provider": "0.8.7",
"bunyan-debug-stream": "2.0.0",
"dotenv": "8.0.0",
"escape-html": "1.0.3",
"got": "9.6.0",
"gravatar-url": "3.0.1",
"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;
}
63 changes: 40 additions & 23 deletions src/createStateManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import c from './config.js';
import datadog from './datadog.js';

const defaultState = {
seq: c.seq,
Expand All @@ -9,35 +10,51 @@ 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 start = Date.now();
const { userData } = await algoliaIndex.getSettings();
datadog.timing('stateManager.get', Date.now() - start);

return userData;
},
set(state) {

async set(state) {
currentState = state;

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

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

0 comments on commit c024451

Please sign in to comment.