This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vividcortex.coffee
135 lines (99 loc) · 3.89 KB
/
vividcortex.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Description:
# Renders VividCortex components as images
#
# Configuration:
# HUBOT_VC_ORGANIZATION # This is the organization nickname, you'll see it in the VividCortex app URL
# HUBOT_VC_ENVIRONMENT # This is the environment ID, you'll see it in the VividCortex app URL too
# HUBOT_VC_TOKEN # This is the same token used to install VividCortex
#
# Commands:
# hubot <vividcortex|vc> <top-queries|query-compare|top-processes> last <count> <seconds|minutes|hours|days|month> - Generate a capture of the specified component
# hubot <vividcortex|vc> <top-queries|query-compare|top-processes> last minute|hour|month
# hubot <vividcortex|vc> query-compare last N seconds
# hubot <vividcortex|vc> top-processes last 10 minutes
# hubot <vividcortex|vc> top-queries last 20 days
# hubot <vividcortex|vc> share top-queries last 3 months
#
# Notes:
# - Does not support graphs at the moment
# - The second parameter only accepts "last" as a option, the plan is making it support multiple values like timestamps.
#
# Author:
# cesarvarela
# VividCortex api URL
api = "https://app.vividcortex.com/api/v2"
# Config
ORGANIZATION = process.env.HUBOT_VC_ORGANIZATION
ENVIRONMENT = process.env.HUBOT_VC_ENVIRONMENT
TOKEN = process.env.HUBOT_VC_TOKEN
module.exports = (robot) ->
environmentIsOk = (msg) ->
unless ORGANIZATION?
msg.send "Missing HUBOT_VC_ORGANIZATION in environment: please set and try again"
return false
unless ENVIRONMENT?
msg.send "Missing HUBOT_VC_ENVIRONMENT in environment: please set and try again"
return false
unless TOKEN?
msg.send "Missing HUBOT_VC_TOKEN in environment: please set and try again"
return false
return true # thanks coffeescript, but I like my returns
urls =
# Customizing this URLS allow us to add/remove columns, filters, sort by, etc.
"top-queries" : "/top-queries?limit=5&hosts="
"query-compare" : "/query-compare?limit=5&hosts="
"top-processes" : "/top-processes?limit=5&hosts="
# Generates the url to be loaded before generating the screenshot,
# the <since> parameter is going to be used in the future
getURL = (component, since, count, unit) ->
base = "/#{ORGANIZATION}/#{ENVIRONMENT}"
from = 0
till = 0
from = switch unit
when "seconds", "second" then 1
when "minutes", "minute" then 60
when "hours", "hour" then 3600
when "days", "day" then 3600 * 24
when "months", "month" then 3600 * 24 * 30
else from = 3600
range = "&from=" + (-from * count) + "&until=" + till
url = base + urls[component] + range
return url
# Creates the api-share request
loadShare = (config, msg) ->
msg.send "Capturing #{config.component}, say cheese..."
robot.http("#{api}/share/capture?selector=" + encodeURIComponent(config.selector) + "&url=" + encodeURIComponent(config.url))
.header('Accept', 'application/json')
.header('Authorization', "Bearer #{TOKEN}")
.get() (err, res, body) ->
try
data = JSON.parse(body)
catch e
msg.send "Invalid api-share response: #{body}"
if data and 'url' of data
msg.send data.url
# Respond callback
robot.respond /(vividcortex|vc) (.+)/i, (msg) ->
unless environmentIsOk msg
return
commandArray = msg.match[2].replace(/^\s+|\s+$/g, "").split(/\s+/)
component = commandArray[0]
since = commandArray[1]
if commandArray[2]
if commandArray[2].match /^\d+$/ # check if user specified a number
count = parseInt commandArray[2]
unit = commandArray[3]
else
count = 1
unit = commandArray[2]
else
count = 1
unit = "hour"
if component of urls
config =
component: component
selector: "[vc-shareable=\"#{component}\"]"
url: getURL(component, since, count, unit, msg)
loadShare(config, msg)
else
msg.send "Component #{component} not defined."