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);
};
import CloudResource from "./CloudResource";
import Project from "./Project";
import User from "./User";
import BaseModel from "./DatabaseBaseModel/DatabaseBaseModel";
import Route from "../../Types/API/Route";
import ColumnAccessControl from "../../Types/Database/AccessControl/ColumnAccessControl";
import TableAccessControl from "../../Types/Database/AccessControl/TableAccessControl";
import ColumnLength from "../../Types/Database/ColumnLength";
import ColumnType from "../../Types/Database/ColumnType";
import CrudApiEndpoint from "../../Types/Database/CrudApiEndpoint";
import TableColumn from "../../Types/Database/TableColumn";
import TableColumnType from "../../Types/Database/TableColumnType";
import TableMetadata from "../../Types/Database/TableMetadata";
import TenantColumn from "../../Types/Database/TenantColumn";
import IconProp from "../../Types/Icon/IconProp";
import ObjectID from "../../Types/ObjectID";
import Permission from "../../Types/Permission";
import { Column, Entity, Index, JoinColumn, ManyToOne } from "typeorm";
/*
 * Live inventory of a single managed-compute instance / task
 * (service.instance.id) of a CloudResource, upserted from the telemetry
 * ingest pipeline; not user-editable. CPU / memory are populated when the
 * collector emits container.cpu.utilization / container.memory.usage.
 */
const READ_PERMISSIONS = [
    Permission.ProjectOwner,
    Permission.ProjectAdmin,
    Permission.ProjectMember,
    Permission.Viewer,
    Permission.SettingsAdmin,
    Permission.SettingsMember,
    Permission.SettingsViewer,
    Permission.ReadCloudResource,
];
let CloudResourceInstance = class CloudResourceInstance extends BaseModel {
    constructor() {
        super(...arguments);
        this.project = undefined;
        this.projectId = undefined;
        this.cloudResource = undefined;
        this.cloudResourceId = undefined;
        this.instanceName = undefined;
        this.latestCpuPercent = undefined;
        this.latestMemoryBytes = undefined;
        this.lastSeenAt = undefined;
        this.createdByUserId = undefined;
        this.deletedByUser = undefined;
        this.deletedByUserId = undefined;
    }
};
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        manyToOneRelationColumn: "projectId",
        type: TableColumnType.Entity,
        modelType: Project,
        title: "Project",
        description: "Relation to Project this instance belongs to.",
    }),
    ManyToOne(() => {
        return Project;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "projectId" }),
    __metadata("design:type", Project)
], CloudResourceInstance.prototype, "project", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    Index(),
    TableColumn({
        type: TableColumnType.ObjectID,
        required: true,
        canReadOnRelationQuery: true,
        title: "Project ID",
        description: "ID of the Project this instance belongs to.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], CloudResourceInstance.prototype, "projectId", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        manyToOneRelationColumn: "cloudResourceId",
        type: TableColumnType.Entity,
        modelType: CloudResource,
        title: "Cloud Resource",
        description: "Cloud resource this instance belongs to.",
    }),
    ManyToOne(() => {
        return CloudResource;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "cloudResourceId" }),
    __metadata("design:type", CloudResource)
], CloudResourceInstance.prototype, "cloudResource", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    Index(),
    TableColumn({
        type: TableColumnType.ObjectID,
        required: true,
        canReadOnRelationQuery: true,
        title: "Cloud Resource ID",
        description: "ID of the cloud resource this instance belongs to.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], CloudResourceInstance.prototype, "cloudResourceId", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Instance Name",
        description: "service.instance.id value identifying this instance / task.",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], CloudResourceInstance.prototype, "instanceName", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        required: false,
        type: TableColumnType.Number,
        canReadOnRelationQuery: true,
        title: "Latest CPU Percent",
        description: "Most recent CPU utilization percent for this instance. Null until a metric arrives.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Decimal,
        transformer: {
            to: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                return value;
            },
            from: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                if (typeof value === "number") {
                    return value;
                }
                const parsed = parseFloat(value);
                return isNaN(parsed) ? null : parsed;
            },
        },
    }),
    __metadata("design:type", Number)
], CloudResourceInstance.prototype, "latestCpuPercent", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        required: false,
        type: TableColumnType.BigPositiveNumber,
        canReadOnRelationQuery: true,
        title: "Latest Memory Bytes",
        description: "Most recent memory usage (bytes) for this instance.",
    }),
    Column({
        nullable: true,
        type: ColumnType.BigPositiveNumber,
        transformer: {
            to: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                return Math.trunc(value).toString();
            },
            from: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                const parsed = parseInt(value, 10);
                return isNaN(parsed) ? null : parsed;
            },
        },
    }),
    __metadata("design:type", Number)
], CloudResourceInstance.prototype, "latestMemoryBytes", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        required: true,
        type: TableColumnType.Date,
        canReadOnRelationQuery: true,
        title: "Last Seen At",
        description: "When telemetry for this instance was last observed.",
    }),
    Column({
        nullable: false,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], CloudResourceInstance.prototype, "lastSeenAt", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        type: TableColumnType.ObjectID,
        title: "Created By User ID",
        description: "ID of the user who created this row (ingest writes as root).",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: true,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], CloudResourceInstance.prototype, "createdByUserId", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        manyToOneRelationColumn: "deletedByUserId",
        type: TableColumnType.Entity,
        modelType: User,
        title: "Deleted By User",
        description: "Relation to the user who deleted this row.",
    }),
    ManyToOne(() => {
        return User;
    }, {
        eager: false,
        nullable: true,
        onDelete: "SET NULL",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "deletedByUserId" }),
    __metadata("design:type", User)
], CloudResourceInstance.prototype, "deletedByUser", void 0);
__decorate([
    ColumnAccessControl({ create: [], read: READ_PERMISSIONS, update: [] }),
    TableColumn({
        type: TableColumnType.ObjectID,
        title: "Deleted By User ID",
        description: "ID of the user who deleted this row.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: true,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], CloudResourceInstance.prototype, "deletedByUserId", void 0);
CloudResourceInstance = __decorate([
    TenantColumn("projectId"),
    TableAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
        delete: [],
    }),
    CrudApiEndpoint(new Route("/cloud-resource-instance")),
    TableMetadata({
        tableName: "CloudResourceInstance",
        singularName: "Cloud Resource Instance",
        pluralName: "Cloud Resource Instances",
        icon: IconProp.Cloud,
        tableDescription: "Live inventory of managed-compute instances / tasks (service.instance.id) of a cloud resource, as last reported via OpenTelemetry. Populated by the ingest pipeline; not user-editable.",
    }),
    Index(["projectId", "cloudResourceId", "instanceName"], {
        unique: true,
    }),
    Entity({
        name: "CloudResourceInstance",
    })
], CloudResourceInstance);
export default CloudResourceInstance;
//# sourceMappingURL=CloudResourceInstance.js.map