-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
289 lines (243 loc) · 8.79 KB
/
app.js
File metadata and controls
289 lines (243 loc) · 8.79 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
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
// TensorBot v3
// A Discord bot to Monitor TensorDock Servers
// Created by Uncharted@ ~2024
require('dotenv').config();
const request = require('request');
const cron = require('node-cron');
const qs = require('qs');
const { Client, ChannelType, GatewayIntentBits } = require('discord.js');
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
});
const storage = [];
const botToken = process.env.BOT_TOKEN;
const apiKey = process.env.API_KEY;
const apiToken = process.env.API_TOKEN;
const guildID = process.env.GUILD_ID;
bot.login(botToken);
let summaryData;
let hostnodes;
let guild;
bot.on('ready', () => {
console.log('Bot ready!');
guild = bot.guilds.cache.get(guildID);
updateMessages();
});
async function fetchData() {
try {
const data = {
'api_key': apiKey,
'api_token': apiToken,
'period': getThisMonth()
};
const options = {
'method': 'POST',
'url': 'https://marketplace.tensordock.com/api/v0/billing/summary',
'headers': {},
'form': data
};
const response = await new Promise((resolve, reject) => {
request(options, (error, response) => {
if (error) reject(error);
else resolve(response);
});
});
const retdata = JSON.parse(response.body);
//console.log(retdata);
return retdata;
} catch (error) {
console.error(error);
throw error; // Re-throw the error to handle it properly
}
}
async function fetchRevenue(startTimestamp, endTimestamp, hostnodeID) {
try {
const data = {
'api_key': apiKey,
'api_token': apiToken,
'start_timestamp': startTimestamp,
'end_timestamp': endTimestamp,
'hostnode_id': hostnodeID
};
const options = {
'method': 'POST',
'url': 'https://marketplace.tensordock.com/api/v0/billing/revenue',
'headers': {},
'form': data
};
const response = await new Promise((resolve, reject) => {
request(options, (error, response) => {
if (error) reject(error);
else resolve(response);
});
});
const retdata = JSON.parse(response.body);
//console.log(retdata);
return retdata;
} catch (error) {
console.error(error);
throw error; // Re-throw the error to handle it properly
}
}
async function updateMessages() {
try {
const summaryResponse = await fetchData();
console.log(summaryResponse);
if (!Array.isArray(summaryResponse.transactions.vm_payouts)) {
throw new Error('Invalid data format'); // added .hostnodes
}
const payouts = summaryResponse.transactions.vm_payouts;
const uniqueHostnodes = new Set();
payouts.forEach((payout) => {
uniqueHostnodes.add(payout.hostnode_id);
});
hostnodes = Array.from(uniqueHostnodes);;
console.log(hostnodes);
// Fetch revenue data
const now = new Date();
const currentYear = now.getFullYear();
const currentMonth = now.getMonth() + 1; // Months are 0-based, so add 1 to get the correct month number
const firstDay = new Date(currentYear, currentMonth - 1, 1); // Get the first day of the month
const today = new Date();
const startDateStr = `${firstDay.getFullYear()}-${('0' + (firstDay.getMonth() + 1)).slice(-2)}-${('0' + firstDay.getDate()).slice(-2)}`;
const endDateStr = `${today.getFullYear()}-${('0' + (today.getMonth() + 1)).slice(-2)}-${('0' + today.getDate()).slice(-2)}`;
console.log(startDateStr);
console.log(endDateStr);
const revenueData = await Promise.all(hostnodes.map(async (hostnode) => {
try {
const retRevData = await fetchRevenue(startDateStr, endDateStr, hostnode);
console.log('---**--');
console.log(retRevData);
console.log('---**--');
return retRevData.result.data;
} catch (error) {
console.error(error);
throw error; // Re-throw the error to handle it properly
}
}));
// Create channels for each hostnode
const hostnodeChannels = {};
for (const hostnode of hostnodes) {
const channelName = `${hostnode}`;
let existingChannel = guild.channels.cache.find((channel) => channel.name === channelName);
if (!existingChannel) {
try {
const createdChannel = await guild.channels.create({ name: channelName, type: ChannelType.GuildText, parent: guild.channels.cache.find((channel) => channel.name === 'TensorDock') });
hostnodeChannels[channelName] = createdChannel;
} catch (error) {
console.error(error);
}
} else {
hostnodeChannels[channelName] = existingChannel;
}
}
// Delete old messages
for (const [channelId, channel] of Object.entries(hostnodeChannels)) {
//console.log("*");
//console.log(channelId);
//console.log(channel);
//console.log("*");
if (channel.id) {
//console.log(channel.id);
//const channelObj = guild.channels.cache.get(channel.id);
//if (!channelObj) continue;
const messages = await channel.messages.fetch({ limit: 100 });
if (messages.size > 0) {
await messages.forEach(message => message.delete());
}
}
}
// Create the Embeds
const hardwareEmbeds = [];
const revenueEmbeds = [];
const activeVMsEmbeds = [];
revenueData.forEach((hostnode) => {
//hostnode = hostnode.result.data; // Add .result.data to each hostnode
const hardwareEmbed = {
title: `Hostnode ${hostnode.hostnode_id} - Hardware`,
description: `Host Hardware`,
fields: [
{
name: 'Storage',
value: `**Used:** ${hostnode.used_storage} GB\n**Available:** ${hostnode.available_storage} GB`,
},
{
name: 'GPUs',
value: `**Used:** ${hostnode.used_gpus}\n**Available:** ${hostnode.available_gpus}`,
},
{
name: 'CPUs',
value: `**Used:** ${hostnode.used_cpus}\n**Available:** ${hostnode.available_cpus}`,
},
{
name: 'RAM',
value: `**Used:** ${hostnode.used_ram} GB\n **Available:** ${hostnode.available_ram} GB`,
},
],
};
hardwareEmbeds.push(hardwareEmbed);
const revenueEmbed = {
title: `Hostnode ${hostnode.hostnode_id} - Revenue`,
description: `Per Hour Total(s)`,
fields: [
{ name: 'Storage', value: `${hostnode.revenue_storage} USD` },
{ name: 'Compute', value: `${hostnode.revenue_compute} USD` },
{ name: 'Total', value: `${Number(hostnode.revenue_storage) + Number(hostnode.revenue_compute)} USD` },
],
};
revenueEmbeds.push(revenueEmbed);
console.log('-----');
console.log(hostnode);
console.log('-----');
const activeVMsTitle = `Hostnode ${hostnode.hostnode_id} - Active VMs`;
const activeVMsDescription = `Location: ${hostnode.location}`;
let activeVMsFields;
if (hostnode.virtual_machines) {
activeVMsFields = hostnode.virtual_machines.map((vm) => ({
name: `VM ID: ${vm.id}`,
value: `Used Storage: ${vm.used_storage} GB, Used GPUs: ${vm.used_gpus}, Used CPUs: ${vm.used_cpus}, Used RAM: ${vm.used_ram} GB, Revenue Storage: ${vm.revenue_storage}, Revenue Compute: ${vm.revenue_compute}`
}));
} else {
activeVMsFields = [{ name: 'No active VMs found', value: 'No active VMs found' }];
}
const activeVMsEmbed = {
title: activeVMsTitle,
description: activeVMsDescription,
fields: activeVMsFields
};
// Send embeds
try {
//const channel = `${hostnodeChannels[hostnode.hostnode_id]}`;
//const channel = guild.channels.cache.find(channel => `${channel.name}` === hostnode.hostnode_id);
for (const [channelId, channel] of Object.entries(hostnodeChannels)) {
console.log('**---');
console.log(hostnode.hostnode_id);
console.log(hostnode.hostnode_id);
console.log(channelId);
//console.log(`${channelId}`);
console.log('**---');
if (`${channelId}` === hostnode.hostnode_id) {
console.log("if channel");
console.log(channel);
//const realchannel = guild.channels.cache.get(channelId);
channel.send({ embeds: [hardwareEmbed, revenueEmbed, ...activeVMsEmbeds] });
}
}
} catch (error) {
console.error(error);
}
});
} catch (error) {
console.error(error);
}
}
function getThisMonth() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
return `${year}-${padZero(month)}`;
}
function padZero(num) {
return num < 10 ? `0${num}` : `${num}`;
}
cron.schedule('*/5 * * * *', updateMessages);