All files / packages/dataflint-mcp/src dataflint-job-service.ts

87.17% Statements 102/117
92.3% Branches 24/26
91.66% Functions 11/12
87.17% Lines 102/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x     1x 10x 10x 10x   10x 10x 10x 10x 10x 10x 10x 10x   10x                   10x 2x 2x 2x 2x 2x 2x 2x 2x 2x   10x 3x 3x 3x 3x 3x   3x   3x 1x 1x 1x 1x 1x   2x 2x 2x   2x 3x   10x 2x 2x   10x 1x 1x   10x 1x 1x   10x 1x 1x   10x 5x 5x 5x 5x 5x 5x 5x   5x 2x 2x 2x 2x 2x 2x   3x 3x 3x   5x 5x   5x 5x   10x 3x 3x 3x 3x 3x 3x   2x 2x 2x 2x 2x 2x 2x   2x       2x         2x 2x 3x 2x 2x 2x 3x 3x 3x 10x  
import { DataFlintMcpState } from "./state";
import { Job, SqlQueriesCode, Node } from "./dataflint-server-models";
import { DataFlintServerService } from "./dataflint-server-service";
import { Logger } from "./logger";
import { IMcpLogger } from "./types";
 
export class DataFlintJobService {
    private dataflintService: DataFlintServerService;
    private mcpState: DataFlintMcpState;
    private logger: IMcpLogger;
 
    constructor(
        dataflintService: DataFlintServerService,
        mcpState: DataFlintMcpState
    ) {
        this.dataflintService = dataflintService;
        this.mcpState = mcpState;
        this.logger = Logger.getInstance();
    }
 
    async getCachedOrFetchJobPlan(jobId: number): Promise<any> {
        return this.getCachedOrFetch(
            jobId,
            () => this.mcpState.getJobPlan(jobId),
            () => this.dataflintService.getJobPlan(jobId),
            (data) => this.mcpState.updateJobPlan(jobId, data),
            "plan"
        );
    }
 
    async getCachedOrFetchCode(jobId: number): Promise<SqlQueriesCode> {
        const cacheKey = `${jobId}`;
        return this.getCachedOrFetch(
            jobId,
            () => this.mcpState.getJobGeneratedCode(cacheKey),
            () => this.dataflintService.getSqlQueriesCode(jobId, {}),
            (data) => this.mcpState.updateJobGeneratedCode(cacheKey, data),
            "code"
        );
    }
 
    async getNodeDetails(
        jobId: number,
        sqlId: number,
        nodeId: number
    ): Promise<Node | null> {
        await this.getCachedOrFetchEnrichedSql(jobId);
 
        const node = this.mcpState.getNode(jobId, sqlId, nodeId);
 
        if (!node) {
            this.logger.warn(
                `Node ID ${nodeId} not found for SQL ${sqlId} in job ${jobId}`
            );
            return null;
        }
 
        this.logger.debug(
            `Retrieved node ${nodeId} (${node.enrichedName}) for SQL ${sqlId} in job ${jobId}`
        );
 
        return node;
    }
 
    getJob(jobId: number): Job | undefined {
        return this.mcpState.getJob(jobId);
    }
 
    updateJobGeneratedCode(cacheKey: string, code: SqlQueriesCode): void {
        this.mcpState.updateJobGeneratedCode(cacheKey, code);
    }
 
    updateApplicationJobs(jobs: any[]): void {
        this.mcpState.updateApplicationJobs(jobs);
    }
 
    getDataFlintService(): DataFlintServerService {
        return this.dataflintService;
    }
 
    private async getCachedOrFetch<T>(
        jobId: number,
        getCached: () => T | null | undefined,
        fetchFromApi: () => Promise<T>,
        updateCache: (data: T) => void,
        dataType: string
    ): Promise<T> {
        const cached = getCached();
 
        if (cached) {
            const itemCount = Array.isArray(cached) ? cached.length : "data";
            this.logger.debug(
                `Retrieved ${itemCount} ${dataType} from cache for job ${jobId}`
            );
            return cached;
        }
 
        this.logger.info(`Fetching ${dataType} for job ${jobId} (cache miss)`);
        const fetched = await fetchFromApi();
        updateCache(fetched);
 
        const itemCount = Array.isArray(fetched) ? fetched.length : "data";
        this.logger.info(`Cached ${itemCount} ${dataType} for job ${jobId}`);
 
        return fetched;
    }
 
    async getCachedOrFetchEnrichedSql(
        jobId: number
    ): Promise<Map<number, any>> {
        return this.getCachedOrFetch(
            jobId,
            () => this.mcpState.getJobEnrichedSql(jobId),
            async () => {
                // Request only the executions section, which returns an array of SQL queries
                const executions = await this.dataflintService.getJobPlan(
                    jobId,
                    {
                        section: "executions",
                    }
                );
                const map = new Map<number, any>();
                // executions is an array when section="executions" is specified
                if (Array.isArray(executions)) {
                    for (const sql of executions) {
                        map.set(sql.id, sql);
                    }
                } else if (executions !== null && executions !== undefined) {
                    this.logger.warn(
                        `Unexpected response format from getJobPlan for job ${jobId}: expected array, got ${typeof executions}`
                    );
                }
                return map;
            },
            (data) => {
                const list = Array.from(data.values());
                this.mcpState.updateJobEnrichedSql(jobId, list);
            },
            "enriched SQL"
        );
    }
}