+ "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 }];"
0 commit comments