Spaces:
Running
Running
| 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.") | |