"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * 分析模板 API 路由
 * 提供预定义的分析模板接口
 */
const express_1 = __importDefault(require("express"));
const traceProcessorService_1 = require("../services/traceProcessorService");
const templateManager_1 = require("../services/analysisTemplates/templateManager");
const router = express_1.default.Router();
/**
 * POST /api/template-analysis/auto
 * 自动选择并执行分析模板
 *
 * Body:
 * {
 *   traceId: string;
 *   question: string;
 * }
 */
router.post('/auto', async (req, res) => {
    try {
        const { traceId, question } = req.body;
        if (!traceId || !question) {
            return res.status(400).json({
                error: 'Missing required fields: traceId, question',
            });
        }
        const traceProcessor = (0, traceProcessorService_1.getTraceProcessorService)();
        const templateManager = new templateManager_1.AnalysisTemplateManager(traceProcessor);
        const result = await templateManager.analyzeWithAutoTemplate({
            traceId,
            question,
        });
        if (!result) {
            return res.json({
                success: true,
                templateUsed: false,
                message: 'No suitable template found, use custom SQL instead',
            });
        }
        res.json({
            success: true,
            templateUsed: true,
            templateName: result.templateName,
            summary: result.summary,
            data: result.data,
        });
    }
    catch (error) {
        console.error('Template analysis error:', error);
        res.status(500).json({
            error: 'Template analysis failed',
            message: error.message,
        });
    }
});
/**
 * POST /api/template-analysis/four-quadrant
 * 执行四大象限分析
 *
 * Body:
 * {
 *   traceId: string;
 *   startTs?: number;
 *   endTs?: number;
 *   threadId?: number;
 * }
 */
router.post('/four-quadrant', async (req, res) => {
    try {
        const { traceId, startTs, endTs, threadId } = req.body;
        if (!traceId) {
            return res.status(400).json({
                error: 'Missing required field: traceId',
            });
        }
        const traceProcessor = (0, traceProcessorService_1.getTraceProcessorService)();
        const templateManager = new templateManager_1.AnalysisTemplateManager(traceProcessor);
        const result = await templateManager.fourQuadrantAnalyzer.analyze(traceId, startTs, endTs, threadId);
        res.json({
            success: true,
            data: result,
        });
    }
    catch (error) {
        console.error('Four quadrant analysis error:', error);
        res.status(500).json({
            error: 'Four quadrant analysis failed',
            message: error.message,
        });
    }
});
/**
 * POST /api/template-analysis/cpu-core
 * 执行 CPU 核心分布分析
 *
 * Body:
 * {
 *   traceId: string;
 *   threadId: number;
 *   startTs?: number;
 *   endTs?: number;
 * }
 */
router.post('/cpu-core', async (req, res) => {
    try {
        const { traceId, threadId, startTs, endTs } = req.body;
        if (!traceId || !threadId) {
            return res.status(400).json({
                error: 'Missing required fields: traceId, threadId',
            });
        }
        const traceProcessor = (0, traceProcessorService_1.getTraceProcessorService)();
        const templateManager = new templateManager_1.AnalysisTemplateManager(traceProcessor);
        const result = await templateManager.cpuCoreAnalyzer.analyze(traceId, threadId, startTs, endTs);
        res.json({
            success: true,
            data: result,
        });
    }
    catch (error) {
        console.error('CPU core analysis error:', error);
        res.status(500).json({
            error: 'CPU core analysis failed',
            message: error.message,
        });
    }
});
/**
 * POST /api/template-analysis/frame-stats
 * 执行帧率统计分析
 *
 * Body:
 * {
 *   traceId: string;
 *   packageName?: string;
 *   startTs?: number;
 *   endTs?: number;
 * }
 */
router.post('/frame-stats', async (req, res) => {
    try {
        const { traceId, packageName, startTs, endTs } = req.body;
        if (!traceId) {
            return res.status(400).json({
                error: 'Missing required field: traceId',
            });
        }
        const traceProcessor = (0, traceProcessorService_1.getTraceProcessorService)();
        const templateManager = new templateManager_1.AnalysisTemplateManager(traceProcessor);
        const result = await templateManager.frameStatsAnalyzer.analyze(traceId, packageName, startTs, endTs);
        res.json({
            success: true,
            data: result,
        });
    }
    catch (error) {
        console.error('Frame stats analysis error:', error);
        res.status(500).json({
            error: 'Frame stats analysis failed',
            message: error.message,
        });
    }
});
exports.default = router;
//# sourceMappingURL=templateAnalysisRoutes.js.map