Skip to content

Commit 61ee6c0

Browse files
author
Loïc Knuchel
committed
improve js code
1 parent d97acf6 commit 61ee6c0

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

js/main.js

+43-47
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
var http = require("http");
22
var fs = require('fs');
3-
4-
function fetchFromUrl(hostname, port, path) {
5-
var options = {
6-
port,
7-
hostname,
8-
path: path,
9-
method: 'GET'
10-
};
11-
12-
return new Promise((resolve, reject) => {
13-
var data = '';
14-
var req = http.request(options, (res) => {
15-
res.setEncoding('utf8');
16-
res.on('data', (chunk) => data += chunk);
17-
res.on('end', () => resolve(JSON.parse(data)))
18-
});
19-
20-
req.on('error', e => reject(e));
21-
req.end();
22-
});
3+
const projections = require('./projections.js');
4+
5+
const filename = process.argv[2];
6+
const resultFile = '../data/results.json';
7+
8+
// write your projections in ./projections.js
9+
Promise.all([readFile(filename), readFile(resultFile)])
10+
.then(([file, results]) => [parseEvents(file), JSON.parse(results)])
11+
.then(([events, results]) => exec(results[filename], events, projections))
12+
.catch(error => console.log(error));
13+
14+
function exec(results, events, projections) {
15+
for(let name in projections) {
16+
if(projections.hasOwnProperty(name)) {
17+
const start = Date.now();
18+
const res = projections[name](events);
19+
const end = Date.now();
20+
if(results && results[name]) {
21+
if(JSON.stringify(res) === JSON.stringify(results[name])) {
22+
console.log(name + ' OK ('+(end-start)+' ms)');
23+
} else {
24+
console.error(name + ' ERROR ('+(end-start)+' ms)');
25+
console.error('result: ' + JSON.stringify(res, null, 2));
26+
return events; // break the loop
27+
}
28+
} else {
29+
console.log(name + ' ('+(end-start)+' ms) results : ' + JSON.stringify(res));
30+
}
31+
}
32+
}
33+
return events;
2334
}
2435

25-
function fetchFromFile(stream) {
26-
return new Promise((resolve, reject) => {
27-
fs.readFile(stream, 'utf-8', (err, data) => {
28-
if(err) reject(err);
2936

30-
resolve(JSON.parse(data));
31-
})
32-
});
33-
}
34-
35-
// If you want to have the timestamps of the event as a Date object rather than a string, enable this
36-
// transformation. This transformation mutates the events. After the transformation, the timestamp is a Date
37-
// Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date for more info.
38-
function transformTimestampToDate(events) {
39-
return events.map(event => {
40-
event.timestamp = new Date(event.timestamp);
41-
return event;
42-
});
37+
function parseEvents(file) {
38+
return JSON.parse(file).map(event => {
39+
event.timestamp = new Date(event.timestamp);
40+
return event;
41+
});
4342
}
4443

45-
// Write your projection here
46-
function eventCounter(events) {
47-
return events.reduce((acc, event) => acc + 1, 0);
44+
function readFile(filename) {
45+
return new Promise((resolve, reject) => {
46+
fs.readFile(filename, 'utf-8', (err, data) => {
47+
if(err) reject(err);
48+
else resolve(data);
49+
});
50+
});
4851
}
49-
50-
//fetchFromUrl('localhost', 8000, '/test_from_run.json')
51-
var fileName = process.argv[2];
52-
fetchFromFile(fileName)
53-
.then(events => transformTimestampToDate(events))
54-
.then(events => console.log(eventCounter(events)))
55-
.catch(error => console.log(error))

js/projections.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Write your projections here...
2+
3+
module.exports = {
4+
numberOfEvents: events => {},
5+
numberOfRegistredPlayers: events => {},
6+
numberOfRegistredPlayersPerMonth: events => {},
7+
mostPopularQuizs: events => {},
8+
inactivePlayers: events => {},
9+
activePlayers: events => {}
10+
};

0 commit comments

Comments
 (0)