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 DatabaseService from "./DatabaseService";
import Model from "../../Models/DatabaseModels/TelemetryEntityRelationship";
import OneUptimeDate from "../../Types/Date";
import logger from "../Utils/Logger";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import { reconcileByNaturalKey } from "../Utils/Telemetry/EntityRegistry";
export class TelemetryEntityRelationshipService extends DatabaseService {
    constructor() {
        super(Model);
    }
    /**
     * Forward-only topology reconciliation: upsert a row per directed edge and
     * bump `lastSeenAt`. Resilient — a topology-graph failure must never break
     * signal ingest, so every error is swallowed (logged). Callers throttle
     * this (see `reconcileEntityRegistryThrottled` in
     * `Common/Server/Utils/Telemetry/EntityRegistry`), and only pass edges
     * among registry-promoted entities so every edge endpoint has a row.
     */
    async reconcileRelationships(data) {
        for (const edge of data.edges) {
            try {
                await this.upsertRelationship({ projectId: data.projectId, edge });
            }
            catch (err) {
                logger.error(`TelemetryEntityRelationshipService: failed to upsert edge ${edge.fromEntityKey}-[${edge.relationshipType}]->${edge.toEntityKey}:`);
                logger.error(err);
            }
        }
    }
    async upsertRelationship(data) {
        const { projectId, edge } = data;
        const now = OneUptimeDate.getCurrentDate();
        await reconcileByNaturalKey({
            service: this,
            query: {
                projectId,
                fromEntityKey: edge.fromEntityKey,
                toEntityKey: edge.toEntityKey,
                relationshipType: edge.relationshipType,
            },
            lastSeenAt: now,
            describe: `edge ${edge.fromEntityKey}-[${edge.relationshipType}]->${edge.toEntityKey}`,
            buildModel: () => {
                const model = new Model();
                model.projectId = projectId;
                model.fromEntityKey = edge.fromEntityKey;
                model.toEntityKey = edge.toEntityKey;
                model.relationshipType = edge.relationshipType;
                model.firstSeenAt = now;
                model.lastSeenAt = now;
                return model;
            },
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], TelemetryEntityRelationshipService.prototype, "reconcileRelationships", null);
export default new TelemetryEntityRelationshipService();
//# sourceMappingURL=TelemetryEntityRelationshipService.js.map