-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (107 loc) · 4.36 KB
/
index.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
const https = require('https');
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
//const name = (req.query.name || (req.body && req.body.name));
//const responseMessage = name
// ? "Hello, " + name + ". This HTTP triggered function executed successfully."
// : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
var newRelicUserKey = GetEnvironmentVariable("NEWRELIC_USER_KEY");
//context.log(newRelicUserKey);
var newRelicDashboardEntityGuid = GetEnvironmentVariable("NEWRELIC_DASHBOARD_ENTITYGUID");
//context.log(newRelicDashboardEntityGuid);
var refreshInterval = GetEnvironmentVariable("REFRESH_INTERVAL");
//context.log(refreshInterval);
var isRefreshPage = GetEnvironmentVariable("IS_REFRESH_PAGE");
//context.log(isRefreshPage);
var optionsNerdGraph = {
host: 'api.newrelic.com',
port: 443,
path: '/graphql',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-Key': newRelicUserKey
}
};
var graphQuery =
{
"query": "mutation {dashboardCreateSnapshotUrl(guid: \"" + newRelicDashboardEntityGuid + "\")}"
};
var refreshPageCode = '';
if (isRefreshPage == 'true') {
refreshPageCode = `setTimeout(onRefresh, ` + refreshInterval + `);`;
}
var content = `<!DOCTYPE html>
<html>
<head>
<script src='https://statics.teams.cdn.office.net/sdk/v1.6.0/js/MicrosoftTeams.min.js'></script>
<style>
.display-name {
font-family: "Segoe UI",system-ui,"Apple Color Emoji","Segoe UI Emoji",sans-serif;
}
</style>
</head>
<body>
<span class="display-name">Microsoft Teams User: </span><span id="user" class="display-name"></span>
<script>
microsoftTeams.initialize();
microsoftTeams.getContext((context) => {
let userId = document.getElementById('user');
userId.innerHTML = context.userPrincipalName;
});
function onRefresh() {
location.reload();
}
`+ refreshPageCode + `
</script>
<div>
#NEWRELIC#
</div>
<div>
<input type="button" name="Button" value="Refresh" onclick="onRefresh()">
</div>
</body>
</html>`;
var chunk = '';
const myReq = https.request(optionsNerdGraph, function (res) {
context.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', (d) => {
chunk += d;
});
res.on('end', (d) => {
var json = JSON.parse(chunk);
var imgUrl = json.data.dashboardCreateSnapshotUrl;
imgUrl = imgUrl.replace('?format=PDF', '?format=PNG');
context.log('imgUrl: ' + imgUrl);
var newrelicChart = `<img id="nrChart" src="` + imgUrl + `" />`;
content = content.replace('#NEWRELIC#', newrelicChart);
context.res = {
status: 200,
headers: {
'Content-Type': 'text/html'
},
body: content
}
context.done();
})
})
myReq.on('error', function (e) {
context.log('problem with request: ' + e.message);
content = content.replace('#NEWRELIC#', 'An error occured: ' + e.message);
context.res = {
status: 200,
headers: {
'Content-Type': 'text/html'
},
body: content
}
context.done();
});
//context.log('query: ' + JSON.stringify(graphQuery));
myReq.write(JSON.stringify(graphQuery));
myReq.end();
}
function GetEnvironmentVariable(name) {
return process.env[name];
}