-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 81c0f23
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
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,15 @@ | ||
{ | ||
"extends": "airbnb/base", | ||
|
||
"rules": { | ||
"no-console": 0 | ||
}, | ||
"globals": { | ||
"describe", | ||
"it", | ||
"before", | ||
"beforeEach", | ||
"after", | ||
"afterEach" | ||
} | ||
} |
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,2 @@ | ||
node_modules/ | ||
build/ |
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,19 @@ | ||
===== | ||
About | ||
===== | ||
|
||
SeeStats RTM (Real Time Messaging) API provides facilities to receive | ||
new stats data as soon as it's available. | ||
It's powered by socket.io. | ||
|
||
Dependencies | ||
============ | ||
|
||
* nodejs 5.7.1 | ||
|
||
Setup | ||
===== | ||
|
||
:: | ||
|
||
$ nodejs src/main.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,26 @@ | ||
var gulp = require('gulp'); | ||
var eslint = require('gulp-eslint'); | ||
var babel = require('gulp-babel'); | ||
|
||
var allSources = ['**/*.js', '!node_modules/**', '!gulpfile.js', '!build/**']; | ||
|
||
gulp.task('default', function lint() { | ||
console.log('Possible commands:'); | ||
console.log('\tgulp lint - checks code with eslint'); | ||
console.log('\tgulp build - compiles es6 JavaScript code to es5 standard,'); | ||
}); | ||
|
||
gulp.task('build', function build() { | ||
return gulp.src(allSources) | ||
.pipe(babel({ | ||
presets: ['es2015'], | ||
})) | ||
.pipe(gulp.dest('build')); | ||
}); | ||
|
||
gulp.task('lint', function lint() { | ||
return gulp.src(allSources) | ||
.pipe(eslint()) | ||
.pipe(eslint.format()) | ||
.pipe(eslint.failAfterError()); | ||
}); |
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,20 @@ | ||
{ | ||
"name": "see-stats-rtm-api", | ||
"version": "0.1.0", | ||
"description": "SeeStats Real Time Messaging API", | ||
"main": "src/main.js", | ||
"dependencies": { | ||
"elasticsearch": "^10.1.3", | ||
"gulp": "^3.9.1", | ||
"moment": "^2.11.2", | ||
"socket.io": "^1.4.5" | ||
}, | ||
"devDependencies": { | ||
"gulp-babel": "^6.1.2", | ||
"gulp-eslint": "^2.0.0", | ||
"babel": "^6.5.1", | ||
"babel-preset-es2015": "^6.5.0", | ||
"eslint": "1.10.3", | ||
"eslint-config-airbnb": "5.0.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const rtm = require('./rtm.js'); | ||
const stats = require('./stats.js'); | ||
|
||
const io = require('socket.io')(); | ||
|
||
/** | ||
* Main entry point. | ||
*/ | ||
function main () { | ||
var statsClient = new stats.Client(); | ||
statsClient.connect('https://es.seestats.org'); | ||
|
||
io.on('connection', (socket) => { | ||
socket.on('get_event_count', (data) => { | ||
statsClient.startFetchingStats(socket); | ||
}); | ||
}); | ||
io.listen(3000); | ||
} | ||
|
||
main() |
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,72 @@ | ||
/** | ||
* Facilities to fetch stats data from Elasticsearch. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const es = require('elasticsearch'); | ||
const moment = require('moment'); | ||
|
||
function makeIndexName() { | ||
const today = moment(Date.now()).format('YYYY.MM.DD'); | ||
|
||
return 'see-stats-' + today; | ||
} | ||
|
||
function makeCountFilters() { | ||
return { | ||
index: makeIndexName(), | ||
body: { | ||
'query': { | ||
'bool': { | ||
'must': [{ | ||
'range': { | ||
'@timestamp': { | ||
'from': 'now-1s/s', | ||
'to': 'now' | ||
} | ||
} | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
class Client { | ||
|
||
constructor() { | ||
this.esConn = null; | ||
} | ||
|
||
connect(host) { | ||
this.esConn = new es.Client({host: host}); | ||
} | ||
|
||
/** | ||
* Fetches for new stats data every second. | ||
*/ | ||
startFetchingStats(socket) { | ||
console.log('Start fetching'); | ||
|
||
setInterval(() => {this.fetchCount(socket);}, 1000); | ||
} | ||
|
||
fetchCount(socket) { | ||
this.esConn.count(makeCountFilters(), (err, resp) => { | ||
if (err) { | ||
console.log('Failed to fetch count:', err); | ||
return; | ||
}; | ||
|
||
socket.emit('event_count', resp.count); | ||
}); | ||
} | ||
}; | ||
|
||
exports.Client = Client; | ||
|
||
|
||
exports.fetch = function () { | ||
console.log('fetch stats'); | ||
}; |