-
Notifications
You must be signed in to change notification settings - Fork 51
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
Loïc Knuchel
committed
May 24, 2017
1 parent
d97acf6
commit 61ee6c0
Showing
2 changed files
with
53 additions
and
47 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 |
---|---|---|
@@ -1,55 +1,51 @@ | ||
var http = require("http"); | ||
var fs = require('fs'); | ||
|
||
function fetchFromUrl(hostname, port, path) { | ||
var options = { | ||
port, | ||
hostname, | ||
path: path, | ||
method: 'GET' | ||
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
var data = ''; | ||
var req = http.request(options, (res) => { | ||
res.setEncoding('utf8'); | ||
res.on('data', (chunk) => data += chunk); | ||
res.on('end', () => resolve(JSON.parse(data))) | ||
}); | ||
|
||
req.on('error', e => reject(e)); | ||
req.end(); | ||
}); | ||
const projections = require('./projections.js'); | ||
|
||
const filename = process.argv[2]; | ||
const resultFile = '../data/results.json'; | ||
|
||
// write your projections in ./projections.js | ||
Promise.all([readFile(filename), readFile(resultFile)]) | ||
.then(([file, results]) => [parseEvents(file), JSON.parse(results)]) | ||
.then(([events, results]) => exec(results[filename], events, projections)) | ||
.catch(error => console.log(error)); | ||
|
||
function exec(results, events, projections) { | ||
for(let name in projections) { | ||
if(projections.hasOwnProperty(name)) { | ||
const start = Date.now(); | ||
const res = projections[name](events); | ||
const end = Date.now(); | ||
if(results && results[name]) { | ||
if(JSON.stringify(res) === JSON.stringify(results[name])) { | ||
console.log(name + ' OK ('+(end-start)+' ms)'); | ||
} else { | ||
console.error(name + ' ERROR ('+(end-start)+' ms)'); | ||
console.error('result: ' + JSON.stringify(res, null, 2)); | ||
return events; // break the loop | ||
} | ||
} else { | ||
console.log(name + ' ('+(end-start)+' ms) results : ' + JSON.stringify(res)); | ||
} | ||
} | ||
} | ||
return events; | ||
} | ||
|
||
function fetchFromFile(stream) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(stream, 'utf-8', (err, data) => { | ||
if(err) reject(err); | ||
|
||
resolve(JSON.parse(data)); | ||
}) | ||
}); | ||
} | ||
|
||
// If you want to have the timestamps of the event as a Date object rather than a string, enable this | ||
// transformation. This transformation mutates the events. After the transformation, the timestamp is a Date | ||
// Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date for more info. | ||
function transformTimestampToDate(events) { | ||
return events.map(event => { | ||
event.timestamp = new Date(event.timestamp); | ||
return event; | ||
}); | ||
function parseEvents(file) { | ||
return JSON.parse(file).map(event => { | ||
event.timestamp = new Date(event.timestamp); | ||
return event; | ||
}); | ||
} | ||
|
||
// Write your projection here | ||
function eventCounter(events) { | ||
return events.reduce((acc, event) => acc + 1, 0); | ||
function readFile(filename) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filename, 'utf-8', (err, data) => { | ||
if(err) reject(err); | ||
else resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
//fetchFromUrl('localhost', 8000, '/test_from_run.json') | ||
var fileName = process.argv[2]; | ||
fetchFromFile(fileName) | ||
.then(events => transformTimestampToDate(events)) | ||
.then(events => console.log(eventCounter(events))) | ||
.catch(error => console.log(error)) |
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,10 @@ | ||
// Write your projections here... | ||
|
||
module.exports = { | ||
numberOfEvents: events => {}, | ||
numberOfRegistredPlayers: events => {}, | ||
numberOfRegistredPlayersPerMonth: events => {}, | ||
mostPopularQuizs: events => {}, | ||
inactivePlayers: events => {}, | ||
activePlayers: events => {} | ||
}; |