|
24 | 24 | # |
25 | 25 | # Commands: |
26 | 26 | # hubot graf db <dashboard slug>[:<panel id>][ <template variables>][ <from clause>][ <to clause>] - Show grafana dashboard graphs |
27 | | -# hubot graf list - Lists all dashboards available |
| 27 | +# hubot graf list <tag> - Lists all dashboards available (optional: <tag>) |
| 28 | +# hubot graf search <keyword> - Search available dashboards by <keyword> |
28 | 29 | # |
29 | 30 |
|
30 | 31 | crypto = require 'crypto' |
@@ -142,31 +143,52 @@ module.exports = (robot) -> |
142 | 143 | sendRobotResponse msg, title, imageUrl, link |
143 | 144 |
|
144 | 145 | # Get a list of available dashboards |
145 | | - robot.respond /(?:grafana|graph|graf) list$/i, (msg) -> |
146 | | - callGrafana 'search', (dashboards) -> |
| 146 | + robot.respond /(?:grafana|graph|graf) list\s?(.+)?/i, (msg) -> |
| 147 | + if msg.match[1] |
| 148 | + tag = msg.match[1].trim() |
| 149 | + callGrafana "search?tag=#{tag}", (dashboards) -> |
| 150 | + robot.logger.debug dashboards |
| 151 | + response = "Dashboards tagged `#{tag}`:\n" |
| 152 | + sendDashboardList dashboards, response, msg |
| 153 | + else |
| 154 | + callGrafana 'search', (dashboards) -> |
| 155 | + robot.logger.debug dashboards |
| 156 | + response = "Available dashboards:\n" |
| 157 | + sendDashboardList dashboards, response, msg |
| 158 | + |
| 159 | + # Search dashboards |
| 160 | + robot.respond /(?:grafana|graph|graf) search (.+)/i, (msg) -> |
| 161 | + query = msg.match[2].trim() |
| 162 | + robot.logger.debug query |
| 163 | + callGrafana "search?query=#{query}", (dashboards) -> |
147 | 164 | robot.logger.debug dashboards |
148 | | - response = "Available dashboards:\n" |
| 165 | + response = "Dashboards matching `#{query}`:\n" |
| 166 | + sendDashboardList dashboards, response, msg |
| 167 | + |
| 168 | + # Send Dashboard list |
| 169 | + sendDashboardList = (dashboards, response, msg) -> |
| 170 | + # Handle refactor done for version 2.0.2+ |
| 171 | + if dashboards.dashboards |
| 172 | + list = dashboards.dashboards |
| 173 | + else |
| 174 | + list = dashboards |
| 175 | + |
| 176 | + robot.logger.debug list |
| 177 | + unless list.length > 0 |
| 178 | + return |
149 | 179 |
|
| 180 | + for dashboard in list |
150 | 181 | # Handle refactor done for version 2.0.2+ |
151 | | - if dashboards.dashboards |
152 | | - list = dashboards.dashboards |
| 182 | + if dashboard.uri |
| 183 | + slug = dashboard.uri.replace /^db\//, '' |
153 | 184 | else |
154 | | - list = dashboards |
155 | | - |
156 | | - robot.logger.debug list |
157 | | - |
158 | | - for dashboard in list |
159 | | - # Handle refactor done for version 2.0.2+ |
160 | | - if dashboard.uri |
161 | | - slug = dashboard.uri.replace /^db\//, '' |
162 | | - else |
163 | | - slug = dashboard.slug |
164 | | - response = response + "- #{slug}: #{dashboard.title}\n" |
| 185 | + slug = dashboard.slug |
| 186 | + response = response + "- #{slug}: #{dashboard.title}\n" |
165 | 187 |
|
166 | | - # Remove trailing newline |
167 | | - response.trim() |
| 188 | + # Remove trailing newline |
| 189 | + response.trim() |
168 | 190 |
|
169 | | - msg.send response |
| 191 | + msg.send response |
170 | 192 |
|
171 | 193 | # Handle generic errors |
172 | 194 | sendError = (message, msg) -> |
|
0 commit comments