-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (86 loc) · 3.25 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
// Created by J. Eric Hartzog on 7/19/17
const phantomjs = require('phantomjs-prebuilt')
const webdriverio = require('webdriverio')
const wdOpts = { desiredCapabilities: { browserName: 'phantomjs' } }
const scrapeInfo = require('./scrape-info');
const scalingLogic = require('./scaling-logic');
const args = require('./auth-info.json');
phantomjs.run('--webdriver=4444').then(async program => {
console.log(`${(new Date()).toString()} - Starting run`);
console.time('run');
let browser;
try {
// For reason using await on this doesn't work, so we just pause (ugh)
browser = webdriverio.remote(wdOpts)
.init()
.url(`https://galaxy.meteor.com/app/${args.appName}`);
await browser.pause(5000);
// console.log('Browser loaded');
await browser.executeAsync(function (done) {
eval("window.localStorage.clear()");
setTimeout(function () {
done();
}, 5000);
});
// console.log('Cleared local storage');
try {
await browser.setValue('[name="username"]', args.galaxyUsername)
await browser.setValue('[name="password"]', args.galaxyPassword)
await browser.click('form button[type="submit"]')
} catch (err) {
console.log(`Error trying to log in ${err}`);
}
await browser.pause(5000);
// Wait till no more spinners
const loading = async () => {
try {
let spinners = await browser.getCssProperty('div.loading-spinner', 'opacity');
// If we only get 1 result, place it in array
if (!Array.isArray(spinners)) {
spinners = [spinners];
}
return spinners.reduce((acc, cur) => {
return acc || cur.value !== 0;
}, false);
} catch (err) {
// If we can't find the spinners, then the page is still loading
console.warn(err);
return true;
}
};
while (await loading()) {
console.log('Still loading, pausing...');
await browser.pause(5000);
}
const info = await scrapeInfo(browser);
console.log('Current stats:', info);
const scaleAction = scalingLogic(info);
if (scaleAction > 0) {
await browser.executeAsync(function (done) {
eval("$('button.cardinal-action.increment').click()");
setTimeout(function () {
done();
}, 5000);
});
console.log('Completed scaling up');
}
if (scaleAction < 0) {
await browser.executeAsync(function (done) {
eval("$('button.cardinal-action.decrement').click()");
setTimeout(function () {
done();
}, 5000);
});
console.log('Completed scaling down');
}
} catch (err) {
console.error(err);
try {
browser.saveScreenshot(`./error - ${(new Date()).toString()}.png`);
} catch (imgError) {
console.error(imgError);
}
}
console.timeEnd('run');
program.kill();
})