File size: 6,452 Bytes
24a2ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'r', encoding='utf-8') as f:
    content = f.read()

# 1. Remove dead totalWaitTime/baseWaitTime/sizeWaitTime variables (around line 240)
old_wait = """    const baseWaitTime = imgCount > 0 ? Math.min(500 + imgCount * 400, 6000) : 500;
    const sizeWaitTime = imgSizeMB > 0 ? Math.min(imgSizeMB * 100, 3000) : 0;
    const totalWaitTime = Math.min(baseWaitTime + sizeWaitTime, 8000);"""
if old_wait in content:
    content = content.replace(old_wait, "")
    print("CLEANED: Removed dead baseWaitTime/sizeWaitTime/totalWaitTime variables")
else:
    print("INFO: Wait time variables already cleaned or different")

# 2. Remove dead _rawToFormatted object (around line 1303)
# This is a block that builds a mapping but never uses it
# Find the pattern: var _rawToFormatted = {}; ... (until it's no longer referenced)
raw_to_fmt_start = "    var _rawToFormatted = {};"
raw_to_fmt_end = "    }"  # end of the forEach that populates it
idx = content.find(raw_to_fmt_start)
if idx >= 0:
    # Find the end of this block - it's the forEach that populates _rawToFormatted
    # The next section starts with "var chartSeries" or similar
    # Find the closing brace of the forEach
    search_from = idx
    brace_count = 0
    end_idx = -1
    in_block = False
    i = idx
    while i < len(content):
        if content[i:i+2] == "// ":
            # Skip to end of line
            newline = content.find("\n", i)
            if newline < 0: break
            i = newline + 1
            continue
        if content[i] == "{":
            brace_count += 1
            in_block = True
        elif content[i] == "}":
            brace_count -= 1
            if in_block and brace_count == 0:
                end_idx = i + 1
                break
        i += 1

    if end_idx > 0:
        block = content[idx:end_idx]
        # Check if _rawToFormatted is used after this block
        remaining = content[end_idx:]
        if "_rawToFormatted" not in remaining:
            content = content[:idx] + content[end_idx:]
            print("CLEANED: Removed dead _rawToFormatted object")
        else:
            print("WARNING: _rawToFormatted is still referenced, skipping")
    else:
        print("WARNING: Could not find end of _rawToFormatted block")
else:
    print("INFO: _rawToFormatted already cleaned or not found")

# 3. Remove dead pieActualDataKey
if "var pieActualDataKey = valueKey" in content:
    content = content.replace("var pieActualDataKey = valueKey || chartSeries[0]?.dataKey || 'value';\n", "")
    print("CLEANED: Removed dead pieActualDataKey")
else:
    print("INFO: pieActualDataKey already cleaned or not found")

# 4. Fix double-count regex pattern for new C(
if "new\\\\s+C\\\\s*\\\\(" in content:
    content = content.replace(
        "(widgetHtml.match(/new\\\\s+C\\\\s*\\\\(/gi) || []).length +\\n                       ",
        ""
    )
    print("CLEANED: Removed double-count new C( regex")
else:
    # Try without double escaping
    if "new\\\\s+C\\\\s*\\\\(" in content:
        content = content.replace(
            "(widgetHtml.match(/new\\s+C\\s*\\(/gi) || []).length +\n                       ",
            ""
        )
        print("CLEANED: Removed double-count new C( regex (alt)")
    else:
        print("INFO: Double-count regex not found or different format")

# 5. Remove duplicate COLORS entries
old_colors = """    const COLORS = [
      '#339CFF', '#40C977', '#FF8549', '#FFD240',
      '#339CFF', '#40C977', '#FF8549', '#FFD240',
      '#339CFF', '#40C977', '#FF8549', '#FFD240',
    ];"""
new_colors = """    const COLORS = ['#339CFF', '#40C977', '#FF8549', '#FFD240'];"""
if old_colors in content:
    content = content.replace(old_colors, new_colors)
    print("CLEANED: Removed duplicate COLORS entries")
else:
    print("INFO: COLORS already cleaned or different format")

# 6. Remove %%INJECTED_SCRIPTS%% placeholder (always replaced with empty string)
old_inject = """.replace('%%INJECTED_SCRIPTS%%', '');"""
if old_inject in content:
    content = content.replace(old_inject, ";")
    print("CLEANED: Removed %%INJECTED_SCRIPTS%% placeholder")
else:
    print("INFO: %%INJECTED_SCRIPTS%% already cleaned or not found")

# 7. Clean up verbose PIE DATA logging
old_pie_log = """    if (chartType === 'pie' || chartType === 'donut') {
      console.log(`[WIDGET] renderChatGPTChart PIE DATA: data=${JSON.stringify(data)}, formattedData=${JSON.stringify(formattedData)}`);
    }"""
if old_pie_log in content:
    content = content.replace(old_pie_log, "")
    print("CLEANED: Removed verbose PIE DATA logging")
else:
    print("INFO: PIE DATA logging already cleaned")

# 8. Clean up nameKey/valueKey verbose logging
old_kv_log = """    if (nameKey) console.log(`[WIDGET] renderChatGPTChart: nameKey=${nameKey}, valueKey=${valueKey}`);"""
if old_kv_log in content:
    content = content.replace(old_kv_log, "")
    print("CLEANED: Removed verbose nameKey/valueKey logging")
else:
    print("INFO: nameKey/valueKey logging already cleaned")

# 9. Remove [WIDGET] DEBUG widgetHtml length logging
old_widget_log = "    console.log(`[WIDGET] DEBUG widgetHtml length=` + widgetHtml.length + `, first 500 chars: ` + widgetHtml.substring(0, 500));"
if old_widget_log in content:
    content = content.replace(old_widget_log, "")
    print("CLEANED: Removed widgetHtml length debug logging")
else:
    print("INFO: widgetHtml length logging already cleaned")

# 10. Remove [WIDGET] extractChartConfig DEBUG logging
old_extract_log = "    console.log(`[WIDGET] extractChartConfig: DEBUG widgetHtml (first 800 chars): ` + widgetHtml.substring(0, 800));"
if old_extract_log in content:
    content = content.replace(old_extract_log, "")
    print("CLEANED: Removed extractChartConfig DEBUG logging")
else:
    print("INFO: extractChartConfig DEBUG logging already cleaned")

# 11. Remove [WIDGET] renderChart DEBUG logging
old_render_log = "    console.log(`[WIDGET] renderChart DEBUG: widgetHtml length=` + widgetHtml.length + `, first 500 chars: ` + widgetHtml.substring(0, 500));"
if old_render_log in content:
    content = content.replace(old_render_log, "")
    print("CLEANED: Removed renderChart DEBUG logging")
else:
    print("INFO: renderChart DEBUG logging already cleaned")

with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'w', encoding='utf-8') as f:
    f.write(content)

print("\nDone.")