|
1 | 1 | var http = require("http");
|
2 | 2 | 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; |
23 | 34 | }
|
24 | 35 |
|
25 |
| -function fetchFromFile(stream) { |
26 |
| - return new Promise((resolve, reject) => { |
27 |
| - fs.readFile(stream, 'utf-8', (err, data) => { |
28 |
| - if(err) reject(err); |
29 | 36 |
|
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 | + }); |
43 | 42 | }
|
44 | 43 |
|
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 | + }); |
48 | 51 | }
|
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)) |
0 commit comments