-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathquestionService.js
More file actions
59 lines (40 loc) · 1.7 KB
/
Copy pathquestionService.js
File metadata and controls
59 lines (40 loc) · 1.7 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
const puppeteer = require('puppeteer');
const config = require('config');
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
async function saveQuestion(questionId) {
console.log("+ Collecting question " + questionId )
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Uncomment if you want the screenshot with a different
/*await page.setViewport({
width: 900,
height: 600,
deviceScaleFactor: 1,
});*/
let metabaseUrl = config.get('metabase.url');
question = {}
question.id = questionId;
question.imagePath = `${questionId}.png`;
question.url = `${metabaseUrl}question/${questionId}`
await page.goto(metabaseUrl);
await page.waitFor('input[name=username]');
await page.type('input[name=username]', config.get('metabase.user'));
await page.type('input[name=password]', config.get('metabase.password'));
await page.keyboard.press('Enter');
await page.waitForFunction( () => document.querySelector("body").innerText.includes('OUR DATA'));
await page.goto(question.url, {waitUntil: 'networkidle2'});
const questionTimeout = 60 * 4 * 1000;
await page.waitForFunction( () => !document.querySelector(".Loading-message"), { timeout: questionTimeout });
const text = await page.$('.mr1.Subhead-cNCOMx.hiDbPY')
question.name = await page.evaluate(el => el.textContent, text)
const graph = await page.$('.Visualization');
await graph.screenshot({path: question.imagePath});
await browser.close();
console.log("- Question collected " + questionId )
return question;
}
module.exports = saveQuestion