-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor the creation of the Grafana Client to the Grafan Client Factory. * Remove only * Make sure we forbid only on commit and push * Refactor response to be easier to read. Fail fast. * Small refactoring to make code more readable. * refactored pausing / resuming of alerts to the service. * refactor single alert * refactor alert querying into the service * remove send alerts, as it is refactored. * refactor search out * refactor get dashboard out * Version bump & make failing tests execute faster. * Add prettier support to dev container. * formatting * harmonize with the rest of the code * add missing ';' * Move object around and refactor parseToGrafanaDashboardRequest and getScreenshotUrls * Harmonize code * Fix fetch testing. * Was missing co-pilot in the dev env. * Implementation of the bot. * Refactor types. * Combine the processing of the string to Grafana dashboards. * Fix return type. * Improve documentation with types. Move `nock.cleanAll` to the right `setupNock`. * Add tests for pausing and unpausing all alerts. Improves code coverage. * Add tests for Grafana service processing and response handling. Improves code coverage. * The "should respond with a png graph in the default s3 region" test takes a bit longer at my machine, so add some more timeout to prevent false negatives. * Fix bug with template values not showing up in the title. Improved test coverage. The test template dashboard now has a parameterized title for the Graph panel (Graph for $server). * Add getUidBySlug for the Grafana Service. * Fix typing. * Rename `DashboardResponse` to `DashboardChart` so it does not look like `GrafanaDashboardResponse.Response`. * These defaults work better with TypeScript. * Make it possible to change the output by overriding the system with a custom responder. * Naming. * No need for the GrafanaClientFactory now that we have the Bot. * Rename bot.js to Bot.js * Restore naming * Restore strict * Restore http * Restore http * Change formatting * ship types.d.ts as well
- Loading branch information
1 parent
ad009c8
commit c932fe4
Showing
20 changed files
with
1,533 additions
and
646 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npm test | ||
npm test -- --forbid-only --forbid-pending |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npm test | ||
npm test -- --forbid-only --forbid-pending |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "hubot-grafana", | ||
"description": "Query Grafana dashboards", | ||
"version": "5.1.2", | ||
"version": "6.0.0", | ||
"author": "Stephen Yeargin <[email protected]>", | ||
"license": "MIT", | ||
"keywords": [ | ||
|
@@ -39,7 +39,7 @@ | |
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha \"test/**/*.js\" --reporter spec", | ||
"test": "mocha \"test/**/*.js\" --reporter spec --no-experiemental-fetch --timeout 200", | ||
"test-with-coverage": "nyc --reporter=text mocha \"test/**/*.js\" --reporter spec", | ||
"bootstrap": "script/bootstrap", | ||
"prepare": "husky install", | ||
|
@@ -49,7 +49,8 @@ | |
"src/**/*.js", | ||
"CONTRIBUTING.md", | ||
"LICENSE", | ||
"index.js" | ||
"index.js", | ||
"types.d.ts" | ||
], | ||
"volta": { | ||
"node": "18.19.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const { Adapter } = require('./adapters/Adapter'); | ||
const { GrafanaService } = require('./service/GrafanaService'); | ||
const { GrafanaClient } = require('./grafana-client'); | ||
|
||
/** | ||
* The bot brings the Adapter and the Grafana Service together. | ||
* It can be used for uploading charts and sending responses out. | ||
*/ | ||
class Bot { | ||
/** | ||
* Represents the Bot class. | ||
* @constructor | ||
* @param {Hubot.Robot} robot - The robot instance. | ||
*/ | ||
constructor(robot) { | ||
/** @type {Adapter} */ | ||
this.adapter = new Adapter(robot); | ||
|
||
/** @type {Hubot.Log} */ | ||
this.logger = robot.logger; | ||
} | ||
|
||
/** | ||
* Creates a new Grafana service based on the provided message. | ||
* @param {Hubot.Response} context - The context object. | ||
* @returns {GrafanaService|null} - The created Grafana service or null if the client is not available. | ||
*/ | ||
createService(context) { | ||
|
||
const robot = context.robot; | ||
let host = process.env.HUBOT_GRAFANA_HOST; | ||
let apiKey = process.env.HUBOT_GRAFANA_API_KEY; | ||
|
||
if (process.env.HUBOT_GRAFANA_PER_ROOM === '1') { | ||
const room = this.getRoom(context); | ||
host = robot.brain.get(`grafana_host_${room}`); | ||
apiKey = robot.brain.get(`grafana_api_key_${room}`); | ||
} | ||
|
||
if (host == null) { | ||
this.sendError('No Grafana endpoint configured.', context); | ||
return null; | ||
} | ||
|
||
let client = new GrafanaClient(robot.http, robot.logger, host, apiKey); | ||
return new GrafanaService(client); | ||
} | ||
|
||
/** | ||
* Sends a dashboard chart. | ||
* | ||
* @param {Hubot.Response} context - The context object. | ||
* @param {DashboardChart} dashboard - The dashboard object. | ||
* @returns {Promise<void>} - A promise that resolves when the chart is sent. | ||
*/ | ||
async sendDashboardChart(context, dashboard) { | ||
if (!this.adapter.isUploadSupported()) { | ||
this.adapter.responder.send(context, dashboard.title, dashboard.imageUrl, dashboard.grafanaChartLink); | ||
return; | ||
} | ||
|
||
const service = this.createService(context); | ||
if (service == null) return; | ||
|
||
/** @type {DownloadedFile|null} */ | ||
let file = null; | ||
|
||
try { | ||
file = await service.client.download(dashboard.imageUrl); | ||
} catch (err) { | ||
this.sendError(err, context); | ||
return; | ||
} | ||
|
||
this.logger.debug(`Uploading file: ${file.body.length} bytes, content-type[${file.contentType}]`); | ||
this.adapter.uploader.upload(context, dashboard.title || 'Image', file, dashboard.grafanaChartLink); | ||
} | ||
|
||
/** | ||
* *Sends an error message. | ||
* @param {string} message the error message. | ||
* @param {Hubot.Response} context The context. | ||
*/ | ||
sendError(message, context) { | ||
context.robot.logger.error(message); | ||
context.send(message); | ||
} | ||
|
||
/** | ||
* Gets the room from the context. | ||
* @param {Hubot.Response} context The context. | ||
* @returns {string} | ||
*/ | ||
getRoom(context) { | ||
// placeholder for further adapter support (i.e. MS Teams) as then room also | ||
// contains thread conversation id | ||
return context.envelope.room; | ||
} | ||
} | ||
|
||
exports.Bot = Bot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.