Optimize gateway comms reload behavior and strengthen regression coverage (#496)

This commit is contained in:
Lingxuan Zuo
2026-03-15 20:36:48 +08:00
committed by GitHub
Unverified
parent 08960d700f
commit 1dbe4a8466
36 changed files with 1511 additions and 197 deletions

View File

@@ -10,7 +10,7 @@
import { app } from 'electron';
import { join } from 'path';
import { existsSync, mkdirSync, appendFileSync } from 'fs';
import { appendFile, readFile, readdir, stat } from 'fs/promises';
import { appendFile, open, readdir, stat } from 'fs/promises';
/**
* Log levels
@@ -230,11 +230,33 @@ export function getRecentLogs(count?: number, minLevel?: LogLevel): string[] {
*/
export async function readLogFile(tailLines = 200): Promise<string> {
if (!logFilePath) return '(No log file found)';
const safeTailLines = Math.max(1, Math.floor(tailLines));
try {
const content = await readFile(logFilePath, 'utf-8');
const lines = content.split('\n');
if (lines.length <= tailLines) return content;
return lines.slice(-tailLines).join('\n');
const file = await open(logFilePath, 'r');
try {
const fileStat = await file.stat();
if (fileStat.size === 0) return '';
const chunkSize = 64 * 1024;
let position = fileStat.size;
let content = '';
let lineCount = 0;
while (position > 0 && lineCount <= safeTailLines) {
const bytesToRead = Math.min(chunkSize, position);
position -= bytesToRead;
const buffer = Buffer.allocUnsafe(bytesToRead);
await file.read(buffer, 0, bytesToRead, position);
content = `${buffer.toString('utf-8')}${content}`;
lineCount = content.split('\n').length - 1;
}
const lines = content.split('\n');
if (lines.length <= safeTailLines) return content;
return lines.slice(-safeTailLines).join('\n');
} finally {
await file.close();
}
} catch (err) {
return `(Failed to read log file: ${err})`;
}

View File

@@ -11,6 +11,15 @@ const TELEMETRY_SHUTDOWN_TIMEOUT_MS = 1500;
let posthogClient: PostHog | null = null;
let distinctId: string = '';
function getCommonProperties(): Record<string, string> {
return {
$app_version: app.getVersion(),
$os: process.platform,
os_tag: process.platform,
arch: process.arch,
};
}
function isIgnorablePostHogShutdownError(error: unknown): boolean {
if (!(error instanceof Error)) {
return false;
@@ -54,12 +63,7 @@ export async function initTelemetry(): Promise<void> {
logger.debug(`Generated new machine ID for telemetry: ${distinctId}`);
}
// Common properties for all events
const properties = {
$app_version: app.getVersion(),
$os: process.platform,
arch: process.arch,
};
const properties = getCommonProperties();
// Check if this is a new installation
const hasReportedInstall = await getSetting('hasReportedInstall');
@@ -86,6 +90,29 @@ export async function initTelemetry(): Promise<void> {
}
}
export function trackMetric(event: string, properties: Record<string, unknown> = {}): void {
logger.info(`[metric] ${event}`, properties);
}
export function captureTelemetryEvent(event: string, properties: Record<string, unknown> = {}): void {
if (!posthogClient || !distinctId) {
return;
}
try {
posthogClient.capture({
distinctId,
event,
properties: {
...getCommonProperties(),
...properties,
},
});
} catch (error) {
logger.debug(`Failed to capture telemetry event "${event}":`, error);
}
}
/**
* Best-effort telemetry shutdown that never blocks app exit on network issues.
*/