-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (134 loc) · 4.5 KB
/
index.js
File metadata and controls
168 lines (134 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#! /usr/bin/env node
const request = require('./request');
const clc = require("cli-color");
const Table = require("cli-table");
const cliSelect = require('cli-select');
const baseUrl = 'https://api.runscope.com';
const { Command } = require('commander');
const program = new Command();
program
.option('-c, --count <integer>', 'Number of results', 10)
.requiredOption('-t, --token <string>', 'Token or use environment RUNSCOPE_TOKEN', process.env.RUNSCOPE_TOKEN)
.option('-r, --refresh-time <integer>', 'Refresh time in ms', 120000)
.option('-w, --watch', 'Watch', false)
.option('--no-results', 'Hide results', false)
.parse(process.argv);
const maxResults = program.count;
const token = program.token;
const refreshTime = program.refreshTime;
const watch = program.watch;
const showResults = program.results;
const head = ['Name', 'Status'];
if (showResults) {
head.push('Latest Results', 'Latest Result Date', 'Success Ratio');
}
const table = new Table({
head
});
(async () => {
try {
let buckets = await getBuckets();
const bucketKey = (await cliSelect({
values: getOptions(buckets),
valueRenderer: value => value.name,
})).value.key;
let tests = await getTests(bucketKey);
let resultPromises = [];
do {
let results;
if (showResults) {
resultPromises = tests.map(test => getResults(bucketKey, test.id));
results = await Promise.all(resultPromises);
}
table.splice(0, table.length);
tests.forEach((test, index) => {
let row = [test.name, getLastTestStatus(test.last_run.status)];
if (showResults) {
row.push(
results[index]
.reverse()
.map(value => getTestStatus(value.result))
.join(' '),
dateFormat(
timestampToDate(results[index][results[index].length - 1].started_at)
),
calcRate(results[index]) + '%'
);
}
table.push(row);
});
if (watch) {
process.stdout.write(clc.reset);
}
process.stdout.write(table.toString());
if (watch) {
await sleep(refreshTime);
}
} while (watch);
} catch (e) {
console.log(e.message);
}
})();
async function getResults(bucketKey, testId) {
const response = await request(
`${baseUrl}/buckets/${bucketKey}/tests/${testId}/results?count=${maxResults}`,
{
headers: {
Authorization: `Bearer ${token}`,
}
}
);
return response.data;
}
async function getTests(bucketKey) {
const response = await request(
`${baseUrl}/buckets/${bucketKey}/tests?count=50`,
{
headers: {
Authorization: `Bearer ${token}`,
}
}
);
return response.data;
}
async function getBuckets() {
const response = await request(
`${baseUrl}/buckets`,
{
headers: {
Authorization: `Bearer ${token}`,
}
}
);
return response.data;
}
function calcRate(data) {
const pass = data.reduce((sum, item) => {
return item.result === 'pass' ? sum + 1 : sum;
}, 0);
return ((pass / maxResults) * 100).toFixed(2);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getOptions(buckets) {
return buckets.map(value => {
return {
name: value.name,
key: value.key,
};
});
}
function getLastTestStatus(status) {
return status === 'completed' ? clc.green('Passed') : clc.red('Failed');
}
function getTestStatus(result) {
return result === 'pass' ? clc.bgGreen(' ') : clc.bgRed(' ');
}
function timestampToDate(timestamp) {
return new Date(timestamp * 1000);
}
function dateFormat(date) {
const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
return `${months[date.getMonth()]} ${date.getDate()} ${date.getFullYear()} at ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}