"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelInvocationView = void 0;
const typeorm_1 = require("typeorm");
const trace_1 = require("../../../shared/src/types/trace");
const Trace_1 = require("./Trace");
let ModelInvocationView = class ModelInvocationView extends typeorm_1.BaseEntity {
};
exports.ModelInvocationView = ModelInvocationView;
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "totalModelInvocations", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "totalTokens", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "chatModelInvocations", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "tokensWeekAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "tokensMonthAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "tokensYearAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "modelInvocationsWeekAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "modelInvocationsMonthAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], ModelInvocationView.prototype, "modelInvocationsYearAgo", void 0);
exports.ModelInvocationView = ModelInvocationView = __decorate([
    (0, typeorm_1.ViewEntity)({
        expression: (dataSource) => dataSource
            .createQueryBuilder()
            .from(Trace_1.SpanTable, 'span')
            .select(`COUNT(CASE WHEN span.spanKind = '${trace_1.SpanKind.LLM}' THEN 1 END)`, 'totalModelInvocations')
            .addSelect(`COALESCE(SUM(CASE 
            WHEN span.spanKind = '${trace_1.SpanKind.LLM}' AND json_extract(span.attributes, '$.output.usage') IS NOT NULL
            THEN (
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.input_tokens'), 0) AS INTEGER) +
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.output_tokens'), 0) AS INTEGER)
            )
            ELSE 0 
        END), 0)`, 'totalTokens')
            .addSelect(`COUNT(CASE 
            WHEN span.spanKind = '${trace_1.SpanKind.LLM}' AND json_extract(span.attributes, '$.output.usage') IS NOT NULL
            THEN 1 
        END)`, 'chatModelInvocations')
            // 一个月前的统计
            .addSelect(`COALESCE(SUM(CASE 
            WHEN span.spanKind = '${trace_1.SpanKind.LLM}' AND json_extract(span.attributes, '$.output.usage') IS NOT NULL
                AND span.startTime > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 month')
            THEN (
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.input_tokens'), 0) AS INTEGER) +
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.output_tokens'), 0) AS INTEGER)
            )
            ELSE 0 
        END), 0)`, 'tokensMonthAgo')
            // 一周前的统计
            .addSelect(`COALESCE(SUM(CASE 
            WHEN span.spanKind = '${trace_1.SpanKind.LLM}' AND json_extract(span.attributes, '$.output.usage') IS NOT NULL
                AND span.startTime > strftime('%Y-%m-%d %H:%M:%S', 'now', '-7 days')
            THEN (
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.input_tokens'), 0) AS INTEGER) +
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.output_tokens'), 0) AS INTEGER)
            )
            ELSE 0 
        END), 0)`, 'tokensWeekAgo')
            // 一年前的统计
            .addSelect(`COALESCE(SUM(CASE 
            WHEN span.spanKind = '${trace_1.SpanKind.LLM}' AND json_extract(span.attributes, '$.output.usage') IS NOT NULL
                AND span.startTime > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 year')
            THEN (
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.input_tokens'), 0) AS INTEGER) +
                CAST(COALESCE(json_extract(span.attributes, '$.output.usage.output_tokens'), 0) AS INTEGER)
            )
            ELSE 0 
        END), 0)`, 'tokensYearAgo')
            // 一个月内的调用次数
            .addSelect(`COUNT(CASE 
                    WHEN span.spanKind = '${trace_1.SpanKind.LLM}' 
                    AND span.startTime > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 month')
                    THEN 1 
                END)`, 'modelInvocationsMonthAgo')
            // 一周内的调用次数
            .addSelect(`COUNT(CASE 
                    WHEN span.spanKind = '${trace_1.SpanKind.LLM}' 
                    AND span.startTime > strftime('%Y-%m-%d %H:%M:%S', 'now', '-7 days')
                    THEN 1 
                END)`, 'modelInvocationsWeekAgo')
            // 一年内的调用次数
            .addSelect(`COUNT(CASE 
                    WHEN span.spanKind = '${trace_1.SpanKind.LLM}' 
                    AND span.startTime > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 year')
                    THEN 1 
                END)`, 'modelInvocationsYearAgo'),
    })
], ModelInvocationView);
