File size: 7,120 Bytes
c225a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// custom_nodes/FL_SystemCheck.js

import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";

app.registerExtension({
    name: "FL.SystemCheck",
    async nodeCreated(node) {
        if (node.comfyClass === "FL_SystemCheck") {
            node.color = "#2a363b";
            node.bgcolor = "#4F0074";
            node.systemInfo = null;
            node.animationOffset = 0;

            // Add button widget
            const widget = node.addWidget("button", "Run System Check", null, () => {
                runSystemCheck(node);
            });
            widget.serialize = false;

            // Override the default drawing behavior
            node.onDrawForeground = function(ctx) {
                if (this.systemInfo) {
                    drawSystemInfo(ctx, this);
                }
            };

            // Start animation loop
            animateNode(node);
        }
    },
});

function animateNode(node) {
    node.animationOffset += 0.005; // Adjust speed as needed
    if (node.animationOffset > 1) node.animationOffset -= 1;
    node.setDirtyCanvas(true);
    requestAnimationFrame(() => animateNode(node));
}

async function runSystemCheck(node) {
    try {
        const response = await api.fetchApi('/fl_system_info');
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        node.systemInfo = await response.json();
        // Remove ENV variables
        delete node.systemInfo["Env: PYTHONPATH"];
        delete node.systemInfo["Env: CUDA_HOME"];
        delete node.systemInfo["Env: LD_LIBRARY_PATH"];
        node.setDirtyCanvas(true);
    } catch (error) {
        console.error("Error fetching system info:", error);
        node.systemInfo = { "Error": "Failed to fetch system information. Check console for details." };
        node.setDirtyCanvas(true);
    }
}

function drawSystemInfo(ctx, node) {
    const margin = 15;
    const componentHeight = 30;
    const componentSpacing = 10;
    let y = 60; // Start below the node title and button

    ctx.save();
    ctx.font = "12px Arial";

    const components = Object.entries(node.systemInfo).map(([key, value]) => ({
        key,
        value,
        icon: getIconForKey(key),
        color: getColorForKey(key)
    }));

    // Calculate max width
    let maxWidth = 0;
    components.forEach(comp => {
        ctx.font = "bold 12px Arial";
        const labelWidth = ctx.measureText(comp.key).width;
        ctx.font = "12px Arial";
        const valueWidth = ctx.measureText(comp.value).width;
        const componentWidth = labelWidth + valueWidth + 80; // 80 for padding and icon
        maxWidth = Math.max(maxWidth, componentWidth);
    });

    // Draw components
    components.forEach((comp, index) => {
        const staggeredOffset = (node.animationOffset + index * 0.1) % 1; // Stagger the animation
        drawComponent(ctx, comp.key, comp.value, y, maxWidth, componentHeight, comp.color, comp.icon, staggeredOffset);
        y += componentHeight + componentSpacing;
    });

    // Adjust node size
    node.size[0] = maxWidth + 2 * margin;
    node.size[1] = y + margin;

    ctx.restore();
}

function drawComponent(ctx, label, value, y, width, height, color, icon, animationOffset) {
    const radius = 5;
    const iconWidth = 30;
    const padding = 5;

    // Draw component background
    ctx.fillStyle = color;
    roundRect(ctx, 15, y, width, height, radius, true, false);

    // Draw icon background
    ctx.fillStyle = lightenColor(color, 20);
    roundRect(ctx, 15, y, iconWidth, height, radius, true, false);

    // Draw icon (centered)
    ctx.font = "16px Arial";
    ctx.fillStyle = "#ffffff";
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.fillText(icon, 15 + iconWidth / 2, y + height / 2);

    // Reset text alignment for label and value
    ctx.textAlign = "left";
    ctx.textBaseline = "alphabetic";

    // Draw label and value
    ctx.font = "bold 12px Arial";
    ctx.fillStyle = "#ffffff";
    ctx.fillText(label, 55, y + height / 2 + 5);

    ctx.font = "12px Arial";
    const valueWidth = ctx.measureText(value).width;
    ctx.fillText(value, width - valueWidth - padding, y + height / 2 + 5);

    // Draw animation
    drawFluidAnimation(ctx, 15, y, width, height, lightenColor(color, 10), animationOffset);
}

function drawFluidAnimation(ctx, x, y, width, height, color, offset) {
    ctx.save();
    ctx.globalCompositeOperation = 'overlay';
    ctx.globalAlpha = 0.3;

    const gradient = ctx.createLinearGradient(x, y, x + width, y);
    gradient.addColorStop(0, 'rgba(255,255,255,0)');
    gradient.addColorStop(0.5, color);
    gradient.addColorStop(1, 'rgba(255,255,255,0)');

    ctx.fillStyle = gradient;

    // Create a wavy path
    ctx.beginPath();
    ctx.moveTo(x, y);

    const waveHeight = height * 0.4;
    const waveCount = 3;
    for (let i = 0; i <= width; i++) {
        const dx = i / width;
        const offsetY = Math.sin((dx + offset) * Math.PI * 2 * waveCount) * waveHeight;
        ctx.lineTo(x + i, y + height / 2 + offsetY);
    }

    ctx.lineTo(x + width, y + height);
    ctx.lineTo(x, y + height);
    ctx.closePath();
    ctx.fill();

    ctx.restore();
}

function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
    ctx.beginPath();
    ctx.moveTo(x + radius, y);
    ctx.arcTo(x + width, y, x + width, y + height, radius);
    ctx.arcTo(x + width, y + height, x, y + height, radius);
    ctx.arcTo(x, y + height, x, y, radius);
    ctx.arcTo(x, y, x + width, y, radius);
    ctx.closePath();
    if (fill) {
        ctx.fill();
    }
    if (stroke) {
        ctx.stroke();
    }
}

function lightenColor(color, percent) {
    const num = parseInt(color.replace("#", ""), 16),
          amt = Math.round(2.55 * percent),
          R = (num >> 16) + amt,
          G = (num >> 8 & 0x00FF) + amt,
          B = (num & 0x0000FF) + amt;
    return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 +
        (G < 255 ? G < 1 ? 0 : G : 255) * 0x100 +
        (B < 255 ? B < 1 ? 0 : B : 255)).toString(16).slice(1);
}

function getIconForKey(key) {
    const iconMap = {
        "Operating System": "๐Ÿ–ฅ๏ธ",
        "CPU": "โš™๏ธ",
        "RAM": "๐Ÿง ",
        "GPU": "๐ŸŽฎ",
        "CUDA version": "๐Ÿš€",
        "Python version": "๐Ÿ",
        "PyTorch": "๐Ÿ”ฅ",
        "xformers": "โšก",
        "torchvision": "๐Ÿ‘๏ธ",
        "numpy": "๐Ÿ”ข",
        "Pillow": "๐Ÿ–ผ๏ธ",
        "OpenCV": "๐Ÿ“ท",
        "transformers": "๐Ÿค–"
    };
    return iconMap[key] || "โ„น๏ธ";
}

function getColorForKey(key) {
    const colorMap = {
        "Operating System": "#4a90e2",
        "CPU": "#50c878",
        "RAM": "#9b59b6",
        "GPU": "#e74c3c",
        "CUDA version": "#f39c12",
        "Python version": "#3498db",
        "PyTorch": "#e67e22",
        "xformers": "#1abc9c",
        "torchvision": "#34495e",
        "numpy": "#2ecc71",
        "Pillow": "#e84393",
        "OpenCV": "#c5a01c",
        "transformers": "#6c5ce7"
    };
    return colorMap[key] || "#95a5a6";
}