UNPKG

2.15 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const jsome = require('jsome');
4const Utils = require('./utils.js');
5
6const InsightsDir = path.resolve(__dirname, './insights');
7
8const reduce_file_by_line = (name, filepath, initAccum, reduceFunc) => new Promise(async (resolve, reject) => { try {
9 const accum = await Utils.line_reduce(
10 path.resolve(__dirname, './data') + '/' + name + '.txt',
11 (json, accum) => reduceFunc(json, accum),
12 initAccum
13 );
14 jsome(accum);
15 fs.writeFileSync(filepath, JSON.stringify(accum, null, 2));
16 resolve(accum);
17} catch(err) { reject(err) } });
18
19exports.rank_by_average_service_time = () =>
20 reduce_file_by_line('route_performance', InsightsDir + '/route_performance.json', {}, (json, accum) => {
21 const { path, connect, service, dyno } = json;
22 if (typeof accum[path] === 'undefined') {
23 accum[path] = {
24 [dyno]: [1, service]
25 }
26 return accum;
27 }
28 if (typeof accum[path][dyno] === 'undefined') {
29 accum[path] = {
30 [dyno]: [1, service]
31 }
32 return accum;
33 }
34 const [total, service_total] = accum[path][dyno];
35 const next_total = total + 1;
36 const next_service_total = service_total + service;
37 accum[path][dyno] = [next_total, Math.floor(next_service_total / 2)];
38 return accum;
39 });
40
41exports.service_failed = () =>
42 reduce_file_by_line('service_failed', InsightsDir + '/service_failed.json', [], (json, accum) => {
43 return accum.concat([json])
44 });
45
46exports.service_succeeded = () =>
47 reduce_file_by_line('service_succeeded', InsightsDir + '/service_succeeded.json', [], (json, accum) => {
48 return accum.concat([json])
49 });
50
51exports.worker_failed = () =>
52 reduce_file_by_line('worker_failed', InsightsDir + '/worker_failed.json', [], (json, accum) => {
53 return accum.concat([json])
54 });
55
56exports.worker_succeeded = () =>
57 reduce_file_by_line('worker_succeeded', InsightsDir + '/worker_succeeded.json', [], (json, accum) => {
58 return accum.concat([json])
59 });
60
61exports.basic = () =>
62 reduce_file_by_line('basic', InsightsDir + '/basic.json', [], (json, accum) => {
63 return accum.concat([json])
64 });