-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (42 loc) · 1.65 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
const request = require('request-promise');
const express = require('express')
const {register, Gauge} = require('prom-client')
const sleep = require('sleep-promise');
const app = express();
const port = process.env.PORT || 3001;
const pairs = process.env.FETCH_PAIRS.split(',').map(pair => pair.split(':'));
const rippleExchangeRates = new Gauge({
name: 'ripple_exchange_rates',
help: 'Ripple exchange rates',
labelNames: ['base', 'counter', 'base_issuer', 'counter_issuer']
});
app.get('/metrics', (req, res) => {
res.set('Content-Type', register.contentType)
res.end(register.metrics())
});
const server = app.listen(port, () => {
console.log(`Metrics listening on port ${port}!`);
});
async function fetchRate(base, base_issuer, counter, counter_issuer) {
const result = await request.get({
method: 'GET',
url: `https://data.ripple.com/v2/exchange_rates/${base}${base_issuer ? '+' + base_issuer : ''}/${counter}${counter_issuer ? '+' + counter_issuer : ''}`,
json: true
});
if (result.result !== 'success') {
throw new Error(`cannot fetch rates ${base}+${base_issuer}/${counter}+${counter_issuer}`);
}
return parseFloat(result.rate);
}
(async function fetchRates() {
while (true) {
console.log('fetch rates');
for (const pair of pairs) {
console.log('fetch pair', pair);
const [base, base_issuer, counter, counter_issuer] = pair;
const rate = await fetchRate(base, base_issuer, counter, counter_issuer);
rippleExchangeRates.set({base, base_issuer, counter, counter_issuer}, rate);
}
await sleep(10000);
}
})();