-
Notifications
You must be signed in to change notification settings - Fork 1
/
eco.js
154 lines (138 loc) · 3.91 KB
/
eco.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const vega = require('vega');
const dateFormat = require('dateformat');
const es_client = require("./elastic_api.js").client;
const Errors = require("./errors.js");
module.exports.addMeasure = async function (gold, orn, florin) {
const timestamp = Date.now();
const index = `eco-${process.env.SERVER_UID}-${dateFormat(timestamp, "yyyy-mm")}`;
await es_client.indices.exists({
index: index
}).then((exists) => {
if (!exists) {
return es_client.indices.create({
index: index,
ignore: [400]
});
}
});
es_client.index({
index: index,
body: {
timestamp: timestamp,
gold: gold,
orn: orn,
florin: florin
}
}).catch((err) => console.error(err));
}
module.exports.analyse = async function () {
const timestamp = Date.now();
const index = `eco-${process.env.SERVER_UID}-${dateFormat(timestamp, "yyyy-mm")}`;
return new Promise((resolve, reject) => {
return es_client.search({
index: index,
// no body, search will return all results
}).then(async (success) => {
const hits = success.hits.hits.map(h => h._source);
if (hits.length === 0)
return reject({callback: Errors.no_data});
const g = await vegaRun(hits.map(h => ({x: h.timestamp, y: h.gold, c: 0})));
const o = await vegaRun(hits.map(h => ({x: h.timestamp, y: h.orn, c: 0})));
const f = await vegaRun(hits.map(h => ({x: h.timestamp, y: h.florin, c: 0})));
return resolve({attachments: {gold: g, orn: o, florin: f}});
}).catch((err) => console.error(err));
});
}
async function vegaRun(values) {
const last = values[values.length - 1];
const today = new Date();
const date = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
const timeseries = [...Array(31).keys()]
.map(i => ({x: date.setDate(i + 1), y: last.y, c: 8}))
.concat(values);
// create a new view instance for a given Vega JSON spec
const vegaJson = {
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A chart of the guild's economy",
"width": 700,
"height": 300,
"config": {
"axis": {
"labelColor": "white"
},
"axisY": {
"labelFontSize": 16
}
},
"padding": 5,
"signals": [
{"name": "interpolate", "value": "linear"}
],
"data": [
{
"name": "table",
"values": timeseries
}
],
"scales": [
{
"name": "x",
"type": "time",
"range": "width",
"nice": true,
"zero": false,
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": false,
"domain": {"data": "table", "field": "y"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "table", "field": "c"}
}
],
"axes": [
{"orient": "bottom", "scale": "x"},
{"orient": "left", "scale": "y"}
],
"marks": [
{ // allow strokeDash to have different behaviors by c
"type": "group",
"from": {
"facet": {
"name": "series",
"data": "table",
"groupby": "c"
}
},
"marks": [
{
"type": "line",
"from": {
"data": "series"
},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "y"},
"strokeDash": {"type":"nominal", "field":"c"},
"stroke": {"scale": "color", "field": "c"}
}
}
}
]
}
]
};
var view = new vega.View(vega.parse(vegaJson)).renderer('none').initialize();
return await view.toCanvas()
.then((canvas) => canvas.toBuffer())
.catch((err) => console.error(err));
}