-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-connection-only.js
More file actions
153 lines (132 loc) Β· 4.58 KB
/
Copy pathtest-connection-only.js
File metadata and controls
153 lines (132 loc) Β· 4.58 KB
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
require('dotenv').config();
const axios = require('axios');
// Configuration
const config = {
datadog: {
apiKey: process.env.DATADOG_API_KEY,
appKey: process.env.DATADOG_APP_KEY,
site: process.env.DATADOG_SITE || 'datadoghq.eu',
},
channels: [
{
id: process.env.SLACK_CHANNEL_1_ID,
scheduleId: process.env.SLACK_CHANNEL_1_SCHEDULE_ID,
name: 'c1-oncall-bot',
mode: 'general-all-teams'
},
{
id: process.env.SLACK_CHANNEL_2_ID,
scheduleId: process.env.SLACK_CHANNEL_2_SCHEDULE_ID,
name: 'system-production',
mode: 'topic-only'
}
]
};
/**
* Test Datadog API connection for a specific schedule
*/
async function testDatadogSchedule(scheduleId, scheduleName) {
try {
console.log(`\nπ Testing: ${scheduleName}`);
console.log(` Schedule ID: ${scheduleId}`);
// Get schedule details
const scheduleUrl = `https://api.${config.datadog.site}/api/v2/on-call/schedules/${scheduleId}`;
const scheduleResponse = await axios.get(scheduleUrl, {
headers: {
'DD-API-KEY': config.datadog.apiKey,
'DD-APPLICATION-KEY': config.datadog.appKey
}
});
const scheduleNameFromAPI = scheduleResponse.data.data.attributes.name;
console.log(` β
Schedule found: "${scheduleNameFromAPI}"`);
// Get current on-call
const onCallUrl = `https://api.${config.datadog.site}/api/v2/on-call/schedules/${scheduleId}/on-call`;
const onCallResponse = await axios.get(onCallUrl, {
headers: {
'DD-API-KEY': config.datadog.apiKey,
'DD-APPLICATION-KEY': config.datadog.appKey,
'Content-Type': 'application/json'
},
params: {
include: 'user'
}
});
const onCallData = onCallResponse.data;
if (onCallData && onCallData.data) {
const attributes = onCallData.data.attributes || {};
let user = null;
if (onCallData.included && onCallData.included.length > 0) {
user = onCallData.included.find(item => item.type === 'users');
}
const userName = user?.attributes?.name || user?.attributes?.email || 'Unknown User';
const userEmail = user?.attributes?.email || '';
const start = attributes.start ? new Date(attributes.start) : null;
const end = attributes.end ? new Date(attributes.end) : null;
console.log(` π€ Current On-Call: ${userName}`);
if (userEmail) console.log(` π§ Email: ${userEmail}`);
if (start && end) {
const startStr = start.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZone: 'America/Toronto'
});
const endStr = end.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZone: 'America/Toronto'
});
console.log(` β° Shift: ${startStr} - ${endStr} EST`);
}
return { success: true, userName, userEmail };
} else {
console.log(` β οΈ No one currently on-call`);
return { success: true, noOnCall: true };
}
} catch (error) {
console.error(` β Error: ${error.response?.data?.errors?.[0]?.detail || error.message}`);
return { success: false, error: error.message };
}
}
/**
* Test all configured channels
*/
async function testAllChannels() {
console.log('π Testing Datadog API Connection (NO messages will be sent to Slack)');
console.log('='.repeat(70));
const results = [];
for (const channel of config.channels) {
const result = await testDatadogSchedule(channel.scheduleId, channel.name);
results.push({
channel: channel.name,
mode: channel.mode,
...result
});
}
console.log('\n' + '='.repeat(70));
console.log('π Summary:');
console.log('='.repeat(70));
results.forEach(result => {
const status = result.success ? 'β
' : 'β';
console.log(`${status} ${result.channel} (${result.mode})`);
if (!result.success) {
console.log(` Error: ${result.error}`);
}
});
const allSuccess = results.every(r => r.success);
console.log('\n' + '='.repeat(70));
if (allSuccess) {
console.log('β
All channels are configured correctly!');
console.log('π€ Bot is ready and waiting for scheduled times:');
console.log(' β’ c1-oncall-bot: Every day at 9:00 AM EST');
console.log(' β’ system-production: Every Monday at 8:00 AM EST');
} else {
console.log('β οΈ Some channels have configuration issues.');
}
console.log('='.repeat(70));
}
// Run the test
testAllChannels().catch(console.error);