Skip to content

Commit

Permalink
Merge pull request #185 from stephenyeargin/fix/multiple-responses
Browse files Browse the repository at this point in the history
Fix multiple responses on slow Grafana
  • Loading branch information
KeesCBakker authored May 13, 2024
2 parents 6299989 + c27aeb9 commit 15b9fb9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubot-grafana",
"description": "Query Grafana dashboards",
"version": "7.0.0",
"version": "7.0.1",
"author": "Stephen Yeargin <[email protected]>",
"license": "MIT",
"keywords": [
Expand Down
82 changes: 41 additions & 41 deletions src/grafana.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,17 @@ module.exports = (robot) => {
});

// Get a specific dashboard with options
robot.respond(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i, async (context) => {

robot.respond(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(.*)?/i, (context) => {
let str = context.match[1];
if (context.match[2]) {
str += ' ' + context.match[2];
}

await bot.sendDashboardChartFromString(context, str, maxReturnDashboards);
bot.sendDashboardChartFromString(context, str, maxReturnDashboards);
});

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

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

const dashboards = await service.search(null, tag);
if (dashboards == null) return;
sendDashboardList(dashboards, title, context);
service.search(null, tag).then(async (dashboards) => {
if (dashboards == null) return;
await sendDashboardList(dashboards, title, context);
});
});

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

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

const dashboards = await service.search(query);
if (dashboards == null) return;

const title = `Dashboards matching \`${query}\`:\n`;
sendDashboardList(dashboards, title, msg);
service.search(query).then(async (dashboards) => {
if (dashboards == null) return;
const title = `Dashboards matching \`${query}\`:\n`;
await sendDashboardList(dashboards, title, msg);
});
});

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

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

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

let alerts = await service.queryAlerts(state);
if (alerts == null) return;
service.queryAlerts(state).then((alerts) => {
if (alerts == null) return;

robot.logger.debug(alerts);
robot.logger.debug(alerts);

let text = title;
let text = title;

for (const alert of alerts) {
let line = `- *${alert.name}* (${alert.id}): \`${alert.state}\``;
if (alert.newStateDate) {
line += `\n last state change: ${alert.newStateDate}`;
}
if (alert.executionError) {
line += `\n execution error: ${alert.executionError}`;
for (const alert of alerts) {
let line = `- *${alert.name}* (${alert.id}): \`${alert.state}\``;
if (alert.newStateDate) {
line += `\n last state change: ${alert.newStateDate}`;
}
if (alert.executionError) {
line += `\n execution error: ${alert.executionError}`;
}
text += line + `\n`;
}
text += line + `\n`;
}
msg.send(text.trim());
msg.send(text.trim());
});
});

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

const message = service.pauseSingleAlert(alertId, paused);

if (message) {
msg.send(message);
}
service.pauseSingleAlert(alertId, paused).then((message) => {
if (message) {
msg.send(message);
}
});
});

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

const command = msg.match[1];
const paused = command === 'pause';
const result = await service.pauseAllAlerts(paused);

if (result.total == 0) return;

msg.send(
`Successfully tried to ${command} *${result.total}* alerts.\n*Success: ${result.success}*\n*Errored: ${result.errored}*`
);
service.pauseAllAlerts(paused).then((result) => {
if (result.total == 0) return;
msg.send(
`Successfully tried to ${command} *${result.total}* alerts.\n*Success: ${result.success}*\n*Errored: ${result.errored}*`
);
});
});

/**
Expand Down
9 changes: 5 additions & 4 deletions src/service/GrafanaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ class GrafanaService {
if (dashboard.templating.list) {
for (const template of Array.from(dashboard.templating.list)) {
this.logger.debug(template);
if (!template.current) {
continue;
}

const _param = req.template_params.find((param) => param.name === template.name);
templateMap[`$${template.name}`] = _param ? _param.value : template.current.text;
templateMap[`$${template.name}`] = _param
? _param.value
: template.current
? template.current.text
: `$${template.name}`;
}
}

Expand Down

0 comments on commit 15b9fb9

Please sign in to comment.