"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = exports.externalServiceUp = exports.cm_docs_operations = exports.cm_docs_cache_misses = exports.cm_docs_cache_hits = exports.emailTemplateCacheMiss = exports.emailTemplateCacheHit = exports.emailTemplateRollback = exports.emailTestSent = exports.emailTemplatesUpdated = exports.cm_geocode_duration = exports.cm_geocode_requests_total = exports.cm_geocode_cache_misses = exports.cm_geocode_cache_hits = exports.locationResultCount = exports.locationQueryCount = exports.locationQueryDuration = exports.shiftSignupsTotal = exports.activeCanvassSessions = exports.canvassVisitsTotal = exports.responseSubmissionsTotal = exports.campaignEmailsTotal = exports.activeSessions = exports.loginAttemptsTotal = exports.emailSendDuration = exports.emailQueueSize = exports.emailsFailedTotal = exports.emailsSentTotal = exports.httpRequestsTotal = exports.httpRequestDuration = void 0;
exports.recordEmailSent = recordEmailSent;
exports.recordEmailFailed = recordEmailFailed;
exports.setEmailQueueSize = setEmailQueueSize;
exports.recordLoginAttempt = recordLoginAttempt;
exports.setActiveSessions = setActiveSessions;
exports.recordCampaignEmail = recordCampaignEmail;
exports.recordResponseSubmission = recordResponseSubmission;
exports.recordCanvassVisit = recordCanvassVisit;
exports.setActiveCanvassSessions = setActiveCanvassSessions;
exports.recordShiftSignup = recordShiftSignup;
exports.recordLocationQuery = recordLocationQuery;
exports.setServiceUp = setServiceUp;
exports.recordEmailTemplateUpdate = recordEmailTemplateUpdate;
exports.recordEmailTest = recordEmailTest;
exports.recordTemplateRollback = recordTemplateRollback;
exports.recordTemplateCacheHit = recordTemplateCacheHit;
exports.recordTemplateCacheMiss = recordTemplateCacheMiss;
const prom_client_1 = __importDefault(require("prom-client"));
const env_1 = require("../config/env");
const register = new prom_client_1.default.Registry();
exports.register = register;
register.setDefaultLabels({
    app: 'changemaker-v2-api',
    instance: env_1.env.INSTANCE_LABEL || env_1.env.DOMAIN || 'unknown',
});
prom_client_1.default.collectDefaultMetrics({ register });
// --- HTTP Metrics (existing) ---
exports.httpRequestDuration = new prom_client_1.default.Histogram({
    name: 'http_request_duration_seconds',
    help: 'Duration of HTTP requests in seconds',
    labelNames: ['method', 'route', 'status_code'],
    buckets: [0.01, 0.05, 0.1, 0.3, 0.5, 1, 3, 5, 10],
    registers: [register],
});
exports.httpRequestsTotal = new prom_client_1.default.Counter({
    name: 'http_requests_total',
    help: 'Total number of HTTP requests',
    labelNames: ['method', 'route', 'status_code'],
    registers: [register],
});
// --- Email Metrics ---
exports.emailsSentTotal = new prom_client_1.default.Counter({
    name: 'cm_emails_sent_total',
    help: 'Total campaign emails sent successfully',
    labelNames: ['campaign_id'],
    registers: [register],
});
exports.emailsFailedTotal = new prom_client_1.default.Counter({
    name: 'cm_emails_failed_total',
    help: 'Total campaign emails that failed to send',
    labelNames: ['campaign_id', 'error_type'],
    registers: [register],
});
exports.emailQueueSize = new prom_client_1.default.Gauge({
    name: 'cm_email_queue_size',
    help: 'Current email queue size (waiting + active)',
    registers: [register],
});
exports.emailSendDuration = new prom_client_1.default.Histogram({
    name: 'cm_email_send_duration_seconds',
    help: 'Duration of individual email sends',
    buckets: [0.1, 0.5, 1, 2, 5, 10, 30],
    registers: [register],
});
// --- Auth Metrics ---
exports.loginAttemptsTotal = new prom_client_1.default.Counter({
    name: 'cm_login_attempts_total',
    help: 'Total login attempts',
    labelNames: ['status'],
    registers: [register],
});
exports.activeSessions = new prom_client_1.default.Gauge({
    name: 'cm_active_sessions',
    help: 'Number of active refresh tokens (approximate session count)',
    registers: [register],
});
// --- Campaign / Influence Metrics ---
exports.campaignEmailsTotal = new prom_client_1.default.Counter({
    name: 'cm_campaign_emails_total',
    help: 'Total campaign emails created (SMTP + mailto)',
    labelNames: ['campaign_id'],
    registers: [register],
});
exports.responseSubmissionsTotal = new prom_client_1.default.Counter({
    name: 'cm_response_submissions_total',
    help: 'Total response submissions from public',
    registers: [register],
});
// --- Canvass / Map Metrics ---
exports.canvassVisitsTotal = new prom_client_1.default.Counter({
    name: 'cm_canvass_visits_total',
    help: 'Total canvass visits recorded',
    labelNames: ['outcome'],
    registers: [register],
});
exports.activeCanvassSessions = new prom_client_1.default.Gauge({
    name: 'cm_active_canvass_sessions',
    help: 'Number of active canvass sessions',
    registers: [register],
});
exports.shiftSignupsTotal = new prom_client_1.default.Counter({
    name: 'cm_shift_signups_total',
    help: 'Total shift signups',
    registers: [register],
});
// --- Location Query Metrics ---
exports.locationQueryDuration = new prom_client_1.default.Histogram({
    name: 'cm_map_location_query_duration_seconds',
    help: 'Duration of location queries',
    labelNames: ['endpoint', 'has_bounds'],
    buckets: [0.01, 0.05, 0.1, 0.2, 0.5, 1, 2, 5],
    registers: [register],
});
exports.locationQueryCount = new prom_client_1.default.Counter({
    name: 'cm_map_location_query_count_total',
    help: 'Total location queries',
    labelNames: ['endpoint', 'has_bounds'],
    registers: [register],
});
exports.locationResultCount = new prom_client_1.default.Histogram({
    name: 'cm_map_location_result_count',
    help: 'Number of locations returned per query',
    labelNames: ['endpoint'],
    buckets: [10, 50, 100, 500, 1000, 5000, 10000],
    registers: [register],
});
// --- Geocoding Metrics ---
exports.cm_geocode_cache_hits = new prom_client_1.default.Counter({
    name: 'cm_geocode_cache_hits_total',
    help: 'Total geocoding cache hits',
    registers: [register],
});
exports.cm_geocode_cache_misses = new prom_client_1.default.Counter({
    name: 'cm_geocode_cache_misses_total',
    help: 'Total geocoding cache misses',
    registers: [register],
});
exports.cm_geocode_requests_total = new prom_client_1.default.Counter({
    name: 'cm_geocode_requests_total',
    help: 'Total geocoding requests',
    labelNames: ['provider', 'status'],
    registers: [register],
});
exports.cm_geocode_duration = new prom_client_1.default.Histogram({
    name: 'cm_geocode_duration_seconds',
    help: 'Duration of geocoding requests',
    labelNames: ['provider'],
    buckets: [0.1, 0.5, 1, 2, 5, 10, 30],
    registers: [register],
});
// --- Email Template Metrics ---
exports.emailTemplatesUpdated = new prom_client_1.default.Counter({
    name: 'cm_email_templates_updated_total',
    help: 'Email templates updated',
    labelNames: ['template_key', 'user_role'],
    registers: [register],
});
exports.emailTestSent = new prom_client_1.default.Counter({
    name: 'cm_email_test_sent_total',
    help: 'Test emails sent',
    labelNames: ['template_key', 'success'],
    registers: [register],
});
exports.emailTemplateRollback = new prom_client_1.default.Counter({
    name: 'cm_email_template_rollback_total',
    help: 'Template rollbacks',
    labelNames: ['template_key'],
    registers: [register],
});
exports.emailTemplateCacheHit = new prom_client_1.default.Counter({
    name: 'cm_email_template_cache_hit_total',
    help: 'Template cache hits',
    labelNames: ['template_key'],
    registers: [register],
});
exports.emailTemplateCacheMiss = new prom_client_1.default.Counter({
    name: 'cm_email_template_cache_miss_total',
    help: 'Template cache misses',
    labelNames: ['template_key'],
    registers: [register],
});
// --- Docs Editor Metrics ---
exports.cm_docs_cache_hits = new prom_client_1.default.Counter({
    name: 'cm_docs_cache_hits_total',
    help: 'Documentation cache hits',
    labelNames: ['type'], // 'tree' or 'file'
    registers: [register],
});
exports.cm_docs_cache_misses = new prom_client_1.default.Counter({
    name: 'cm_docs_cache_misses_total',
    help: 'Documentation cache misses',
    labelNames: ['type'],
    registers: [register],
});
exports.cm_docs_operations = new prom_client_1.default.Counter({
    name: 'cm_docs_operations_total',
    help: 'Documentation file operations',
    labelNames: ['operation'], // 'read', 'write', 'create', 'delete', 'rename'
    registers: [register],
});
// --- External Service Health ---
exports.externalServiceUp = new prom_client_1.default.Gauge({
    name: 'cm_external_service_up',
    help: 'Whether an external service is reachable (1=up, 0=down)',
    labelNames: ['service'],
    registers: [register],
});
// --- Helper Functions ---
function recordEmailSent(campaignId) {
    exports.emailsSentTotal.inc({ campaign_id: campaignId });
}
function recordEmailFailed(campaignId, errorType = 'unknown') {
    exports.emailsFailedTotal.inc({ campaign_id: campaignId, error_type: errorType });
}
function setEmailQueueSize(size) {
    exports.emailQueueSize.set(size);
}
function recordLoginAttempt(status) {
    exports.loginAttemptsTotal.inc({ status });
}
function setActiveSessions(count) {
    exports.activeSessions.set(count);
}
function recordCampaignEmail(campaignId) {
    exports.campaignEmailsTotal.inc({ campaign_id: campaignId });
}
function recordResponseSubmission() {
    exports.responseSubmissionsTotal.inc();
}
function recordCanvassVisit(outcome) {
    exports.canvassVisitsTotal.inc({ outcome });
}
function setActiveCanvassSessions(count) {
    exports.activeCanvassSessions.set(count);
}
function recordShiftSignup() {
    exports.shiftSignupsTotal.inc();
}
function recordLocationQuery(endpoint, hasBounds, resultCount, durationSeconds) {
    exports.locationQueryCount.inc({ endpoint, has_bounds: hasBounds.toString() });
    exports.locationQueryDuration.observe({ endpoint, has_bounds: hasBounds.toString() }, durationSeconds);
    exports.locationResultCount.observe({ endpoint }, resultCount);
}
function setServiceUp(service, up) {
    exports.externalServiceUp.set({ service }, up ? 1 : 0);
}
function recordEmailTemplateUpdate(templateKey, userRole) {
    exports.emailTemplatesUpdated.inc({ template_key: templateKey, user_role: userRole });
}
function recordEmailTest(templateKey, success) {
    exports.emailTestSent.inc({ template_key: templateKey, success: success.toString() });
}
function recordTemplateRollback(templateKey) {
    exports.emailTemplateRollback.inc({ template_key: templateKey });
}
function recordTemplateCacheHit(templateKey) {
    exports.emailTemplateCacheHit.inc({ template_key: templateKey });
}
function recordTemplateCacheMiss(templateKey) {
    exports.emailTemplateCacheMiss.inc({ template_key: templateKey });
}
//# sourceMappingURL=metrics.js.map