Skip to content

Commit 970e435

Browse files
authored
Merge pull request #84 from CoreWorxLab/vastai-balance-usage
Enhance Vast.ai get_balance with usage details
2 parents db136ac + 7907149 commit 970e435

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

tools/utilities/vast-ai/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vast-ai",
33
"friendlyName": "Vast.ai",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"description": "Vast.ai - search GPU offers, rent and manage cloud GPU instances.",
66
"category": "utilities",
77
"toolSuite": true,

tools/utilities/vast-ai/workflow.json

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,41 @@
581581
},
582582
{
583583
"parameters": {
584-
"jsCode": "const data = $input.item.json;\n\nconst credit = data.credit || data.balance || 0;\nconst balance = typeof credit === 'number' ? credit.toFixed(2) : credit;\n\nreturn [{ json: { message: `Your Vast.ai balance is $${balance}.`, balance: parseFloat(balance) } }];"
584+
"url": "https://console.vast.ai/api/v0/instances/",
585+
"authentication": "predefinedCredentialType",
586+
"nodeCredentialType": "httpHeaderAuth",
587+
"options": {
588+
"response": {
589+
"response": {
590+
"responseFormat": "json"
591+
}
592+
}
593+
}
594+
},
595+
"onError": "continueErrorOutput",
596+
"type": "n8n-nodes-base.httpRequest",
597+
"typeVersion": 4.2,
598+
"position": [
599+
600,
600+
-50
601+
],
602+
"name": "Get Usage Instances",
603+
"credentials": {
604+
"httpHeaderAuth": {
605+
"id": null,
606+
"name": "${VASTAI_API_KEY}"
607+
}
608+
},
609+
"id": "a1b2c3d4-usage-inst-node"
610+
},
611+
{
612+
"parameters": {
613+
"jsCode": "const userData = $('Get Balance').item.json;\nconst instData = $input.item.json;\n\nconst credit = userData.credit || userData.balance || 0;\nconst balance = typeof credit === 'number' ? credit.toFixed(2) : credit;\n\n// Calculate usage from running instances\nconst instances = instData.instances || [];\nconst running = instances.filter(i => i.actual_status === 'running');\n\nlet gpuCostHr = 0;\nlet diskCostHr = 0;\n\nfor (const inst of running) {\n const dphBase = inst.dph_base || 0;\n const dphTotal = inst.dph_total || 0;\n gpuCostHr += dphBase;\n diskCostHr += (dphTotal - dphBase);\n}\n\nif (diskCostHr < 0) diskCostHr = 0;\n\nconst totalHr = gpuCostHr + diskCostHr;\nconst totalDay = totalHr * 24;\n\nlet timeRemaining = '';\nif (totalHr > 0) {\n const hoursLeft = parseFloat(balance) / totalHr;\n const days = Math.floor(hoursLeft / 24);\n const hours = Math.floor(hoursLeft % 24);\n const mins = Math.floor((hoursLeft * 60) % 60);\n const parts = [];\n if (days > 0) parts.push(`${days} day${days !== 1 ? 's' : ''}`);\n if (hours > 0) parts.push(`${hours} hour${hours !== 1 ? 's' : ''}`);\n if (mins > 0 && days === 0) parts.push(`${mins} minute${mins !== 1 ? 's' : ''}`);\n timeRemaining = parts.join(', ');\n}\n\nlet message = `Your Vast.ai balance is $${balance}.`;\nif (totalHr > 0) {\n message += ` Current usage is $${totalHr.toFixed(2)} per hour, or about $${Math.round(totalDay)} per day.`;\n if (timeRemaining) message += ` Credits will last approximately ${timeRemaining}.`;\n}\n\nconst totalSpent = userData.total_spend ? Math.abs(userData.total_spend) : 0;\n\nconst result = {\n message,\n balance: parseFloat(balance),\n total_spent: parseFloat(totalSpent.toFixed(2)),\n};\n\nif (totalHr > 0) {\n result.usage = {\n total_per_hour: parseFloat(totalHr.toFixed(4)),\n total_per_day: parseFloat(totalDay.toFixed(4)),\n gpu_per_hour: parseFloat(gpuCostHr.toFixed(4)),\n disk_per_hour: parseFloat(diskCostHr.toFixed(4)),\n running_instances: running.length,\n };\n if (timeRemaining) result.usage.time_remaining = timeRemaining;\n}\n\nreturn [{ json: result }];"
585614
},
586615
"type": "n8n-nodes-base.code",
587616
"typeVersion": 2,
588617
"position": [
589-
600,
618+
800,
590619
-100
591620
],
592621
"name": "Format Balance",
@@ -1095,7 +1124,7 @@
10951124
},
10961125
{
10971126
"parameters": {
1098-
"content": "## CAAL Registry Tracking\n**Tool Name:** vast-ai\n**Description:** Vast.ai - search GPU offers, rent and manage cloud GPU instances.\n**version:** v1.1.0\n**id:** AqH_6oxoqajm82hhTHdRAg\n**link:** [Registry](https://github.com/CoreWorxLab/caal-tools/tree/main/tools/utilities/vast-ai)\n\n### (Do not delete this sticky)",
1127+
"content": "## CAAL Registry Tracking\n**Tool Name:** vast-ai\n**Description:** Vast.ai - search GPU offers, rent and manage cloud GPU instances.\n**version:** v1.2.0\n**id:** AqH_6oxoqajm82hhTHdRAg\n**link:** [Registry](https://github.com/CoreWorxLab/caal-tools/tree/main/tools/utilities/vast-ai)\n\n### (Do not delete this sticky)",
10991128
"height": 260,
11001129
"width": 360
11011130
},
@@ -1320,6 +1349,24 @@
13201349
]
13211350
},
13221351
"Get Balance": {
1352+
"main": [
1353+
[
1354+
{
1355+
"node": "Get Usage Instances",
1356+
"type": "main",
1357+
"index": 0
1358+
}
1359+
],
1360+
[
1361+
{
1362+
"node": "Format Error",
1363+
"type": "main",
1364+
"index": 0
1365+
}
1366+
]
1367+
]
1368+
},
1369+
"Get Usage Instances": {
13231370
"main": [
13241371
[
13251372
{

0 commit comments

Comments
 (0)