Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
initial cluster seed
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouncey committed May 26, 2017
1 parent a995ea7 commit 9bc62df
Show file tree
Hide file tree
Showing 57 changed files with 42,834 additions and 98 deletions.
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"no-console": 0,
"quotes": [
"error",
"single",
{ "allowTemplateLiterals": true }
],
"semi": [
"error",
"always"
]
}
};
5 changes: 3 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const milkDiscourse = require('./init/discourse');
require('dotenv').config();
const init = require('./init');

milkDiscourse();
init();
72 changes: 72 additions & 0 deletions elastic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const events = require('events');
const elasticsearch = require('elasticsearch');

const eventEmitter = new events.EventEmitter();

const {
error,
log,
info
} = require('../utils');

let connected = false;

const client = new elasticsearch.Client({
host: [
{
host: 'localhost',
auth: 'elastic:changeme'
}
]
});

client.ping({
requestTimeout: 1000
}, function (error) {
if (error) {
console.trace('elasticsearch cluster is down!');
} else {
log('All is well with the cluster');
connected = true;
eventEmitter.emit('connection');
}
});

function bulkInsert({ index, type, documents }) {
const insert = { index: { _index: index, _type: type } };
const request = documents.reduce((acc, current) => {
return [ ...acc, insert, current ];
}, []);
client.bulk({
body: request
},
(err) => {
if (err) { error(JSON.stringify(err, null, 2)); }
});
}

function deleteActual() {
error('DELETING all documents from the cluster');
client.indices.delete(
{ index: '_all' },
(err, response) => {
if (err) {
log(JSON.stringify(err, null, 2));
return;
}
info(JSON.stringify(response, null, 2));
return Promise.resolve();
});
}

function deleteAll() {
if (connected) {
return deleteActual();
}
return eventEmitter.on('connection', deleteActual);
}

module.exports ={
bulkInsert,
deleteAll
};
60 changes: 60 additions & 0 deletions init/challenges/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { Observable } = require('rx');
const getChallenges = require('./seed/getChallenges');

const {
bulkInsert
} = require('../../elastic');

function dasherize(name) {
return ('' + name)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-\.]/gi, '')
.replace(/\:/g, '');
}

function snippetGen(description) {
if (!description) {
return '';
}
return description
.join(' ')
.slice(0, 100)
.trim()
+ '...';
}

function getChallengeData() {

Observable.from(getChallenges())
.flatMap(
({ name, challenges })=> {
const block = dasherize(name);
const formattedChallenges = challenges
.reduce((acc, current) => {
const { id, title, description } = current;
const dashedName = dasherize(title);
const formattedChallenge = {
blockName: name,
id,
title,
dashedName,
snippet: snippetGen(description),
url: `https://freecodecamp.com/${block}/${dashedName}`
};
return [ ...acc, formattedChallenge ];
}, []);
return Observable.of({
block,
challenges: formattedChallenges
});
})
.subscribe(
(challengeBlock => {
const { block, challenges } = challengeBlock;
bulkInsert({ index: 'challenge', type: block, documents: challenges });
})
);
}

module.exports = getChallengeData;
Loading

0 comments on commit 9bc62df

Please sign in to comment.