refactor(insight): remove debug logging and unused test generator

This commit is contained in:
DragonnZhang 2026-01-23 18:02:09 +08:00
parent 733522c076
commit 2931e75a17
5 changed files with 64 additions and 180 deletions

View file

@ -6,7 +6,6 @@
import fs from 'fs/promises';
import path from 'path';
// Import from the existing insight server for now to avoid module resolution issues
import type {
InsightData,
HeatMapData,
@ -34,8 +33,8 @@ async function readJsonlFile<T>(filePath: string): Promise<T[]> {
const content = await fs.readFile(filePath, 'utf-8');
return content
.split('\n')
.filter(line => line.trim())
.map(line => JSON.parse(line) as T);
.filter((line) => line.trim())
.map((line) => JSON.parse(line) as T);
} catch (error) {
console.error(`Error reading JSONL file ${filePath}:`, error);
return [];
@ -43,12 +42,6 @@ async function readJsonlFile<T>(filePath: string): Promise<T[]> {
}
export class DataProcessor {
private debugLog(message: string) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ${message}`;
console.log(logMessage);
}
// Helper function to format date as YYYY-MM-DD
private formatDate(date: Date): string {
return date.toISOString().split('T')[0];
@ -243,7 +236,7 @@ export class DataProcessor {
chatFiles = files.filter((file) => file.endsWith('.jsonl'));
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
this.debugLog(
console.log(
`Error reading chats directory for project ${projectDir}: ${error}`,
);
}
@ -295,9 +288,9 @@ export class DataProcessor {
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
// Base directory doesn't exist, return empty insights
this.debugLog(`Base directory does not exist: ${baseDir}`);
console.log(`Base directory does not exist: ${baseDir}`);
} else {
this.debugLog(`Error reading base directory: ${error}`);
console.log(`Error reading base directory: ${error}`);
}
}
@ -335,7 +328,11 @@ export class DataProcessor {
}
// Calculate achievements
const achievements = this.calculateAchievements(activeHours, heatmap, tokenUsage);
const achievements = this.calculateAchievements(
activeHours,
heatmap,
tokenUsage,
);
return {
heatmap,
@ -349,4 +346,4 @@ export class DataProcessor {
achievements,
};
}
}
}

View file

@ -20,12 +20,6 @@ export class StaticInsightGenerator {
this.templateRenderer = new TemplateRenderer();
}
private debugLog(message: string) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ${message}`;
console.log(logMessage);
}
// Ensure the output directory exists
private async ensureOutputDirectory(): Promise<string> {
const outputDir = path.join(os.homedir(), '.qwen', 'insights');
@ -36,14 +30,13 @@ export class StaticInsightGenerator {
// Generate the static insight HTML file
async generateStaticInsight(baseDir: string): Promise<string> {
try {
this.debugLog('Starting static insight generation...');
// Process data
this.debugLog('Processing insight data...');
const insights: InsightData = await this.dataProcessor.generateInsights(baseDir);
console.log('Processing insight data...');
const insights: InsightData =
await this.dataProcessor.generateInsights(baseDir);
// Render HTML
this.debugLog('Rendering HTML template...');
console.log('Rendering HTML template...');
const html = await this.templateRenderer.renderInsightHTML(insights);
// Ensure output directory exists
@ -51,36 +44,14 @@ export class StaticInsightGenerator {
const outputPath = path.join(outputDir, 'insight.html');
// Write the HTML file
this.debugLog(`Writing HTML file to: ${outputPath}`);
console.log(`Writing HTML file to: ${outputPath}`);
await fs.writeFile(outputPath, html, 'utf-8');
this.debugLog('Static insight generation completed successfully');
console.log('Static insight generation completed successfully');
return outputPath;
} catch (error) {
this.debugLog(`Error generating static insight: ${error}`);
console.log(`Error generating static insight: ${error}`);
throw error;
}
}
// Get the default output path
getDefaultOutputPath(): string {
return path.join(os.homedir(), '.qwen', 'insights', 'insight.html');
}
// Check if insight file exists
async insightFileExists(): Promise<boolean> {
try {
const outputPath = this.getDefaultOutputPath();
await fs.access(outputPath);
return true;
} catch {
return false;
}
}
// Get insight file stats (for checking modification time)
async getInsightFileStats() {
const outputPath = this.getDefaultOutputPath();
return await fs.stat(outputPath);
}
}
}

View file

@ -8,7 +8,7 @@ import fs from 'fs/promises';
import path from 'path';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import type { InsightData, StaticInsightTemplateData } from '../types/StaticInsightTypes.js';
import type { InsightData } from '../types/StaticInsightTypes.js';
export class TemplateRenderer {
private templateDir: string;
@ -19,14 +19,6 @@ export class TemplateRenderer {
this.templateDir = path.join(__dirname, '..', 'templates');
}
// Safe JSON stringification to prevent XSS
private safeJsonStringify(data: any): string {
return JSON.stringify(data)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026');
}
// Load template files
private async loadTemplate(): Promise<string> {
const templatePath = path.join(this.templateDir, 'insight-template.html');
@ -39,56 +31,26 @@ export class TemplateRenderer {
}
private async loadScripts(): Promise<string> {
const scriptsPath = path.join(this.templateDir, 'scripts', 'insight-app.js');
const scriptsPath = path.join(
this.templateDir,
'scripts',
'insight-app.js',
);
return await fs.readFile(scriptsPath, 'utf-8');
}
// Generate current timestamp
private generateTimestamp(): string {
return new Date().toLocaleString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short',
});
}
// Render the complete HTML file
async renderInsightHTML(insights: InsightData): Promise<string> {
const template = await this.loadTemplate();
const styles = await this.loadStyles();
const scripts = await this.loadScripts();
const generatedTime = this.generateTimestamp();
// Create empty content placeholder - content will be generated by JavaScript
const content = '<!-- Content will be generated by JavaScript -->';
// Replace all placeholders
let html = template;
html = html.replace('{{STYLES_PLACEHOLDER}}', styles);
html = html.replace('{{CONTENT_PLACEHOLDER}}', content);
html = html.replace('{{DATA_PLACEHOLDER}}', this.safeJsonStringify(insights));
html = html.replace('{{DATA_PLACEHOLDER}}', JSON.stringify(insights));
html = html.replace('{{SCRIPTS_PLACEHOLDER}}', scripts);
html = html.replace('{{GENERATED_TIME}}', generatedTime);
return html;
}
// Create template data object
async createTemplateData(insights: InsightData): Promise<StaticInsightTemplateData> {
const styles = await this.loadStyles();
const scripts = await this.loadScripts();
const generatedTime = this.generateTimestamp();
return {
styles,
content: '<!-- Content will be generated by JavaScript -->',
data: insights,
scripts,
generatedTime,
};
}
}
}