-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
99 lines (86 loc) · 2.32 KB
/
test.js
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
const path = require('path');
const { writeFileSync, existsSync, mkdirSync } = require('fs');
const { handler } = require('.');
const runTest = (
query = { type: '', screen: '', custom: '', hoverIndex: '', hideLegend },
options = {
filePath: 'images/screenshot.png',
headless: true,
}
) => {
const { type, screen, custom, hoverIndex, hideLegend, dateFrom, dateTo } =
query;
const event = {
queryStringParameters: {
type: type.toUpperCase(),
screen,
custom,
hoverIndex,
hideLegend,
dateFrom,
dateTo,
},
headless: options.headless,
};
const ensureDirectoryExistence = filePath => {
var dirname = path.dirname(filePath);
if (existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
mkdirSync(dirname);
};
const callback = (error, result) => {
if (error) {
console.log(error);
return error;
}
const base64Data = result.body;
if (!base64Data) {
return new Error(result);
}
ensureDirectoryExistence(filePath);
try {
writeFileSync(`${filePath}`, base64Data, 'base64');
} catch (error) {
console.log(error);
}
console.log(`File saved to: ${filePath}`);
};
(async () => await handler(event, null, callback))();
};
// type = "card" || "chart" || "multicard"
// screen -> see screenshots.js CARD || CHART || MULTICARD properties
// custom -> see CHART[name].customChart
// const query = {
// type: 'chart',
// screen: 'Cases',
// custom: 'cases_DateRange_Active_Hospitalized_Deceased_Tooltip',
// hoverIndex: '118',
// hideLegend: 'false',
// dateFrom: '01. 01. 2021',
// dateTo: '30. 04. 2021',
// };
const query = {
type: 'card_embed',
screen: 'icuCurrent',
};
const dateTime = new Date();
const time = dateTime.toISOString().slice(11, 19).split(':').join('_');
const date = dateTime.toISOString().slice(0, 10);
let filename = query.custom ? `${query.screen}_${query.custom}` : query.screen;
filename = query.hoverIndex ? `${filename}_${query.hoverIndex}` : filename;
filename += `_${time}.png`;
const defaultFolder = 'images';
const filePath = path.resolve(
defaultFolder,
date,
query.type.toLocaleLowerCase(),
filename
);
const headless = true; // puppeteer launch browser mode
const options = {
filePath,
headless,
};
runTest(query, options);