-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.js
executable file
·289 lines (249 loc) · 6.77 KB
/
export.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env node
/*
Run example:
Download as PDF:
./export.js --guid "dashboard-guid-here" --apikey "api-key-here" --filename "myfilename.pdf"
Slack:
./export.js --guid "dashboard-guid-here" --apikey "api-key-here" --slack "https://slack-webhook-url" --subject "Example subject line" --link "https://some-link-here"
*/
const {argv} = require('yargs')
.option('guid', {
type: 'string',
description: 'Dashboard GUID',
demandOption: true
})
.option('apikey', {
type: 'string',
description: 'API Key',
demandOption: true
})
.option('filename', {
type: 'string',
default: 'dashboard',
description: 'Export filename prefix'
})
.option('format', {
type: 'string',
default: 'pdf',
description: 'File output format - png or pdf'
})
.option('slack', {
type: 'string',
description: 'Slack webhook URL'
})
.option('subject', {
type: 'string',
default: '',
description: 'Slack subject'
})
.option('link', {
type: 'string',
description: 'Slack link'
})
.option('width', {
type: 'number',
default: 2000,
description: 'Width of snapshot in pixels'
})
const DASH_GUID = argv['guid']
const API_KEY = argv['apikey']
const OUTPUT_FILENAME = argv['filename']
const OUTPUT_FORMAT = argv['format']
const NR_HOST = "https://api.newrelic.com/graphql" // Using EU datacenter? use instead: https://api.eu.newrelic.com/graphiql
const SLACK_URL = argv['slack']
const SLACK_SUBJECT = argv['subject']
const SLACK_LINK = argv['link']
const WIDTH=argv['width'] //width of image
var https = require('https');
var fs = require('fs');
var $http = require("request");
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb);
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest);
if (cb) cb(err.message);
});
};
var generateSnapshot = function(apikey,guid) {
return new Promise((resolve, reject) => {
let options = {
url: NR_HOST,
method: 'POST',
headers : {
"Content-Type": "application/json",
"API-Key": apikey
},
body: JSON.stringify({
"query": `mutation { dashboardCreateSnapshotUrl(guid: \"${guid}\")}`
})
}
$http(options, function callback(error, response, body) {
if(error) {
reject(e)
} else {
try {
let bodyObj=JSON.parse(body)
if(bodyObj.data && bodyObj.data && bodyObj.data.dashboardCreateSnapshotUrl) {
//console.log("Snapshot URL: ",bodyObj.data.dashboardCreateSnapshotUrl)
resolve(bodyObj.data.dashboardCreateSnapshotUrl)
} else {
reject("Snapshot URL not found in body: "+body)
}
} catch (e) {
reject(e)
}
}
});
})
}
var getDashboardPages = function(apikey,guid) {
return new Promise((resolve, reject) => {
let options = {
url: NR_HOST,
method: 'POST',
headers : {
"Content-Type": "application/json",
"API-Key": apikey
},
body: JSON.stringify({
"query": `{
actor {
entitySearch(query: "parentId ='${guid}' or id ='${guid}'") {
results {
entities {
guid
name
... on DashboardEntityOutline {
guid
name
dashboardParentGuid
}
}
}
}
}
}
`
})
}
$http(options, function callback(error, response, body) {
if(error) {
reject(e)
} else {
try {
let bodyObj=JSON.parse(body)
let entities=bodyObj.data.actor.entitySearch.results.entities
if(entities.length > 0 ) {
if(entities.length>1) {
resolve(entities.filter((e)=>{return e.dashboardParentGuid!==null}))
} else {
resolve([entities[0]]) //one pager
}
} else {
reject("Error. No entities returned from search")
}
} catch (e) {
reject(e)
}
}
});
})
}
var notifySlack = function(url,subject, imageUrl, link) {
return new Promise((resolve, reject) => {
let blocks=[]
if(subject) { //add a subject title if there is one
blocks.push({
"type": "header",
"text": {
"type": "plain_text",
"text": subject,
"emoji": true
}
})
}
if(imageUrl) {
blocks.push(
{
"type": "image",
"image_url": imageUrl,
"alt_text": subject
}
)
}
if(link){
blocks.push(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "View this dashboard live in New Relic"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View in New Relic",
"emoji": true
},
"value": "click_me_123",
"url": link,
"action_id": "button-action"
}
}
)
}
let options = {
url: url,
method: 'POST',
headers : {
"Content-Type": "application/json"
},
body: JSON.stringify({
"blocks": blocks
})
}
$http(options, function callback(error, response, body) {
if(error) {
reject(e)
} else {
resolve()
}
});
})
}
async function run() {
let pages=await getDashboardPages(API_KEY,DASH_GUID)
await asyncForEach(pages,async (page,zeroIdx)=>{
let idx=zeroIdx+1
let PDF_URL = await generateSnapshot(API_KEY,page.guid)
if(SLACK_URL) {
const PNG_URL=PDF_URL.replace("format=PDF","format=PNG")+`&width=${WIDTH}`
console.log(`Posting page '${page.name}' to slack...`)
await notifySlack(SLACK_URL,`${SLACK_SUBJECT}${SLACK_SUBJECT=="" ? "" : " - "}${page.name}`,PNG_URL,SLACK_LINK)
} else {
let filename=`${OUTPUT_FILENAME}${pages.length>1 ? "_"+idx : ""}.${OUTPUT_FORMAT}`
download(PDF_URL.replace("format=PDF",`format=${OUTPUT_FORMAT.toUpperCase()}`)+`&width=${WIDTH}`,filename,(e)=>{
if(e) {
console.log(`Something went wrong with download of file ${filename}`)
} else {
console.log(`Page '${page.name}' saved to ${filename}`)
}
})
}
})
}
run().catch((e)=>{
console.log("Error occurred!\n\n",e)
process.exit(1)
})