hubotã§influxdbã®åéãã¼ã¿ãã°ã©ãåãã¦Slackã«ã¢ãããã¼ããã
èªé¯ã®æ§è½å¤ç®¡çã«ãInfluxdbãã¯ããã¨ããTICKã¹ã¿ãã¯ã使ã£ã¦ãã®ã ãã©ããªããé¾å¤è¨å®ããã¾ããããªãã¦ãé¾å¤ãã¾ãããã³ã«Kapacitorãã¢ã©ã¼ããæãã¦ãã¦ãã£ã¨ããããªã£ã¦ããã
ããã§ãhubotã§å®æçã«åéãããã¼ã¿ãã°ã©ãã«ãã¦ãslackã«ã¢ãããã¼ãããããã«ãã¦ã¿ãã
ã½ã¼ã¹ã³ã¼ãã¯ãã¡ã
ã°ã©ãåã«ã¯ãchartjs-nodeã使ã£ã¦ãããcanvasã ã£ããchartjsãd3ã®Node.jsç¨ã©ã¤ãã©ãªãããããã¨ãã£ã¦ãã¨ãã«æè¿å®ã¯ã¹ãã¤ã¦ã§ã¢æ··å ¥ãã¦ãäºä»¶ã¨ãããã£ã¦å¿é ãªãã ãã©ãå¤å大ä¸å¤«ã®ã¯ãã
ã¾ããHubotã¯ä¸è¬çã«ã¯coffee-scriptã使ã£ã¦æ¸ãããã ãã©ããã¾ããcoffee-scriptã¯å¾®å¦ã ã¨æã£ã¦ãpure javascriptã§æ¸ãã¦ãã
var chartNode = new ChartjsNode(400, 300); request.get({url:'http://localhost:8086/query?pretty=true&db=monitoring&q='+encodeURIComponent('select mean(' + key +') from ' + name + ' where time > now() - 24h GROUP BY TIME(10m)')}, function(error, response, body) { if (error) { console.log(error); console.log(response); return; } var parsedJson = JSON.parse(body); var label = []; var data = []; for (let value of parsedJson['results'][0]['series'][0]['values']) { var d = new Date(value[0]); label.push(("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":" + ("0" + d.getSeconds()).slice(-2)); data.push(value[1]); } chartNode.drawChart( { type: 'line', data: { labels: label, datasets: [{ label: parsedJson['results'][0]['series'][0]['name'], data: data, backgroundColor: "rgba(255, 0, 0, 0.5)" }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: false } }] } }}) .then(() => { return chartNode.getImageStream('image/png'); }) .then(streamResult => { return chartNode.writeImageToFile('image/png', '/tmp/testimage.png'); }) }); }); };
request使ã£ã¦ãjsonåã®ãã¼ã¿ãinfluxdbããåå¾ãã¦ãæçµçã«/tmp/testimage.pngãä½ãã
ãããªã«è¤éãªãã¨ãã¦ãªãã¤ããã
request.post({url:'https://slack.com/api/files.upload', formData: { token: process.env.HUBOT_SLACK_TOKEN, filename: 'testimage.png', file: fs.createReadStream('/tmp/testimage.png'), channels: 'develop' }}, function(error, response, body) { });
ä½ã£ã/tmp/testimage.pngãslackã«ã¢ãããã¼ãããã
ã¢ãããã¼ããããã°ã©ãããããªæã
ãããããã§ããã