"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.RunView = void 0;
const typeorm_1 = require("typeorm");
let RunView = class RunView extends typeorm_1.BaseEntity {
};
exports.RunView = RunView;
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "totalProjects", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "totalRuns", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "projectsWeekAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "runsWeekAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "projectsMonthAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "runsMonthAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "projectsYearAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", Number)
], RunView.prototype, "runsYearAgo", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", String)
], RunView.prototype, "monthlyRuns", void 0);
exports.RunView = RunView = __decorate([
    (0, typeorm_1.ViewEntity)({
        expression: (dataSource) => dataSource
            .createQueryBuilder()
            .select('COUNT(DISTINCT run.project)', 'totalProjects')
            .addSelect('COUNT(*)', 'totalRuns')
            // 一个月前的项目和运行数
            .addSelect(`COUNT(DISTINCT CASE 
            WHEN run.timestamp > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 month') 
            THEN run.project END)`, 'projectsMonthAgo')
            .addSelect(`COUNT(CASE 
            WHEN run.timestamp > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 month')
            THEN 1 END)`, 'runsMonthAgo')
            // 最近一周的项目和运行数
            .addSelect(`COUNT(DISTINCT CASE  
            WHEN run.timestamp > strftime('%Y-%m-%d %H:%M:%S', 'now', '-7 days')
            THEN run.project END)`, 'projectsWeekAgo')
            .addSelect(`COUNT(CASE 
            WHEN run.timestamp > strftime('%Y-%m-%d %H:%M:%S', 'now', '-7 days')
            THEN 1 END)`, 'runsWeekAgo')
            // 一年前的项目和运行数
            .addSelect(`COUNT(DISTINCT CASE 
            WHEN run.timestamp > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 year')
            THEN run.project END)`, 'projectsYearAgo')
            .addSelect(`COUNT(CASE 
            WHEN run.timestamp > strftime('%Y-%m-%d %H:%M:%S', 'now', '-1 year')
            THEN 1 END)`, 'runsYearAgo')
            .addSelect(`(
            WITH RECURSIVE
            months(date) AS (
                SELECT date('now', 'start of month', '-11 months')
                UNION ALL
                SELECT date(date, '+1 month')
                FROM months
                WHERE date < date('now', 'start of month')
            ),
            monthly_counts AS (
                SELECT 
                    strftime('%Y-%m', months.date) as month,
                    COUNT(CASE 
                        WHEN strftime('%Y-%m', run.timestamp) = strftime('%Y-%m', months.date) 
                        THEN 1 
                    END) as count
                FROM months
                LEFT JOIN run_table run ON strftime('%Y-%m', run.timestamp) = strftime('%Y-%m', months.date)
                GROUP BY strftime('%Y-%m', months.date)
                ORDER BY month DESC
            )
            SELECT json_group_array(
                json_object(
                    'month', month,
                    'count', count
                )
            )
            FROM monthly_counts
        )`, 'monthlyRuns')
            .from('run_table', 'run'),
    })
], RunView);
