Skip to content

Commit 15b9fb9

Browse files
authored
Merge pull request #185 from stephenyeargin/fix/multiple-responses
Fix multiple responses on slow Grafana
2 parents 6299989 + c27aeb9 commit 15b9fb9

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hubot-grafana",
33
"description": "Query Grafana dashboards",
4-
"version": "7.0.0",
4+
"version": "7.0.1",
55
"author": "Stephen Yeargin <stephen@yearg.in>",
66
"license": "MIT",
77
"keywords": [

src/grafana.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,17 @@ module.exports = (robot) => {
7373
});
7474

7575
// Get a specific dashboard with options
76-
robot.respond(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i, async (context) => {
77-
76+
robot.respond(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i, (context) => {
7877
let str = context.match[1];
7978
if (context.match[2]) {
8079
str += ' ' + context.match[2];
8180
}
8281

83-
await bot.sendDashboardChartFromString(context, str, maxReturnDashboards);
82+
bot.sendDashboardChartFromString(context, str, maxReturnDashboards);
8483
});
8584

8685
// Get a list of available dashboards
87-
robot.respond(/(?:grafana|graph|graf) list\s?(.+)?/i, async (context) => {
86+
robot.respond(/(?:grafana|graph|graf) list\s?(.+)?/i, (context) => {
8887
const service = bot.createService(context);
8988
if (!service) return;
9089

@@ -95,28 +94,29 @@ module.exports = (robot) => {
9594
title = `Dashboards tagged \`${tag}\`:\n`;
9695
}
9796

98-
const dashboards = await service.search(null, tag);
99-
if (dashboards == null) return;
100-
sendDashboardList(dashboards, title, context);
97+
service.search(null, tag).then(async (dashboards) => {
98+
if (dashboards == null) return;
99+
await sendDashboardList(dashboards, title, context);
100+
});
101101
});
102102

103103
// Search dashboards
104-
robot.respond(/(?:grafana|graph|graf) search (.+)/i, async (msg) => {
104+
robot.respond(/(?:grafana|graph|graf) search (.+)/i, (msg) => {
105105
const service = bot.createService(msg);
106106
if (!service) return;
107107

108108
const query = msg.match[1].trim();
109109
robot.logger.debug(query);
110110

111-
const dashboards = await service.search(query);
112-
if (dashboards == null) return;
113-
114-
const title = `Dashboards matching \`${query}\`:\n`;
115-
sendDashboardList(dashboards, title, msg);
111+
service.search(query).then(async (dashboards) => {
112+
if (dashboards == null) return;
113+
const title = `Dashboards matching \`${query}\`:\n`;
114+
await sendDashboardList(dashboards, title, msg);
115+
});
116116
});
117117

118118
// Show alerts
119-
robot.respond(/(?:grafana|graph|graf) alerts\s?(.+)?/i, async (msg) => {
119+
robot.respond(/(?:grafana|graph|graf) alerts\s?(.+)?/i, (msg) => {
120120
const service = bot.createService(msg);
121121
if (!service) return;
122122

@@ -131,24 +131,25 @@ module.exports = (robot) => {
131131

132132
robot.logger.debug(title.trim());
133133

134-
let alerts = await service.queryAlerts(state);
135-
if (alerts == null) return;
134+
service.queryAlerts(state).then((alerts) => {
135+
if (alerts == null) return;
136136

137-
robot.logger.debug(alerts);
137+
robot.logger.debug(alerts);
138138

139-
let text = title;
139+
let text = title;
140140

141-
for (const alert of alerts) {
142-
let line = `- *${alert.name}* (${alert.id}): \`${alert.state}\``;
143-
if (alert.newStateDate) {
144-
line += `\n last state change: ${alert.newStateDate}`;
145-
}
146-
if (alert.executionError) {
147-
line += `\n execution error: ${alert.executionError}`;
141+
for (const alert of alerts) {
142+
let line = `- *${alert.name}* (${alert.id}): \`${alert.state}\``;
143+
if (alert.newStateDate) {
144+
line += `\n last state change: ${alert.newStateDate}`;
145+
}
146+
if (alert.executionError) {
147+
line += `\n execution error: ${alert.executionError}`;
148+
}
149+
text += line + `\n`;
148150
}
149-
text += line + `\n`;
150-
}
151-
msg.send(text.trim());
151+
msg.send(text.trim());
152+
});
152153
});
153154

154155
// Pause/unpause an alert
@@ -159,28 +160,27 @@ module.exports = (robot) => {
159160
const paused = msg.match[1] === 'pause';
160161
const alertId = msg.match[2];
161162

162-
const message = service.pauseSingleAlert(alertId, paused);
163-
164-
if (message) {
165-
msg.send(message);
166-
}
163+
service.pauseSingleAlert(alertId, paused).then((message) => {
164+
if (message) {
165+
msg.send(message);
166+
}
167+
});
167168
});
168169

169170
// Pause/unpause all alerts
170171
// requires an API token with admin permissions
171-
robot.respond(/(?:grafana|graph|graf) (unpause|pause) all(?:\s+alerts)?/i, async (msg) => {
172+
robot.respond(/(?:grafana|graph|graf) (unpause|pause) all(?:\s+alerts)?/i, (msg) => {
172173
const service = bot.createService(msg);
173174
if (!service) return;
174175

175176
const command = msg.match[1];
176177
const paused = command === 'pause';
177-
const result = await service.pauseAllAlerts(paused);
178-
179-
if (result.total == 0) return;
180-
181-
msg.send(
182-
`Successfully tried to ${command} *${result.total}* alerts.\n*Success: ${result.success}*\n*Errored: ${result.errored}*`
183-
);
178+
service.pauseAllAlerts(paused).then((result) => {
179+
if (result.total == 0) return;
180+
msg.send(
181+
`Successfully tried to ${command} *${result.total}* alerts.\n*Success: ${result.success}*\n*Errored: ${result.errored}*`
182+
);
183+
});
184184
});
185185

186186
/**

src/service/GrafanaService.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ class GrafanaService {
160160
if (dashboard.templating.list) {
161161
for (const template of Array.from(dashboard.templating.list)) {
162162
this.logger.debug(template);
163-
if (!template.current) {
164-
continue;
165-
}
166163

167164
const _param = req.template_params.find((param) => param.name === template.name);
168-
templateMap[`$${template.name}`] = _param ? _param.value : template.current.text;
165+
templateMap[`$${template.name}`] = _param
166+
? _param.value
167+
: template.current
168+
? template.current.text
169+
: `$${template.name}`;
169170
}
170171
}
171172

0 commit comments

Comments
 (0)