-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (89 loc) · 2.43 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
const http = require('https');
exports.handler = (event, context, callback) => {
var key = event['queryStringParameters']['key'];
var url = "https://sugarmate.io/api/v1/" + key + "/latest.json";
var chart = event['queryStringParameters']['chart'];
if (!chart) {
chart = "difference";
}
getUrl(url, function (result, error) {
if (error) {
console.log(error);
} else {
var data = "";
switch(chart.toUpperCase()) {
case "GAUGE":
data = getGaugeData(result["result"]);
break;
default:
data = getCountDifferenceData(result["result"]);
}
callback(null, {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
}
});
};
var getUrl = function (url, callback) {
var req = http.get(url, (res) => {
var body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
var result = JSON.parse(body);
callback({ result });
});
}).on("error", (error) => {
console.log(error);
});
};
function getCountDifferenceData(result) {
var trend_symbol = result["trend_symbol"];
var trend_words = result["trend_words"];
var value = result["value"];
var delta = result["delta"];
var previousValue = (delta - (delta * 2)) + value;
var data = {
postfix: trend_symbol,
color: getColor(trend_words),
data: [{
value: value
},{
value: previousValue
}]
}
return data;
}
function getGaugeData(result) {
var trend_symbol = result["trend_symbol"];
var trend_words = result["trend_words"];
var value = result["value"];
var data = {
postfix: trend_symbol,
color: getColor(trend_words),
data: {
minValue: 80,
value: value,
maxValue: 120
}
}
return data;
}
function getColor(value) {
switch(value.toUpperCase()) {
case "FORTY_FIVE_UP":
case "FORTY_FIVE_DOWN":
return "yellow";
case "SINGLE_UP":
case "SINGLE_DOWN":
return "orange";
case "DOUBLE_UP":
case "DOUBLE_DOWN":
return "red";
default:
return "white";
}
}